It depends on your evaluation function; in your case, assuming the goal is to have more balls on the board than your opponent, the Player would be maximizing & the AI would be minimizing.
Java minimax abalone implementation
سؤال
I want to implement Minimax in my Abalone game but I don't know how to do it. To be exact I don't know when the algo need to max or min the player. If I have understand the logic, I need to min the player and max the AI ?
This is the wikipedia pseudo code
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := -∞
for each child of node
val := minimax(child, depth - 1, FALSE))
bestValue := max(bestValue, val);
return bestValue
else
bestValue := +∞
for each child of node
val := minimax(child, depth - 1, TRUE))
bestValue := min(bestValue, val);
return bestValue
(* Initial call for maximizing player *)
minimax(origin, depth, TRUE)
And my implementation
private Integer minimax(Board board, Integer depth, Color current, Boolean maximizingPlayer) {
Integer bestValue;
if (0 == depth)
return ((current == selfColor) ? 1 : -1) * this.evaluateBoard(board, current);
Integer val;
if (maximizingPlayer) {
bestValue = -INF;
for (Move m : board.getPossibleMoves(current)) {
board.apply(m);
val = minimax(board, depth - 1, current, Boolean.FALSE);
bestValue = Math.max(bestValue, val);
board.revert(m);
}
return bestValue;
} else {
bestValue = INF;
for (Move m : board.getPossibleMoves(current)) {
board.apply(m);
val = minimax(board, depth - 1, current, Boolean.TRUE);
bestValue = Math.min(bestValue, val);
board.revert(m);
}
return bestValue;
}
}
And my evaluate function
private Integer evaluateBoard(Board board, Color player) {
return board.ballsCount(player) - board.ballsCount(player.other());
}
المحلول
نصائح أخرى
The usual method in 2 player games is to always maximize, but negate the value as it is passed up to the parent.
Your evaluation function is not very usefull for minimax search as it would be constant for most moves of the game. The moves in Abalone are a lot less dramatic than Chess. Try using the sum of distances between all the player's marbles. This function gives minimax something to work with.
You also need to ensure that selfColor is the colour of the player to move when you make the initial call to minimax.
The recursion end could also be written
if (0 == depth)
return this.evaluateBoard(board, selfColor);
Out of scope of the question, but could be relevant to you: I find negamax easier to work with.