/**
* 电脑下棋策略
* 在权值最大处下棋
*/
public
void
auto_chess
(){
//找出最大权值
int
[]
m
=
new
int
[
100
];
int
[]
n
=
new
int
[
100
];
int
max
=
value
[
0
][
0
];
int
k
=
0
;
for
(
int
i
=
0
;
i
<
11
;
i
++)
for
(
int
j
=
0
;
j
<
11
;
j
++){
if
(
value
[
i
][
j
]>
max
){
max
=
value
[
i
][
j
];
m
[
0
]=
i
;
n
[
0
]=
j
;
for
(
int
s
=
1
;
s
<=
k
;
s
++){
m
[
s
]=
0
;
n
[
s
]=
0
;
}
k
=
0
;
}
if
(
value
[
i
][
j
]==
max
){
max
=
value
[
i
][
j
];
m
[++
k
]=
i
;
n
[
k
]=
j
;
}
}
java
.
util
.
Random
r
=
new
java
.
util
.
Random
();
int
x
=
r
.
nextInt
(
k
);
//System.out.println("k="+k+",x="+x);
//设置画笔颜色
if
(
flag
==
true
){
//改变画布的颜色
g
.
setColor
(
Color
.
black
);
chesses
[
m
[
x
]][
n
[
x
]]
=
1
;
//黑色
flag
=
false
;
}
else
if
(
flag
==
false
){
//改变画布的颜色
g
.
setColor
(
Color
.
white
);
chesses
[
m
[
x
]][
n
[
x
]]
=
2
;
//白色
flag
=
true
;
}
//将下棋处的权值置为0
value
[
m
[
x
]][
n
[
x
]]=
0
;
print
();
//下棋
g
.
fillOval
(
40
*(
n
[
x
]+
1
)-
15
,
40
*(
m
[
x
]+
1
)-
15
,
30
,
30
);
//确保电脑下棋后人能下棋
flag_chess
=
false
;
list
.
add
(
40
*(
n
[
x
]+
1
));
list
.
add
(
40
*(
m
[
x
]+
1
));
list
.
add
(
g
.
getColor
());
//电脑下棋后判断输赢
win
(
m
[
x
],
n
[
x
]);
}
/**
* 设置所下棋子周围八格的权值
* @param i 所下棋子横坐标
* @param j 所下棋子纵坐标
*/
public
void
set_value
(
int
i
,
int
j
,
int
per
,
int
com
){
int
t1
=
0
,
t2
=
0
,
t
=
0
;
//利用side[]的值判定棋子是活棋还是冲棋,s表示判定的棋子个数
//斜向棋子判定
//左上-----右下
for
(
int
s
=
0
;
s
<
4
;
s
++){
side
[
s
]=
0
;
}
num
=
0
;
for
(
int
m
=
i
,
n
=
j
;
m
<=
i
+
4
&&
n
<=
j
+
4
&&
m
<
11
&&
n
<
11
;
m
++,
n
++){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
}
if
(
chesses
[
m
][
n
]==
com
){
//if(flag1==false)
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
t
=
side
[
num
]+
1
;
t1
=
m
;
t2
=
n
;
//System.out.println("t1="+t1+",t2="+t2+",t="+t);
break
;
}
}
for
(
int
m
=
i
,
n
=
j
;
m
>=
i
-
4
&&
n
>=
j
-
4
&&
m
>=
0
&&
n
>=
0
;
m
--,
n
--){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
num
--;
//if(flag1==false)
if
(
t
==
1
){
//System.out.println("see white!!");
if
(
num
==
4
)
value
[
t1
][
t2
]+=
100
;
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
num
--;
if
(
t
==
1
){
if
(
num
==
4
||
num
==
3
){
value
[
m
][
n
]+=
100
;
value
[
t1
][
t2
]+=
100
;}
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
;
}
if
(
t
==
0
){
if
(
num
==
4
)
value
[
m
][
n
]+=
100
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
}
//}
//右上---左下
for
(
int
s
=
0
;
s
<
4
;
s
++){
side
[
s
]=
0
;
}
num
=
0
;
t1
=
0
;
t2
=
0
;
t
=
0
;
//if(i-4>=0&&j+4<11){
for
(
int
m
=
i
,
n
=
j
;
m
>=
i
-
4
&&
n
<=
j
+
4
&&
m
>=
0
&&
n
<
11
;
m
--,
n
++){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
//if(flag1==false)
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
t
=
side
[
num
]+
1
;
t1
=
m
;
t2
=
n
;
break
;
}
}
for
(
int
m
=
i
,
n
=
j
;
m
<=
i
+
4
&&
n
>=
j
-
4
&&
m
<
11
&&
n
>=
0
;
m
++,
n
--){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
num
--;
if
(
t
==
1
){
if
(
num
==
4
)
value
[
t1
][
t2
]+=
100
;
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
num
--;
//此棋为活棋
if
(
t
==
1
){
if
(
num
==
4
||
num
==
3
){
value
[
m
][
n
]+=
100
;
value
[
t1
][
t2
]+=
100
;}
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
;
}
//此棋为冲棋
if
(
t
==
0
){
if
(
num
==
4
)
value
[
m
][
n
]+=
100
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
}
//}
//横向棋子判定
for
(
int
s
=
0
;
s
<
4
;
s
++){
side
[
s
]=
0
;
}
num
=
0
;
t1
=
0
;
t2
=
0
;
t
=
0
;
//if(j+4<11){
for
(
int
m
=
i
,
n
=
j
;
n
<=
j
+
4
&&
n
<
11
;
n
++){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
//if(flag1==false)
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
t
=
side
[
num
]+
1
;
t1
=
m
;
t2
=
n
;
break
;
}
}
for
(
int
m
=
i
,
n
=
j
;
n
>=
0
;
n
--){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
num
--;
if
(
t
==
1
){
if
(
num
==
4
)
value
[
t1
][
t2
]+=
100
;
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
num
--;
if
(
t
==
1
){
if
(
num
==
4
||
num
==
3
){
value
[
m
][
n
]+=
100
;
value
[
t1
][
t2
]+=
100
;}
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
;
}
if
(
t
==
0
){
if
(
num
==
4
)
value
[
m
][
n
]+=
100
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
}
//}
//竖向棋子判定
for
(
int
s
=
0
;
s
<
4
;
s
++){
side
[
s
]=
0
;
}
num
=
0
;
t1
=
0
;
t2
=
0
;
t
=
0
;
//if(i+4<11){
for
(
int
m
=
i
,
n
=
j
;
m
<=
i
+
4
&&
m
<
11
;
m
++){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
//if(flag1==false)
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
t
=
side
[
num
]+
1
;
t1
=
m
;
t2
=
n
;
break
;
}
}
for
(
int
m
=
i
,
n
=
j
;
m
>=
0
;
m
--){
if
(
chesses
[
m
][
n
]==
per
){
num
++;
//flag1=true;
}
if
(
chesses
[
m
][
n
]==
com
){
num
--;
if
(
t
==
1
){
if
(
num
==
4
)
value
[
t1
][
t2
]+=
100
;
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
if
(
chesses
[
m
][
n
]==
0
){
num
--;
if
(
t
==
1
){
System
.
out
.
println
(
"num41="
+
num
);
if
(
num
==
4
||
num
==
3
){
value
[
m
][
n
]+=
100
;
value
[
t1
][
t2
]+=
100
;}
value
[
t1
][
t2
]+=
num
*(
num
+
1
)*
5
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
;
}
if
(
t
==
0
){
if
(
num
==
4
)
value
[
m
][
n
]+=
100
;
value
[
m
][
n
]+=
num
*(
num
+
1
)*
5
/
2
;
}
break
;
}
}
}