【发布时间】:2012-07-06 15:29:48
【问题描述】:
我现在为我糟糕的英语道歉,我是意大利人。
我正在用 C# 编写一个国际象棋游戏的完整实现,Player vs Player 和 Player vs Computer,现在我在实现 NegaMax 算法时遇到了一些困难。 有兴趣的可以看看我的github仓库:https://github.com/crybot/ChessEngine_NOGUI/tree/master/Chess%20Engine%20-%20NOGUI
我已经尝试将这个项目尽可能的面向对象,所以,如果你认为我的设计不正确,请告诉我:)
这是我的问题:
我实现了一个非常简单的评估函数,它对所有玩家对称地工作:
public static float Evaluate(PieceColor playerColor, Game game)
{
float score = 0;
score += 1 * (game.board.GetNumberOfPieces(PieceType.Pawn, playerColor)
- game.board.GetNumberOfPieces(PieceType.Pawn, playerColor.GetOpposite()));
score += 3 * (game.board.GetNumberOfPieces(PieceType.Bishop, playerColor)
- game.board.GetNumberOfPieces(PieceType.Bishop, playerColor.GetOpposite()));
score += 3 * (game.board.GetNumberOfPieces(PieceType.Knight, playerColor)
- game.board.GetNumberOfPieces(PieceType.Knight, playerColor.GetOpposite()));
score += 5 * (game.board.GetNumberOfPieces(PieceType.Rook, playerColor)
- game.board.GetNumberOfPieces(PieceType.Rook, playerColor.GetOpposite()));
score += 9 * (game.board.GetNumberOfPieces(PieceType.Queen, playerColor)
- game.board.GetNumberOfPieces(PieceType.Queen, playerColor.GetOpposite()));
score += 0.1f * (game.GetPlayer(playerColor).GetMoves(game).Count);
return score;
}
这是我的 NegaMax 函数,它接受一个游戏参数(包括棋盘、玩家、ecc.ecc。)、由枚举提供的 playerColor 和深度搜索;
我首先扫描了 EnginePlayer 的所有可能动作,然后我从 NegaMax 函数中得到了他们的分数,但有些东西不起作用......
private float NegaMax(Game game, PieceColor playerColor, int depth)
{
if (depth == 0)
return EvaluateMove(playerColor, game);
float max = float.MinValue;
float score;
foreach (Move move in game.GetPlayer(playerColor.GetOpposite()).GetMoves(game))
{
game.board.SimulateMove(move);
score = Math.Max(max, -NegaMax(game, playerColor.GetOpposite(), depth - 1));
game.board.CancelMove(move);
if (score > max)
max = score;
}
return max;
}
public Move ThinkMove(Game game, PieceColor playerColor, int depth)
{
Move bestMove = new NullMove();
float bestScore = float.MinValue;
float temp = 0;
foreach (Move move in GetMoves(game))
{
game.board.SimulateMove(move);
temp = NegaMax(game, playerColor, depth);
game.board.CancelMove(move);
if (temp > bestScore)
{
if (Judge.IsLegal(move, game))
{
bestMove = move;
bestScore = temp;
}
}
}
if (bestMove is NullMove)
throw new NotImplementedException();
return bestMove;
}
我想这就是全部......我希望你会发现我做错了什么:) 谢谢。
编辑: 我实现了一个 Perft,它显示了移动生成器的不正确性。它帮助找到了一些相对容易找到的错误,但最后一些结果仍然是错误的。 结果如下:
Perft Depth: 1
Nodes: 20 Captures: 0 Checks: 0 Castles: 0 Mates: 0 EnPassant: 0
Perft Depth: 2
Nodes: 400 Captures: 0 Checks: 0 Castles: 0 Mates: 0 EnPassant: 0
Perft Depth: 3
Nodes: 8902 Captures: 34 Checks: 12 Castles: 0 Mates: 0 EnPassant: 0
Perft Depth: 4
Nodes: 197281 Captures: 1610 Checks: 473 Castles: 0 Mates: 8 EnPassant: 0
这是正确的结果:https://chessprogramming.wikispaces.com/Perft+Results
如您所见,在深度 4,我得到了正确的节点分析,但不正确的捕获和检查值(而配合是正确的)。
多亏了这些结果,我试图通过在深度 4 处划分每次移动分析的节点来隔离错误,得到了这些结果:
Move Nodes
a2a3 8457
a2a4 9329
b2b3 9345
b2b4 9332
c2c3 9272
c2c4 9744
d2d3 11959
d2d4 12435
e2e3 13134
e2e4 13160
f2f3 8457
f2f4 8929
g2g3 9345
g2g4 9328
h2h3 8457
h2h4 9329
b1a3 8885
b1c3 9755
g1f3 9748
g1h3 8881
Total Nodes: 197281
并与这些结果进行比较,从 Sharper 获得:
Sharper v0.17 by Albert Bertilsson
divide 4
b1c3 9755
b1a3 8885
g1h3 8881
g1f3 9748
a2a3 8457
a2a4 9329
b2b3 9345
b2b4 9332
c2c3 9272
c2c4 9744
d2d3 11959
d2d4 12435
e2e3 13134
e2e4 13160
f2f3 8457
f2f4 8929
g2g3 9345
g2g4 9328
h2h3 8457
h2h4 9329
Nodes: 197281
Moves: 20
但即使在这里值也是正确的... 所以我尝试了深度 5 perft 来获得这些结果:
Move Nodes
a2a3 181046
a2a4 217813
b2b3 215255
b2b4 216110
c2c3 222861
c2c4 240044
d2d3 328511
d2d4 361753
e2e3 402988
e2e4 405348
f2f3 178891
f2f4 198437
g2g3 217210
g2g4 214017
h2h3 181044
h2h4 218810
b1a3 198572
b1c3 234656
g1f3 233491
g1h3 198502
Total Nodes: 4865359
然后与Sharper的结果进行比较:
Sharper v0.17 by Albert Bertilsson
divide 5
b1c3 234656
b1a3 198572
g1h3 198502
g1f3 233491
a2a3 181046
a2a4 217832
b2b3 215255
b2b4 216145
c2c3 222861
c2c4 240082
d2d3 328511
d2d4 361790
e2e3 402988
e2e4 405385
f2f3 178889
f2f4 198473
g2g3 217210
g2g4 214048
h2h3 181044
h2h4 218829
Nodes: 4865609
Moves: 20
所以我意识到问题是由棋子的双推产生的动作引起的......但我无法真正找到错误...... 有人遇到过同样的问题吗? 或类似的东西,或者只是提示找到那个错误......
谢谢大家:)
【问题讨论】:
-
究竟是什么不工作?引擎不“智能”或根本不搜索?
-
起初它似乎工作,但随后它试图通过将他的主教从 g8 移动到 a3 来杀死它。深度 2 有时也会给出非法动作。
-
你测试你的移动生成器了吗?
-
我做到了,我努力在移动生成器和 Judge 类(它说明移动是否合法)上找到错误
-
如果您的逻辑产生非法动作,那么这与您的 negamax 方法本身无关,因为它只关心对动作进行评分。首先确保你的逻辑在有效移动中随机选择时下有效的国际象棋。
标签: c# algorithm chess minimax