const chess = new Chess();

        let difficulty = 2;
        
        let selectedPiece = null;
        let botDepth = 3; // Default medium difficulty
        let isThinking = false;

        

        // Piece values for bot evaluation
        const pieceValues = {
            'p': 100,
            'n': 320,
            'b': 330,
            'r': 500,
            'q': 900,
            'k': 20000
        };

        // Position weights for piece-square tables
        const pawnTable = [
            0,  0,  0,  0,  0,  0,  0,  0,
            50, 50, 50, 50, 50, 50, 50, 50,
            10, 10, 20, 30, 30, 20, 10, 10,
            5,  5, 10, 45, 45, 10,  5,  5,
            0,  0,  0, 20, 20,  0,  0,  0,
            5, -5,-10,  0,  0,-10, -5,  5,
            5, 10, 10,-20,-20, 10, 10,  5,
            100,  0,  0,  0,  0,  0,  0,  0
        ];

        const knightTable = [
            -50,-40,-30,-30,-30,-30,-40,-50,
            -40,-20,  0,  0,  0,  0,-20,-40,
            -30,  0, 10, 35, 35, 10,  0,-30,
            -30,  5, 15, 20, 20, 15,  5,-30,
            -30,  0, 15, 20, 20, 15,  0,-30,
            -30,  5, 10, 35, 35, 10,  5,-30,
            -40,-20,  0,  5,  5,  0,-20,-40,
            -50,-40,-30,-30,-30,-30,-40,-50
        ];

        // Piece Unicode symbols
        const pieces = {
            'wP': '♙', 'wR': '♖', 'wN': '♘', 'wB': '♗', 'wQ': '♕', 'wK': '♔',
            'bP': '♟', 'bR': '♜', 'bN': '♞', 'bB': '♝', 'bQ': '♛', 'bK': '♚'
        };

        

        function evaluatePosition() {
            let score = 0;
            const position = chess.board();
            

            for (let i = 0; i < 8; i++) {
                for (let j = 0; j < 8; j++) {
                    const piece = position[i][j];
                    if (piece) {
                        const value = pieceValues[piece.type];
                        const positionBonus = piece.type === 'p' ? pawnTable[i * 8 + j] :
                                            piece.type === 'n' ? knightTable[i * 8 + j] : 0;
                        
                        score += (piece.color === 'w' ? 1 : -1) * (value + positionBonus);
                    }
                }
            }
            return score;
            
        }

        function minimax(depth, alpha, beta, maximizingPlayer) {
            if (depth === 0) return evaluatePosition();
            
            const moves = chess.moves();
            if (moves.length === 0) return chess.in_checkmate() ? -Infinity : 0;

            if (maximizingPlayer) {
                let maxEval = -Infinity;
                for (const move of moves) {
                    chess.move(move);
                    const eval = minimax(depth - 1, alpha, beta, false);
                    chess.undo();
                    maxEval = Math.max(maxEval, eval);
                    alpha = Math.max(alpha, eval);
                    if (beta <= alpha) break;
                }
                return maxEval;
            } else {
                let minEval = Infinity;
                for (const move of moves) {
                    chess.move(move);
                    const eval = minimax(depth - 1, alpha, beta, true);
                    chess.undo();
                    minEval = Math.min(minEval, eval);
                    beta = Math.min(beta, eval);
                    if (beta <= alpha) break;
                }
                return minEval;
            }
            
            
            
        }

        async function makeBotMove() {


            if (chess.game_over()) return;
            
            document.getElementById('thinking').style.display = 'block';
            isThinking = true;
        
            // Add a small delay to show the thinking message
            await new Promise(resolve => setTimeout(resolve, 500));
        
            const moves = chess.moves();
            let bestMove = null;
            let bestEval = Infinity;
        
            for (const move of moves) {
                // Make Black's move
                chess.move(move);
        
                let worstEvalAfterWhite = -Infinity; // Track the worst outcome for Black after White’s best response
        
                // Generate all possible responses by White
                const whiteResponses = chess.moves();
        
                for (const response of whiteResponses) {
                    // Make White's response
                    chess.move(response);
        
                    // Evaluate the resulting position after White’s response
                    const eval = minimax(botDepth - 3, Infinity, -Infinity, false);
        
                    // Undo White's response
                    chess.undo();
        
                    // Track the worst evaluation White can enforce after Black's move
                    worstEvalAfterWhite = Math.max(worstEvalAfterWhite, eval);
                }
        
                // Undo Black's move
                chess.undo();
        
                // Check if this move by Black minimizes the best response by White
                if (worstEvalAfterWhite < bestEval) {
                    bestEval = worstEvalAfterWhite;
                    bestMove = move;
                }
        
                console.log(`Evaluated move: ${move}, Evaluation after White's best response: ${worstEvalAfterWhite}`);
            }
        
            if (bestMove) {
                // Make the best move found
                chess.move(bestMove);
                updateBoard();
                document.getElementById('status-message').textContent = "Your turn (White)";
            }
        
            document.getElementById('thinking').style.display = 'none';
            isThinking = false;
        
            if (chess.game_over()) {
                handleGameOver();
            }

            
        
            
        }
        

        function handleGameOver() {
            let message = "Game Over - ";
            if (chess.in_checkmate()) {
                message += chess.turn() === 'w' ? "Black wins!" : "White wins!";
            } else if (chess.in_draw()) {
                message += "Draw!";
            } else if (chess.in_stalemate()) {
                message += "Stalemate!";
            }
            document.getElementById('status-message').textContent = message;
        }

        ---

can you improve this algorithm and make it so that if the player can respond to the ai by checkmating it then it will highly discourage that move