我正在研究象棋引擎,現在試圖實現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')。 –