(点击
上方公众号
,可快速关注)
来源:zhikaizhang
链接:http://zhikaizhang.cn/2016/07/29/android黑白棋游戏实现/
接上文
private
static
int
evaluate
(
byte
[][]
chessBoard
,
int
difficulty
)
{
int
whiteEvaluate
=
0
;
int
blackEvaluate
=
0
;
switch
(
difficulty
)
{
case
1
:
for
(
int
i
=
0
;
i
8
;
i
++
)
{
for
(
int
j
=
0
;
j
8
;
j
++
)
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
1
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
1
;
}
}
}
break
;
case
2
:
case
3
:
case
4
:
for
(
int
i
=
0
;
i
8
;
i
++
)
{
for
(
int
j
=
0
;
j
8
;
j
++
)
{
if
((
i
==
0
||
i
==
7
)
&&
(
j
==
0
||
j
==
7
))
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
5
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
5
;
}
}
else
if
(
i
==
0
||
i
==
7
||
j
==
0
||
j
==
7
)
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
2
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
2
;
}
}
else
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
1
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
1
;
}
}
}
}
break
;
case
5
:
case
6
:
for
(
int
i
=
0
;
i
8
;
i
++
)
{
for
(
int
j
=
0
;
j
8
;
j
++
)
{
if
((
i
==
0
||
i
==
7
)
&&
(
j
==
0
||
j
==
7
))
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
5
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
5
;
}
}
else
if
(
i
==
0
||
i
==
7
||
j
==
0
||
j
==
7
)
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
2
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
2
;
}
}
else
{
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
1
;
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
1
;
}
}
}
}
blackEvaluate
=
blackEvaluate
*
2
+
Rule
.
getLegalMoves
(
chessBoard
,
BLACK
).
size
();
whiteEvaluate
=
whiteEvaluate
*
2
+
Rule
.
getLegalMoves
(
chessBoard
,
WHITE
).
size
();
break
;
case
7
:
case
8
:
/**
* 稳定度
*/
for
(
int
i
=
0
;
i
9
;
i
++
)
{
for
(
int
j
=
0
;
j
9
;
j
++
)
{
int
weight
[]
=
new
int
[]
{
2
,
4
,
6
,
10
,
15
};
if
(
chessBoard
[
i
][
j
]
==
WHITE
)
{
whiteEvaluate
+=
weight
[
getStabilizationDegree
(
chessBoard
,
new
Move
(
i
,
j
))];
}
else
if
(
chessBoard
[
i
][
j
]
==
BLACK
)
{
blackEvaluate
+=
weight
[
getStabilizationDegree
(
chessBoard
,
new
Move
(
i
,
j
))];
}
}
}
/**
* 行动力
*/
blackEvaluate
+=
Rule
.
getLegalMoves
(
chessBoard
,
BLACK
).
size
();
whiteEvaluate
+=
Rule
.
getLegalMoves
(
chessBoard
,
WHITE
).
size
();
break
;
}
return
blackEvaluate
-
whiteEvaluate
;
}
稳定度计算
我们知道,在黑白棋中,棋盘四角的位置一旦占据是不可能再被翻转的,因此这几个位置上的子必然是稳定子,而边上的子只有可能沿边的方向被翻转,稳定的程度高于中间的位置上的子。
因此,试图给每个子定义一个稳定度,描述该子不被翻转的稳定程度。
一共有四个方向,即左-右,上-下,左上-右下,右上-左下。举个例子,下面代码中的 (drow[0][0], dcol[0][0])表示向左移动一个单位的向量,(drow[0][1], dcol[0][1])表示向右移动一个单位的向量。
对于棋盘中某个子的位置,向左找到第一个不是该颜色的位置(可以是出界),再向右找到第一个不是该颜色的位置(可以是出界),如果这两个位置至少有一个出界,或者两个均为敌方棋子,稳定度加1。
对于另外三个方向作同样操作。可以看到,角上的棋子的稳定度必然为4,其他位置则根据具体情况并不恒定不变。
private
static
int
getStabilizationDegree
(
byte
[][]
chessBoard
,
Move
move
)
{
int
chessColor
=
chessBoard
[
move
.
row
][
move
.
col
];
int
drow
[][],
dcol
[][];
int
row
[]
=
new
int
[
2
],
col
[]
=
new
int
[
2
];
int
degree
=
0
;
drow
=
new
int
[][]
{
{
0
,
0
},
{
-
1
,
1
},
{
-
1
,
1
},
{
1
,
-
1
}
};
dcol
=
new
int
[][]
{
{
-
1
,
1
},
{
0
,
0
},
{
-
1
,
1
},
{
-
1
,
1
}