File size: 11,287 Bytes
8085056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* --- Chess Game Logic & AI --- */

// Game State
let game = new Chess();
let boardEl = document.getElementById('board');
let selectedSquare = null;
let aiDepth = 2; // Default Medium
let playerColor = 'w'; // Player is white
let gameOver = false;

// Piece Unicode Map
const pieces = {
    'p': 'β™Ÿ', 'n': 'β™ž', 'b': '♝', 'r': 'β™œ', 'q': 'β™›', 'k': 'β™š',
    'P': 'β™Ÿ', 'N': 'β™ž', 'B': '♝', 'R': 'β™œ', 'Q': 'β™›', 'K': 'β™š'
};

// Piece Values for AI
const pieceValues = {
    p: 10, n: 30, b: 30, r: 50, q: 90, k: 900,
    P: -10, N: -30, B: -30, R: -50, Q: -90, K: -900 // From Black perspective
};

// Initial Render
document.addEventListener('DOMContentLoaded', () => {
    renderBoard();
    updateStatus();
});

// --- Board Rendering ---

function renderBoard() {
    boardEl.innerHTML = '';
    const boardState = game.board();

    // Last move highlight
    const history = game.history({ verbose: true });
    const lastMove = history.length ? history[history.length - 1] : null;

    for (let i = 0; i < 8; i++) {
        for (let j = 0; j < 8; j++) {
            const squareDiv = document.createElement('div');
            const isLight = (i + j) % 2 === 0;
            const squareName = String.fromCharCode(97 + j) + (8 - i);
            const piece = boardState[i][j];

            squareDiv.className = `square ${isLight ? 'light' : 'dark'}`;
            squareDiv.dataset.square = squareName;
            squareDiv.onclick = () => handleSquareClick(squareName);

            // Highlight selected
            if (selectedSquare === squareName) {
                squareDiv.classList.add('selected');
            }

            // Highlight last move
            if (lastMove && (lastMove.from === squareName || lastMove.to === squareName)) {
                squareDiv.classList.add('last-move');
            }

            // Highlight King in Check
            if (piece && piece.type === 'k' && piece.color === game.turn() && game.in_check()) {
                squareDiv.classList.add('check');
            }

            // Highlight valid moves if a piece is selected
            if (selectedSquare) {
                const moves = game.moves({ square: selectedSquare, verbose: true });
                const move = moves.find(m => m.to === squareName);
                if (move) {
                    if (move.flags.includes('c') || move.flags.includes('e')) {
                        squareDiv.classList.add('valid-capture');
                    } else {
                        squareDiv.classList.add('valid-move');
                    }
                }
            }

            // Render Piece
            if (piece) {
                const pieceSpan = document.createElement('span');
                pieceSpan.className = `piece ${piece.color === 'w' ? 'white' : 'black'}`;
                // Using standard chess unicode
                pieceSpan.textContent = pieces[piece.type]; 
                // Note: Standard unicode usually comes in outline/filled variants. 
                // For simplicity in this constrained env, we use text color to differentiate.
                if(piece.color === 'w') pieceSpan.textContent = pieces[piece.type.toUpperCase()];
                else pieceSpan.textContent = pieces[piece.type];
                
                squareDiv.appendChild(pieceSpan);
            }

            boardEl.appendChild(squareDiv);
        }
    }
}

// --- Interaction ---

function handleSquareClick(square) {
    if (gameOver || game.turn() !== playerColor) return;

    const piece = game.get(square);

    // Select a piece
    if (piece && piece.color === playerColor) {
        selectedSquare = square;
        playSound('move');
        renderBoard();
        return;
    }

    // Move piece
    if (selectedSquare) {
        const moves = game.moves({ square: selectedSquare, verbose: true });
        const move = moves.find(m => m.to === square);

        if (move) {
            game.move(move.san);
            selectedSquare = null;
            renderBoard();
            updateStatus();
            updateHistory();
            
            // Trigger AI after short delay
            if (!game.game_over()) {
                setTimeout(makeAIMove, 250);
            }
        } else {
            // Deselect if clicking invalid empty square
            if(!piece) {
                selectedSquare = null;
                renderBoard();
            }
        }
    }
}

// --- AI Engine (Minimax) ---

function makeAIMove() {
    if (game.game_over()) return;

    updateStatus("AI is thinking...");

    // Use timeout to allow UI update before heavy calculation
    setTimeout(() => {
        const bestMove = calculateBestMove(game, aiDepth);
        game.move(bestMove);
        renderBoard();
        updateStatus();
        updateHistory();
        playSound('move');
    }, 100);
}

function calculateBestMove(gameInstance, depth) {
    const possibleMoves = gameInstance.moves();
    
    if (possibleMoves.length === 0) return null;

    // Alpha-Beta Pruning
    let bestMove = -Infinity;
    let bestMoveFound = null;

    // Shuffle moves to add randomness to equal positions
    possibleMoves.sort(() => Math.random() - 0.5);

    for (const move of possibleMoves) {
        gameInstance.move(move);
        const value = minimax(gameInstance, depth - 1, -10000, 10000, false);
        gameInstance.undo();

        if (value >= bestMove) {
            bestMove = value;
            bestMoveFound = move;
        }
    }

    return bestMoveFound || possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
}

function minimax(gameInstance, depth, alpha, beta, isMaximizingPlayer) {
    if (depth === 0) {
        return -evaluateBoard(gameInstance.board());
    }

    const possibleMoves = gameInstance.moves();

    if (possibleMoves.length === 0) {
        if (gameInstance.in_checkmate()) return isMaximizingPlayer ? -10000 : 10000; // Depth * value
        return 0; // Stalemate
    }

    if (isMaximizingPlayer) {
        let bestMove = -9999;
        for (const move of possibleMoves) {
            gameInstance.move(move);
            bestMove = Math.max(bestMove, minimax(gameInstance, depth - 1, alpha, beta, !isMaximizingPlayer));
            gameInstance.undo();
            alpha = Math.max(alpha, bestMove);
            if (beta <= alpha) return bestMove;
        }
        return bestMove;
    } else {
        let bestMove = 9999;
        for (const move of possibleMoves) {
            gameInstance.move(move);
            bestMove = Math.min(bestMove, minimax(gameInstance, depth - 1, alpha, beta, !isMaximizingPlayer));
            gameInstance.undo();
            beta = Math.min(beta, bestMove);
            if (beta <= alpha) return bestMove;
        }
        return bestMove;
    }
}

function evaluateBoard(board) {
    let totalEvaluation = 0;
    for (let i = 0; i < 8; i++) {
        for (let j = 0; j < 8; j++) {
            totalEvaluation += getPieceValue(board[i][j]);
        }
    }
    return totalEvaluation;
}

function getPieceValue(piece) {
    if (piece === null) return 0;
    // Simple logic: AI is Black, wants to minimize positive score (which favors white)
    // We want AI (Black) to maximize its own value.
    // Let's standardise: Positive = Good for Black, Negative = Good for White
    
    // Re-map for standard Minimax
    // If I am playing as Black (AI), I want positive score.
    let value = 0;
    if (piece.color === 'b') {
        value = pieceValues[piece.type];
    } else {
        value = pieceValues[piece.type.toUpperCase()]; // Negative values
    }
    
    // Add position bonuses (Simplified: center control)
    const centerBonus = (i, j) => {
        if (i >= 3 && i <= 4 && j >= 3 && j <= 4) return 1; 
        return 0;
    }

    // Adjust based on simple position
    if(piece.color === 'b') return value + (piece.type === 'n' ? centerBonus(i,j) : 0);
    return value;
}

// --- UI Helpers ---

function updateStatus(msg = null) {
    const statusEl = document.getElementById('game-status');
    
    if (msg) {
        statusEl.textContent = msg;
        return;
    }

    let status = '';
    const turn = game.turn() === 'w' ? 'White' : 'Black';

    if (game.in_checkmate()) {
        status = `Game Over: ${turn === 'White' ? 'Black' : 'White'} wins by checkmate!`;
        gameOver = true;
        playSound('capture');
    } else if (game.in_draw()) {
        status = 'Game Over: Draw!';
        gameOver = true;
    } else {
        status = `${turn} to move`;
        if (game.in_check()) status += ' (Check!)';
    }
    statusEl.textContent = status;
}

function updateHistory() {
    const historyEl = document.getElementById('move-history');
    const history = game.history();
    let html = '';
    
    for (let i = 0; i < history.length; i += 2) {
        const moveNum = Math.floor(i / 2) + 1;
        html += `<div class="flex gap-2 text-xs">
                    <span class="text-gray-500">${moveNum}.</span>
                    <span class="text-gray-200">${history[i]}</span>
                    ${history[i+1] ? `<span class="text-gray-200">${history[i+1]}</span>` : '<span class="opacity-20">...</span>'}
                 </div>`;
    }
    historyEl.innerHTML = html;
    historyEl.scrollTop = historyEl.scrollHeight;
}

function resetGame() {
    game.reset();
    gameOver = false;
    selectedSquare = null;
    updateStatus();
    renderBoard();
    document.getElementById('move-history').innerHTML = '<p class="text-center italic opacity-50">Game started...</p>';
    playSound('move');
}

function setDifficulty(level) {
    aiDepth = level;
    document.querySelectorAll('.diff-btn').forEach(btn => {
        btn.classList.remove('bg-primary', 'text-white', 'active');
        btn.classList.add('text-gray-400');
    });
    
    // Simple active state logic based on click text
    const buttons = document.querySelectorAll('.diff-btn');
    const index = level - 1;
    buttons[index].classList.add('bg-primary', 'text-white', 'active');
    buttons[index].classList.remove('text-gray-400');
    
    resetGame();
}

// Simple Sound Synth (Beeps)
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playSound(type) {
    if (audioCtx.state === 'suspended') audioCtx.resume();
    
    const osc = audioCtx.createOscillator();
    const gainNode = audioCtx.createGain();
    
    osc.connect(gainNode);
    gainNode.connect(audioCtx.destination);
    
    if (type === 'move') {
        osc.type = 'sine';
        osc.frequency.setValueAtTime(300, audioCtx.currentTime);
        osc.frequency.exponentialRampToValueAtTime(100, audioCtx.currentTime + 0.1);
        gainNode.gain.setValueAtTime(0.1, audioCtx.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.1);
        osc.start();
        osc.stop(audioCtx.currentTime + 0.1);
    } else if (type === 'capture') {
        osc.type = 'triangle';
        osc.frequency.setValueAtTime(150, audioCtx.currentTime);
        gainNode.gain.setValueAtTime(0.2, audioCtx.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.2);
        osc.start();
        osc.stop(audioCtx.currentTime + 0.2);
    }
}