write tic tac teo game in html css js modern in one file
- User
write tic tac teo game in html css js modern in one file
- Assistant
Sure! Below is a modern implementation of a Tic Tac Toe game using HTML, CSS, and JavaScript all within a single file. This version is responsive, visually appealing, and includes features like win detection, draw detection, and the ability to reset the game.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Modern Tic Tac Toe</title> <style> * { box-sizing: border-box; } body { background-color: #f0f4f8; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .container { text-align: center; } h1 { color: #333; } .game-board { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 5px; margin: 20px auto; } .cell { background-color: #fff; border: 2px solid #333; display: flex; justify-content: center; align-items: center; font-size: 2.5rem; cursor: pointer; transition: background-color 0.3s; } .cell:hover { background-color: #e6e6e6; } .cell.disabled { cursor: not-allowed; background-color: #ddd; } .status { margin-top: 20px; font-size: 1.2rem; color: #555; } .reset-button { margin-top: 15px; padding: 10px 20px; font-size: 1rem; background-color: #333; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .reset-button:hover { background-color: #555; } @media (max-width: 500px) { .game-board { grid-template-columns: repeat(3, 80px); grid-template-rows: repeat(3, 80px); gap: 4px; } .cell { font-size: 2rem; } } </style> </head> <body> <div class="container"> <h1>Tic Tac Toe</h1> <div class="game-board" id="gameBoard"> <!-- 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 class="status" id="status">Player X's turn</div> <button class="reset-button" id="resetButton">Reset Game</button> </div> <script> const gameBoard = document.getElementById('gameBoard'); const statusDisplay = document.getElementById('status'); const resetButton = document.getElementById('resetButton'); const cells = document.querySelectorAll('.cell'); const PLAYER_X = 'X'; const PLAYER_O = 'O'; let currentPlayer = PLAYER_X; let board = ['', '', '', '', '', '', '', '', '']; let gameActive = true; 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 clickedCell = e.target; const cellIndex = parseInt(clickedCell.getAttribute('data-index')); if (board[cellIndex] !== '' || !gameActive) { return; } updateCell(clickedCell, cellIndex); checkResult(); } function updateCell(cell, index) { board[index] = currentPlayer; cell.textContent = currentPlayer; cell.classList.add('disabled'); } function switchPlayer() { currentPlayer = currentPlayer === PLAYER_X ? PLAYER_O : PLAYER_X; statusDisplay.textContent = `Player ${currentPlayer}'s turn`; } function checkResult() { let roundWon = false; for (let condition of winningConditions) { const [a, b, c] = condition; if (board[a] && board[a] === board[b] && board[a] === board[c]) { roundWon = true; break; } } if (roundWon) { statusDisplay.textContent = `Player ${currentPlayer} wins!`; gameActive = false; return; } if (!board.includes('')) { statusDisplay.textContent = `It's a draw!`; gameActive = false; return; } switchPlayer(); } function resetGame() { currentPlayer = PLAYER_X; board = ['', '', '', '', '', '', '', '', '']; gameActive = true; statusDisplay.textContent = `Player ${currentPlayer}'s turn`; cells.forEach(cell => { cell.textContent = ''; cell.classList.remove('disabled'); }); } cells.forEach(cell => cell.addEventListener('click', handleCellClick)); resetButton.addEventListener('click', resetGame); </script> </body> </html>Features:
-
Responsive Design: The game layout adjusts for smaller screens to ensure a good user experience on both desktop and mobile devices.
-
Win and Draw Detection: The game automatically detects when a player has won or if the game ends in a draw, updating the status accordingly.
-
Reset Functionality: A reset button allows players to start a new game at any time.
-
Interactive UI: Cells highlight on hover, and once a cell is clicked, it becomes disabled to prevent further clicks.
How It Works:
-
HTML Structure: The game consists of a container that holds the title, the game board (a 3x3 grid of cells), a status display, and a reset button.
-
CSS Styling: Flexbox and Grid are used to center the game and create the grid layout. Visual cues like hover effects and disabled states enhance user interaction.
-
JavaScript Logic:
- Variables: Track the current player, the game board state, and whether the game is active.
- Event Listeners: Attached to each cell to handle clicks and to the reset button.
- Functions:
handleCellClick: Manages what happens when a cell is clicked.updateCell: Updates the UI and game state with the current player's move.switchPlayer: Switches turns between Player X and Player O.checkResult: Checks for a win or a draw after each move.resetGame: Resets the game to its initial state.
Feel free to copy the above code into a
.htmlfile and open it in your browser to play the game! -