我正在研究象棋引擎,现在试图实现Minimax算法。目前我已经编写了一个mimimax代码,但它并不能正常工作。考虑到我不是一个好的棋手,我几分钟之内就击败了引擎。MinMax算法不能正常工作
我想有人善意地看看我的极小极大码,并告诉我我写的是正确的。
在此先感谢。
这里是我的代码:
private int MiniMax(Game game, int depth){
return Max(depth);
}
private int Max(int depth){
if (depth <= 0
|| this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
|| this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){
return EvaluatePieceScore();
}
int max = -Integer.MIN_VALUE;
List<Move> moves = generateMoves(false);
for(Move allMove : moves){
executeMove(allMove);
int score = -Mini(depth - 1);
undoMove(allMove);
if(score > max){
max = score;
}
}
return max;
}
private int Mini(int depth) {
if (depth <= 0
|| this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
|| this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){
return EvaluatePieceScore();
}
int min = Integer.MIN_VALUE;
List<Move> moves = generateMoves(false);
for(Move allMove : moves){
executeMove(allMove);
int score = -Max(depth - 1);
undoMove(allMove);
if(score > min){
min = score;
}
}
return min;
}
有一件事是-Integer.MIN_VALUE将无法正常工作。 – resueman
@resueman,我已经做出了改变,但没有改善 – mish
而你正在对抗哪个“深度”?如果数字很低,可以轻松取胜。在'MiniMax'方法中,你只需要调用'Max',这是可疑的(应该为其他播放器调用'Min')。 –