function minimax(board, AIPlayer, alpha, beta) {
    let score = evaluate(board);
    // Reached the end of a branch
    if (score !== 0) {
        return score;
    }
    // Draw, no moves left
    if (!board.includes(0)) {
        return 0;
    }

    // AI player
    if (AIPlayer) {
        let best = -1000;
        for (const square in board) {
            // If empty square, tries move and recurses
            if (board[square] === 0) {
                board[square] = "X";
                best = Math.max(best, minimax(board, false, alpha, beta));
                board[square] = 0;
                alpha = Math.max(alpha, best);
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    // Actual Player
    } else {
        let best = 1000;
        for (const square in board) {
            // If empty square, tries move and recurses
            if (board[square] === 0) {
                board[square] = "O";
                best = Math.min(best, minimax(board, true, alpha, beta));
                board[square] = 0;
                beta = Math.min(beta, best);
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    }
}
// Minimax algorithm with alpha beta pruning

Minimax algorithm with alpha beta pruning
Minimax algorithm with alpha beta pruning
function minimax(board, AIPlayer, alpha, beta) {
    let score = evaluate(board);
    if (score !== 0) {
        return score;
    }
    if (!board.includes(0)) {
        return 0;
    }
    if (AIPlayer) {
        let best = -1000;
        for (const square in board) {
            if (board[square] === 0) {
                board[square] = "X";
                best = Math.max(best, minimax(board, false, alpha, beta));
                board[square] = 0;
                alpha = Math.max(alpha, best);
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    } else {
        let best = 1000;
        for (const square in board) {
            if (board[square] === 0) {
                board[square] = "O";
                best = Math.min(best, minimax(board, true, alpha, beta));
                board[square] = 0;
                beta = Math.min(beta, best);
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    }
}
function minimax ( board , AIPlayer , alpha , beta ) { let score = evaluate ( board ) ; if ( score !== 0 ) { return score ; } if ( ! board . includes ( 0 ) ) { return 0 ; } if ( AIPlayer ) { let best = - 1000 ; for ( const square in board ) { if ( board [ square ] === 0 ) { board [ square ] = "X" ; best = Math . max ( best , minimax ( board , false , alpha , beta ) ) ; board [ square ] = 0 ; alpha = Math . max ( alpha , best ) ; if ( beta <= alpha ) { break ; } } } return best ; } else { let best = 1000 ; for ( const square in board ) { if ( board [ square ] === 0 ) { board [ square ] = "O" ; best = Math . min ( best , minimax ( board , true , alpha , beta ) ) ; board [ square ] = 0 ; beta = Math . min ( beta , best ) ; if ( beta <= alpha ) { break ; } } } return best ; } }
Minimax algorithm with alpha beta pruning
Minimax algorithm with alpha beta pruning
// Reached the end of a branch
 // Draw, no moves left
 // AI player
 // If empty square, tries move and recurses
 // Actual Player
 // If empty square, tries move and recurses
