|
| 1 | +const cells = document.querySelectorAll('.cell'); |
| 2 | +const statusText = document.getElementById('status'); |
| 3 | +const resetButton = document.getElementById('reset'); |
| 4 | +const difficultySelect = document.getElementById('difficulty'); |
| 5 | + |
| 6 | +let currentPlayer = 'X'; |
| 7 | +let board = ["", "", "", "", "", "", "", "", ""]; |
| 8 | +let gameActive = true; |
| 9 | + |
| 10 | +const winConditions = [ |
| 11 | + [0,1,2], [3,4,5], [6,7,8], |
| 12 | + [0,3,6], [1,4,7], [2,5,8], |
| 13 | + [0,4,8], [2,4,6] |
| 14 | +]; |
| 15 | + |
| 16 | +function handleCellClick(e) { |
| 17 | + const index = e.target.getAttribute('data-index'); |
| 18 | + |
| 19 | + if (board[index] !== "" || !gameActive || currentPlayer !== 'X') return; |
| 20 | + |
| 21 | + makeMove(index, 'X'); |
| 22 | + |
| 23 | + if (gameActive) { |
| 24 | + setTimeout(() => { |
| 25 | + const botIndex = getBotMove(); |
| 26 | + makeMove(botIndex, 'O'); |
| 27 | + }, 500); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function makeMove(index, player) { |
| 32 | + board[index] = player; |
| 33 | + cells[index].textContent = player; |
| 34 | + |
| 35 | + if (checkWin(player)) { |
| 36 | + statusText.textContent = `${player} wins! 🎉`; |
| 37 | + gameActive = false; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (board.every(cell => cell !== "")) { |
| 42 | + statusText.textContent = "It's a draw! 🤝"; |
| 43 | + gameActive = false; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + currentPlayer = player === 'X' ? 'O' : 'X'; |
| 48 | + statusText.textContent = `Player ${currentPlayer}'s turn`; |
| 49 | +} |
| 50 | + |
| 51 | +function checkWin(player) { |
| 52 | + return winConditions.some(condition => |
| 53 | + condition.every(index => board[index] === player) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +function getBotMove() { |
| 58 | + const difficulty = difficultySelect.value; |
| 59 | + |
| 60 | + const emptyIndices = board.map((val, i) => val === "" ? i : null).filter(v => v !== null); |
| 61 | + |
| 62 | + if (difficulty === 'easy') { |
| 63 | + // Random move |
| 64 | + return emptyIndices[Math.floor(Math.random() * emptyIndices.length)]; |
| 65 | + } |
| 66 | + |
| 67 | + if (difficulty === 'medium') { |
| 68 | + // 50% chance minimax, 50% random |
| 69 | + if (Math.random() < 0.5) { |
| 70 | + return getBestMove(); |
| 71 | + } else { |
| 72 | + return emptyIndices[Math.floor(Math.random() * emptyIndices.length)]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Hard: Always use MiniMax |
| 77 | + return getBestMove(); |
| 78 | +} |
| 79 | + |
| 80 | +function getBestMove() { |
| 81 | + let bestScore = -Infinity; |
| 82 | + let move; |
| 83 | + |
| 84 | + board.forEach((cell, index) => { |
| 85 | + if (cell === "") { |
| 86 | + board[index] = 'O'; |
| 87 | + let score = minimax(board, 0, false); |
| 88 | + board[index] = ""; |
| 89 | + if (score > bestScore) { |
| 90 | + bestScore = score; |
| 91 | + move = index; |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + return move; |
| 97 | +} |
| 98 | + |
| 99 | +function minimax(newBoard, depth, isMaximizing) { |
| 100 | + if (checkWin('O')) return 10 - depth; |
| 101 | + if (checkWin('X')) return depth - 10; |
| 102 | + if (newBoard.every(cell => cell !== "")) return 0; |
| 103 | + |
| 104 | + if (isMaximizing) { |
| 105 | + let bestScore = -Infinity; |
| 106 | + newBoard.forEach((cell, index) => { |
| 107 | + if (cell === "") { |
| 108 | + newBoard[index] = 'O'; |
| 109 | + let score = minimax(newBoard, depth + 1, false); |
| 110 | + newBoard[index] = ""; |
| 111 | + bestScore = Math.max(score, bestScore); |
| 112 | + } |
| 113 | + }); |
| 114 | + return bestScore; |
| 115 | + } else { |
| 116 | + let bestScore = Infinity; |
| 117 | + newBoard.forEach((cell, index) => { |
| 118 | + if (cell === "") { |
| 119 | + newBoard[index] = 'X'; |
| 120 | + let score = minimax(newBoard, depth + 1, true); |
| 121 | + newBoard[index] = ""; |
| 122 | + bestScore = Math.min(score, bestScore); |
| 123 | + } |
| 124 | + }); |
| 125 | + return bestScore; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function resetGame() { |
| 130 | + board = ["", "", "", "", "", "", "", "", ""]; |
| 131 | + gameActive = true; |
| 132 | + currentPlayer = 'X'; |
| 133 | + statusText.textContent = "Player X's turn"; |
| 134 | + cells.forEach(cell => cell.textContent = ''); |
| 135 | +} |
| 136 | + |
| 137 | +cells.forEach(cell => cell.addEventListener('click', handleCellClick)); |
| 138 | +resetButton.addEventListener('click', resetGame); |
0 commit comments