code in a html a tic-tac-toe game
- User
code in a html a tic-tac-toe game
Assistant
Here's a simple Tic-Tac-Toe game implemented using HTML, CSS, and JavaScript. You can copy and paste this into an
.htmlfile and open it in your browser to play the game.
✅ Complete HTML Tic-Tac-Toe Game
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Tic Tac Toe</title> <style> body { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; background-color: #f0f0f0; } h1 { margin-bottom: 20px; } .board { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 5px; } .cell { width: 100px; height: 100px; background-color: white; display: flex; align-items: center; justify-content: center; font-size: 2em; cursor: pointer; border: 2px solid #444; user-select: none; } .cell:hover { background-color: #e0e0e0; } #status { margin-top: 20px; font-size: 1.2em; } button { margin-top: 10px; padding: 10px 20px; font-size: 1em; cursor: pointer; } </style> </head> <body> <h1>Tic Tac Toe</h1> <div class="board" id="board"></div> <div id="status">Player X's turn</div> <button onclick="resetGame()">Restart Game</button> <script> const board = document.getElementById('board'); const statusText = document.getElementById('status'); let currentPlayer = 'X'; let gameActive = true; let gameState = ["", "", "", "", "", "", "", "", ""]; const winningConditions = [ [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 cell = e.target; const index = parseInt(cell.getAttribute("data-index")); if (gameState[index] !== "" || !gameActive) return; gameState[index] = currentPlayer; cell.textContent = currentPlayer; if (checkWin()) { statusText.textContent = `Player ${currentPlayer} wins!`; gameActive = false; return; } if (!gameState.includes("")) { statusText.textContent = "It's a draw!"; gameActive = false; return; } currentPlayer = currentPlayer === "X" ? "Y" : "X"; statusText.textContent = `Player ${currentPlayer}'s turn`; } function checkWin() { return winningConditions.some(condition => { const [a, b, c] = condition; return ( gameState[a] && gameState[a] === gameState[b] && gameState[a] === gameState[c] ); }); } function resetGame() { gameState = ["", "", "", "", "", "", "", "", ""]; currentPlayer = "X"; gameActive = true; statusText.textContent = `Player ${currentPlayer}'s turn`; Array.from(board.children).forEach(cell => cell.textContent = ""); } function createBoard() { for (let i = 0; i < 9; i++) { const cell = document.createElement("div"); cell.classList.add("cell"); cell.setAttribute("data-index", i); cell.addEventListener("click", handleCellClick); board.appendChild(cell); } } createBoard(); </script> </body> </html>
🕹️ Features:
- Two-player local mode (X vs O)
- Win detection
- Draw detection
- Restart button
Let me know if you'd like to add AI (computer player), sound effects, animations, or mobile responsiveness!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.