【发布时间】:2013-11-25 15:33:53
【问题描述】:
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(有或没有修剪)绝不可能避免这种情况。唯一能保证最优的移动是那些在树的底部“可见”时的移动,通常是在游戏结束时。