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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tic Tac Toe</title> <style> body { display: flex; height: 100vh; justify-content: center; align-items: center; background: #282c34; color: white; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; flex-direction: column; } h1 { margin-bottom: 10px; } #game { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 10px; margin-bottom: 20px; } .cell { background: #3a3f47; display: flex; justify-content: center; align-items: center; font-size: 3rem; cursor: pointer; user-select: none; border-radius: 10px; transition: background 0.3s; } .cell:hover { background: #50575f; } #status { margin-bottom: 20px; font-size: 1.2rem; min-height: 1.5em; } button { padding: 10px 20px; font-size: 1rem; background: #61dafb; border: none; border-radius: 5px; cursor: pointer; color: #282c34; font-weight: bold; transition: background 0.3s; } button:hover { background: #21a1f1; } </style> </head> <body> <h1>Tic Tac Toe</h1> <div id="game"> <!-- 9 cells --> <div class="cell" data-index="0"></div> <div class="cell" data-index="1"></div> <div class="cell" data-index="2"></div> <div class="cell" data-index="3"></div> <div class="cell" data-index="4"></div> <div class="cell" data-index="5"></div> <div class="cell" data-index="6"></div> <div class="cell" data-index="7"></div> <div class="cell" data-index="8"></div> </div> <div id="status"></div> <button id="restartBtn">Restart Game</button> <script> const cells = document.querySelectorAll('.cell'); const statusDiv = document.getElementById('status'); const restartBtn = document.getElementById('restartBtn'); let board = ['', '', '', '', '', '', '', '', '']; let currentPlayer = 'X'; let isGameActive = true; const WINNING_COMBINATIONS = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; function handleCellClick(e) { const index = e.target.getAttribute('data-index'); if (board[index] !== '' || !isGameActive) { return; } updateCell(e.target, index); checkResult(); } function updateCell(cell, index) { board[index] = currentPlayer; cell.textContent = currentPlayer; } function changePlayer() { currentPlayer = currentPlayer === 'X' ? 'O' : 'X'; statusDiv.textContent = `It's ${currentPlayer}'s turn`; } function checkResult() { let roundWon = false; for (let i = 0; i < WINNING_COMBINATIONS.length; i++) { const [a, b, c] = WINNING_COMBINATIONS[i]; if (board[a] === '' || board[b] === '' || board[c] === '') { continue; } if (board[a] === board[b] && board[b] === board[c]) { roundWon = true; break; } } if (roundWon) { statusDiv.textContent = `Player ${currentPlayer} has won! 🎉`; isGameActive = false; return; } if (!board.includes('')) { statusDiv.textContent = `It's a draw! 🤝`; isGameActive = false; return; } changePlayer(); } function restartGame() { board = ['', '', '', '', '', '', '', '', '']; isGameActive = true; currentPlayer = 'X'; statusDiv.textContent = `It's ${currentPlayer}'s turn`; cells.forEach(cell => (cell.textContent = '')); } cells.forEach(cell => cell.addEventListener('click', handleCellClick)); restartBtn.addEventListener('click', restartGame); // Initialize status message statusDiv.textContent = `It's ${currentPlayer}'s turn`; </script> </body> </html> |