[英]Minimax algorithm for Othello not working correctly
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;
}
}
}
My minimax algorithms does not appear to be returning the most optimal move. 我的minimax算法似乎并没有返回最佳移动。 When my agent that uses minimax agent plays against an agent that performs random moves sometimes it looses.
当我使用minimax代理的代理与执行随机移动的代理竞争时,有时会松动。 from my perceptive everything looks okay could someone please check my logic I must be missing something.
从我的洞察力来看一切都还好,有人可以检查一下我的逻辑我一定是缺少什么。 The heuristic is the score.
启发式是分数。 a positive score means you are winning a negative score means the other player is winning.
正分数表示您赢了负分数表示另一位玩家正在赢。
You have a number of issues. 您有很多问题。
Your getMove
method is really the root of the search, and it's a max node. 您的
getMove
方法实际上是搜索的根,并且是一个最大节点。 Therefore, it should invoke MinMax
with maximizingPlayer = false
. 因此,应使用
MinMax
maximizingPlayer = false
调用MinMax
。
When you invoke MinMax
, you need to swap players. 调用
MinMax
,需要交换播放器。 Right now, you just go from max -> max -> min -> min -> min... since you use true
and false
constants. 现在,您只需从max-> max-> min-> min-> min ...开始,因为您使用的是
true
和false
常量。 Change your invocation (for both the min and max cases) to MinMax(depth - 1, new_game_board, !maximizingPlayer)
. 将您的调用(针对最小和最大情况)更改为
MinMax(depth - 1, new_game_board, !maximizingPlayer)
。
Make sure that game_board.score()
gives an evaluation from the perspective of the max player. 确保
game_board.score()
从最大玩家的角度进行评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.