我在国际象棋程序中实现一个简单的 NegaMax 时遇到问题。
根据几个网站,negamax 在我的代码中应该如下所示:
int Position::negaMax(int curr_depth, int depth) {
cd = curr_depth-1;
if (curr_depth==depth) return evaluate();
int max = -500000;
calc_moves(true);
doBackup(cd);
for (int i=0;i<mvSize[cd];i++) {
move_figure(mvD[cd][i][0],mvD[cd][i][1],mvD[cd][i][2],mvI[cd][i][0],mvI[cd][i][1]);
int score = -negaMax(curr_depth+1,depth);
cd--; undoMove(cd);
if (curr_depth==1)
cout << "Move: " << getMoveString(i) << ", Score: " << score << endl;
if (score>max)
max=score;
}
return max;
}
但是有了这段代码,我得到了这个输出:
Move: a2a3, Score: 0
Move: a2a4, Score: 0
Move: b2b3, Score: 0
Move: b2b4, Score: 0
Move: c2c3, Score: 0
Move: c2c4, Score: 0
Move: d2d3, Score: 0
Move: d2d4, Score: 0
Move: e2e3, Score: 0
Move: e2e4, Score: 0
Move: f2f3, Score: 0
Move: f2f4, Score: 0
Move: g2g3, Score: 0
Move: g2g4, Score: 0
Move: h2h3, Score: 0
Move: h2h4, Score: 0
Move: b1a3, Score: 0
Move: b1c3, Score: 0
Move: g1h3, Score: 0
Move: g1f3, Score: 0
score: 0
如果我从起始位置对 ply3 进行 negaMax,那是不对的。
如果我删除递归函数调用前面的减号,我会得到更好的结果。但在我看来这是不对的,因为在上面的代码中没有减号,我只会最大化一个玩家的分数,而不是两个玩家的分数。
Move: a2a3, Score: 0
Move: a2a4, Score: 30
Move: b2b3, Score: 0
Move: b2b4, Score: 30
Move: c2c3, Score: 0
Move: c2c4, Score: 30
Move: d2d3, Score: 295
Move: d2d4, Score: 295
Move: e2e3, Score: 295
Move: e2e4, Score: 295
Move: f2f3, Score: 0
Move: f2f4, Score: 30
Move: g2g3, Score: 0
Move: g2g4, Score: 30
Move: h2h3, Score: 0
Move: h2h4, Score: 30
Move: b1a3, Score: 30
Move: b1c3, Score: 30
Move: g1h3, Score: 30
Move: g1f3, Score: 30
score: 295
我尝试实现不同版本的 MinMax、NegaMax 和 AlphaBeta。但我总是得到 0 分。我会非常感谢任何提示。