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代理的代理与执行随机移动的代理交互时,它有时会松开。从我的洞察力来看,一切看起来都还好,有人能帮我检查一下我的逻辑吗?我一定是遗漏了什么。启发式就是得分。一个积极的分数意味着你赢了一个负数意味着另一个球员赢了。
发布于 2013-11-25 15:41:42
你有很多问题。
getMove方法实际上是搜索的根,它是一个最大节点。因此,它应该使用MinMax调用maximizingPlayer = false。MinMax时,您需要交换播放器。现在,你只要从最大-> max -> min -> min -> min.因为您使用true和false常量。将您的调用(对于最小和最大的情况)更改为MinMax(depth - 1, new_game_board, !maximizingPlayer)。game_board.score()从最大播放器的角度进行评估。https://stackoverflow.com/questions/20196935
复制相似问题