there is a different version of XO game player vs ai that both can move anything they want in each round (for example both can play X in a round). so i use minimax algorithm but i dont this algorithm i wrote is correct or not


int minimax(bool isMax, int depth) {
    int score = evaluate();


    if (score == 20 || score == -10) // Immediately return for a win/loss.
        return score;


    if (isBoardFull())
        return 0; // Tie game.


    if (isMax) {
        int best = -1000;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j].first == ' ') {
                    // Make the move
                    board[i][j].first = aiMark.first;
                    // Compute evaluation function for this move.
                    best = max(best, minimax(!isMax, depth + 10));
                    // Undo the move
                    board[i][j].first = ' ';
                }
            }
        }
        return best - depth;
    } else {
        int best = 1000;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j].first == ' ') {
                    // Make the move
                    board[i][j].first = playerMark.first;
                    // Compute evaluation function for this move.
                    best = min(best, minimax(!isMax, depth + 10));
                    // Undo the move
                    board[i][j].first = ' ';
                }
            }
        }


        return best + depth;
    }


}

board[i][j].first mean that the mark in (i,j)

i tried to debug it but still i dont know is this right for this version of game or not.

New contributor

yazdan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.