grok-3-preview-02-24· xAI
LMArena Human Preference 140K
tic tac toe html site
- User
tic tac toe html site
- Assistant
Below is a simple implementation of a Tic Tac Toe game using HTML, CSS, and JavaScript. This code creates a fully functional Tic Tac Toe game that you can play in your browser. Copy the code into a
.htmlfile and open it in a web browser to play.<!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; background-color: #f0f0f0; } h1 { color: #333; } #game-board { display: grid; grid-template-columns: repeat(3, 100px); grid-gap: 5px; margin: 20px; } .cell { width: 100px; height: 100px; background-color: #fff; border: 2px solid #333; display: flex; align-items: center; justify-content: center; font-size: 40px; cursor: pointer; } .cell:hover { background-color: #f9f9f9; } #status { font-size: 24px; margin-bottom: 20px; } #reset-btn { padding: 10px 20px; font-size: 18px; cursor: pointer; background-color: #007BFF; color: white; border: none; border-radius: 5px; } #reset-btn:hover { background-color: #0056b3; } </style> </head> <body> <h1>Tic Tac Toe</h1> <div id="status">Player X's turn</div> <div id="game-board"></div> <button id="reset-btn" onclick="resetGame()">Reset Game</button> <script> const board = Array(9).fill(null); let currentPlayer = 'X'; let gameActive = true; const winningCombinations = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows [0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns [0, 4, 8], [2, 4, 6] // Diagonals ]; const gameBoard = document.getElementById('game-board'); const status = document.getElementById('status'); // Create the game board 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); gameBoard.appendChild(cell); } } // Handle cell click function handleCellClick(e) { const index = e.target.getAttribute('data-index'); if (board[index] || !gameActive) return; board[index] = currentPlayer; e.target.textContent = currentPlayer; if (checkWin()) { status.textContent = `Player ${currentPlayer} wins!`; gameActive = false; return; } if (checkDraw()) { status.textContent = "It's a draw!"; gameActive = false; return; } currentPlayer = currentPlayer === 'X' ? 'O' : 'X'; status.textContent = `Player ${currentPlayer}'s turn`; } // Check for a win function checkWin() { return winningCombinations.some(combination => { return combination.every(index => { return board[index] === currentPlayer; }); }); } // Check for a draw function checkDraw() { return board.every(cell => cell !== null); } // Reset the game function resetGame() { board.fill(null); currentPlayer = 'X'; gameActive = true; status.textContent = `Player ${currentPlayer}'s turn`; document.querySelectorAll('.cell').forEach(cell => { cell.textContent = ''; }); } // Initialize the game createBoard(); </script> </body> </html>Explanation of the Code:
-
HTML Structure:
- The HTML contains a title, a status message, a game board (a grid of 9 cells), and a reset button.
- The game board is created dynamically using JavaScript.
-
CSS Styling:
- The game board is styled as a 3x3 grid using CSS Grid.
- Each cell is styled to be clickable, with hover effects for better user interaction.
- The reset button is styled with a modern look.
-
JavaScript Logic:
- The game state is stored in a
boardarray, where each cell is initiallynull. - The
currentPlayervariable tracks whether it's X's or O's turn. - The
gameActivevariable ensures the game stops when someone wins or it's a draw. - The
winningCombinationsarray defines all possible ways to win (rows, columns, diagonals). - The
handleCellClickfunction handles player moves, updates the board, and checks for a win or draw. - The
checkWinfunction checks if the current player has won by matching the board state against the winning combinations. - The
checkDrawfunction checks if the game is a draw (all cells filled with no winner). - The
resetGamefunction resets the game to its initial state.
- The game state is stored in a
How to Use:
- Save the code in a file named
tic-tac-toe.html. - Open the file in a web browser.
- Click on the cells to play as X or O.
- The status message will update to show whose turn it is, announce a winner, or declare a draw.
- Click the "Reset Game" button to start a new game.
This is a basic implementation, but you can enhance it further by adding features like a score tracker, animations, or a computer opponent (AI).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.