I had to save the board state for each move made when Minimax was called and analyze them individually. This allowed me to track the moves and notice that the board state was not being updated correctly. I’ve now resolved the issue. The problem was related to how I was passing my board state (piecesPos). I was retrieving and passing the wrong board state, which caused Minimax to make incorrect or suboptimal moves. Thank you all for your contributions; it is greatly appreciated.
Renaming to piecesPosCopy and using piecesPos
This was getting the actual board state to use when min max is called.
int minMax(List<String> piecesPosCopy, int depth, bool isMaximizing, int alpha, int beta) {
// Base case: if depth is 0 or the game is over, return the evaluation
if (depth == 0 || isGameOver(piecesPos)) {
return evaluateBoard(piecesPos);
}
if (isMaximizing) {
int maxEval = -9999; // Initialize to a very low value
for (int i = 0; i < piecesPos.length; i++) {
if (piecesPos[i][0] == "B" || piecesPos[i][0] == "O") {
List<int> possibleMoves = getPossibleMoves(piecesPos, i);
for (int move in possibleMoves) {
// Save the current state
List<String> saveState = List.from(piecesPos);
// Make the move
performMultitakeAnim = false;
makeMove(piecesPos, i, move);
// Recursive call
int eval = minMax(piecesPos, depth - 1, false, alpha, beta);
// Restore the state
piecesPos = List.from(saveState);
// Update maxEval
maxEval = max(maxEval, eval);
alpha = max(alpha, eval);
// Alpha-Beta Pruning
if (beta <= alpha) {
break;
}
}
}
}
return maxEval;
} else {
int minEval = 9999; // Initialize to a very high value
for (int i = 0; i < piecesPos.length; i++) {
if (piecesPos[i][0] == "W" || piecesPos[i][0] == "Q") {
List<int> possibleMoves = getPossibleMoves(piecesPos, i);
for (int move in possibleMoves) {
// Save the current state
List<String> saveState = List.from(piecesPos);
// Make the move
performMultitakeAnim = false;
makeMove(piecesPos, i, move);
// Recursive call
int eval = minMax(piecesPos, depth - 1, true, alpha, beta);
// Restore the state
piecesPos = List.from(saveState);
// Update minEval
minEval = min(minEval, eval);
beta = min(beta, eval);
// Alpha-Beta Pruning
if (beta <= alpha) {
break;
}
}
}
}
return minEval;
}
}