0
public class OthelloJPlayer extends OthelloPlayer {
@Override
public OthelloMove getMove(OthelloState state) {
int bestchoice = 0;
int bestscore = Integer.MIN_VALUE;
boolean maximizingPlayer = true;
// generate the list of moves:
List<OthelloMove> moves = state.generateMoves();
if (moves.isEmpty()) {
// If there are no possible moves, just return "pass":
return null;
} else {
// turn moves to states
List<OthelloState> states = new ArrayList<OthelloState>();
for (int i = 0; i < moves.size(); i++) {
states.add(state.applyMoveCloning(moves.get(i)));
}
for (int i = 0; i < states.size(); i++) {
// uses minmax to determine best move.
int score = (MinMax(3, states.get(i), maximizingPlayer));
if (score > bestscore) {
bestscore = score;
bestchoice = i;
}
}
}
return moves.get(bestchoice);
}
// min max algorithm
public int MinMax(int depth, OthelloState game_board, boolean maximizingPlayer) {
List<OthelloMove> moves;
if (depth == 0) {
int score = game_board.score();
return score;
}
if (maximizingPlayer) {
int bestvalue = Integer.MIN_VALUE;
// gets other players moves
moves = game_board.generateMoves(1);
if (moves.isEmpty()) {
int score = game_board.score();
return score;
} else {
for (int i = 0; i < moves.size(); i++) {
OthelloState new_game_board = new OthelloState(8);
new_game_board = game_board.applyMoveCloning(moves.get(i));
int returned_score = MinMax(depth - 1, new_game_board, false);
bestvalue = max(bestvalue, returned_score);
}
}
return bestvalue;
} else {
int bestvalue = Integer.MAX_VALUE;
// gets your moves
moves = game_board.generateMoves(0);
if (moves.isEmpty()) {
int score = game_board.score();
return score;
} else {
for (int i = 0; i < moves.size(); i++) {
OthelloState new_game_board = new OthelloState(8);
new_game_board = game_board.applyMoveCloning(moves.get(i));
int returned_score = MinMax(depth - 1, new_game_board, true);
bestvalue = min(bestvalue, returned_score);
}
}
return bestvalue;
}
}
}
我的minimax算法似乎没有返回最优的移动。当我的代理使用minimax代理玩对抗执行随机移动的代理时,它有时会失去。从我认识的一切看起来好的可以有人请检查我的逻辑我一定错过了一些东西。启发式就是得分。一个积极的分数意味着你赢得一个负分数意味着另一个玩家正在获胜。用于黑白棋的Minimax算法不能正常工作
看到这个问题也:http://stackoverflow.com/questions/9511814/minimax-algorithm-doesnt-return-best-move – Fernando
在游戏高达这一个分支因子,你可以通常只会在树中探索几个级别。因此可以做出不理想的举动。绝对不可以Minimax(有或没有修复)可以避免这种情况。保证最佳的唯一举动是当树的底部“可见”时,通常在游戏结束时。 –