So I got a small board game for my Othello game. In this game the AI should decide what to move to make with a Alpha Beta Prune search algorithm. I used the following Pseudocode form geeksforgeeks:
function minimax(node, depth, isMaximizingPlayer, alpha, beta):
if node is a leaf node :
return value of the node
if isMaximizingPlayer :
bestVal = -INFINITY
for each child node :
value = minimax(node, depth+1, false, alpha, beta)
bestVal = max( bestVal, value)
alpha = max( alpha, bestVal)
if beta <= alpha:
break
return bestVal
else :
bestVal = +INFINITY
for each child node :
value = minimax(node, depth+1, true, alpha, beta)
bestVal = min( bestVal, value)
beta = min( beta, bestVal)
if beta <= alpha:
break
return bestVal
This is how I implemented it:
//Called when it's the AI's turn to make a move.
public Board makeMove(Board board) {
setRoot(board);
int alpha = Integer.MIN_VALUE;
int beta = Integer.MAX_VALUE;
int val = alphaBetaSearch(tree.getRoot(), 0, true, alpha, beta);
Board resultBoard = //Should be AI Board/move
setRoot(resultBoard);
return resultBoard;
}
private int alphaBetaSearch(Node node, int depth, boolean maximizing, int alpha, int beta) {
currentDepth = depth;
if (node.isLeaf()) {
return evaluateUtility(node.getBoard());
}
if (maximizing) {
int bestValue = Integer.MIN_VALUE;
for (Node child : node.getChildren()) {
int value = alphaBetaSearch(child, depth + 1, false, alpha, beta);
bestValue = Integer.max(bestValue, value);
alpha = Integer.max(alpha, bestValue);
if (beta <= alpha) {
break;
}
return alpha;
}
} else {
int bestValue = Integer.MAX_VALUE;
for (Node child : node.getChildren()) {
int value = alphaBetaSearch(child, depth + 1, true, alpha, beta);
bestValue = Integer.min(bestValue, value);
beta = Integer.min(beta, bestValue);
if (beta <= alpha) {
break;
}
return beta;
}
}
return 0; //Not sure what should be returned here
}
private int evaluateUtility(Board board) {
int whitePieces = board.getNumberOfWhiteCells();
int blackPieces = board.getNumberOfBlackCells();
int sum = (blackPieces - whitePieces);
return sum;
}
As my Board is quite small (4x4) I have been able to calculate the complete search tree before the game starts in about 20 seconds. This should improve my search as I don't have build anything when playing. Each Node in my tree contains a Board which themselves have a 2D array of cells. The root node/board looks like this:
EMPTY EMPTY EMPTY EMPTY
EMPTY WHITE BLACK EMPTY
EMPTY BLACK WHITE EMPTY
EMPTY EMPTY EMPTY EMPTY
Now when I start the game, this is the starting board and I call the AI to make a move. When the minimax call has executed, it returns the value 2 at the depth of 12. Depth 12 is a leaf node/board in my tree. After running it with the debugger it seems that my implementation doesn't traverse back up the tree. All it does is going down to the leftmost tree and returns it's evaluation.