create 4x4 tic-tac-toe game in html/css/js you are playing with bot
- User
create 4x4 tic-tac-toe game in html/css/js you are playing with bot
Assistant
Here's a 4x4 tic-tac-toe game where you play against a bot:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>4x4 Tic-Tac-Toe</title> <style> body { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; margin-top: 50px; background-color: #f5f5f5; } h1 { color: #333; margin-bottom: 20px; } .game-container { display: flex; flex-direction: column; align-items: center; } .board { display: grid; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(4, 80px); gap: 5px; margin-bottom: 20px; } .cell { width: 80px; height: 80px; background-color: white; border: 2px solid #333; display: flex; justify-content: center; align-items: center; font-size: 40px; cursor: pointer; transition: background-color 0.3s; } .cell:hover { background-color: #f0f0f0; } .status { font-size: 24px; margin-bottom: 20px; height: 30px; } .reset-btn { padding: 10px 20px; font-size: 16px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .reset-btn:hover { background-color: #45a049; } .x { color: #e74c3c; } .o { color: #3498db; } </style> </head> <body> <h1>4x4 Tic-Tac-Toe</h1> <div class="game-container"> <div class="status" id="status">Your turn (X)</div> <div class="board" id="board"></div> <button class="reset-btn" id="resetBtn">Reset Game</button> </div> <script> document.addEventListener('DOMContentLoaded', () => { const board = document.getElementById('board'); const status = document.getElementById('status'); const resetBtn = document.getElementById('resetBtn'); // Game state let gameBoard = Array(16).fill(''); let gameActive = true; const PLAYER = 'X'; const BOT = 'O'; let currentPlayer = PLAYER; // Winning combinations for 4x4 board const winningCombinations = [ // Rows [0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], // Columns [0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], // Diagonals [0, 5, 10, 15], [3, 6, 9, 12] ]; // Initialize the board function initializeGame() { gameBoard = Array(16).fill(''); gameActive = true; currentPlayer = PLAYER; status.textContent = 'Your turn (X)'; // Clear and recreate the board board.innerHTML = ''; for (let i = 0; i < 16; i++) { const cell = document.createElement('div'); cell.classList.add('cell'); cell.dataset.index = i; cell.addEventListener('click', handleCellClick); board.appendChild(cell); } } // Handle cell click function handleCellClick(e) { const index = parseInt(e.target.dataset.index); // Check if the cell is already filled or game is not active if (gameBoard[index] !== '' || !gameActive) { return; } // Update the cell with player's mark updateCell(index, PLAYER); // Check for win or draw if (checkWin(PLAYER)) { status.textContent = 'You win!'; gameActive = false; return; } if (checkDraw()) { status.textContent = 'Game ended in a draw!'; gameActive = false; return; } // Bot's turn status.textContent = "Bot's turn (O)"; // Delay bot's move for better UX setTimeout(() => { botMove(); if (checkWin(BOT)) { status.textContent = 'Bot wins!'; gameActive = false; return; } if (checkDraw()) { status.textContent = 'Game ended in a draw!'; gameActive = false; return; } status.textContent = 'Your turn (X)'; }, 500); } // Update cell with player's mark function updateCell(index, player) { gameBoard[index] = player; const cell = document.querySelector(`.cell[data-index="${index}"]`); cell.textContent = player; cell.classList.add(player.toLowerCase()); } // Bot's move logic function botMove() { // Check if bot can win in the next move for (let i = 0; i < 16; i++) { if (gameBoard[i] === '') { gameBoard[i] = BOT; if (checkWin(BOT)) { updateCell(i, BOT); return; } gameBoard[i] = ''; } } // Check if player can win in the next move and block for (let i = 0; i < 16; i++) { if (gameBoard[i] === '') { gameBoard[i] = PLAYER; if (checkWin(PLAYER)) { gameBoard[i] = ''; updateCell(i, BOT); return; } gameBoard[i] = ''; } } // Try to take center cells if available const centerCells = [5, 6, 9, 10]; const availableCenterCells = centerCells.filter(i => gameBoard[i] === ''); if (availableCenterCells.length > 0) { const randomIndex = Math.floor(Math.random() * availableCenterCells.length); updateCell(availableCenterCells[randomIndex], BOT); return; } // Take any available cell const availableCells = gameBoard.map((cell, index) => cell === '' ? index : null).filter(cell => cell !== null); if (availableCells.length > 0) { const randomIndex = Math.floor(Math.random() * availableCells.length); updateCell(availableCells[randomIndex], BOT); } } // Check for win function checkWin(player) { return winningCombinations.some(combination => { return combination.every(index => gameBoard[index] === player); }); } // Check for draw function checkDraw() { return !gameBoard.includes(''); } // Reset game resetBtn.addEventListener('click', initializeGame); // Initialize the game initializeGame(); }); </script> </body> </html>Would you like me to explain or break down the code?
- User
add dark mode, particles, add 5x5 grid
Assistant
Here's the updated 5x5 tic-tac-toe game with dark mode, particles, and a 5x5 grid:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>5x5 Tic-Tac-Toe</title> <style> :root { --bg-color: #f5f5f5; --text-color: #333; --cell-bg: white; --cell-border: #333; --x-color: #e74c3c; --o-color: #3498db; --btn-bg: #4CAF50; --btn-hover: #45a049; --cell-hover: #f0f0f0; } .dark-mode { --bg-color: #222; --text-color: #f5f5f5; --cell-bg: #333; --cell-border: #555; --x-color: #ff6b6b; --o-color: #5dade2; --btn-bg: #2ecc71; --btn-hover: #27ae60; --cell-hover: #444; } body { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; margin-top: 50px; background-color: var(--bg-color); color: var(--text-color); transition: all 0.3s ease; overflow: hidden; } #particles-js { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; } h1 { color: var(--text-color); margin-bottom: 20px; } .game-container { display: flex; flex-direction: column; align-items: center; z-index: 1; } .board { display: grid; grid-template-columns: repeat(5, 70px); grid-template-rows: repeat(5, 70px); gap: 5px; margin-bottom: 20px; } .cell { width: 70px; height: 70px; background-color: var(--cell-bg); border: 2px solid var(--cell-border); display: flex; justify-content: center; align-items: center; font-size: 36px; cursor: pointer; transition: background-color 0.3s; } .cell:hover { background-color: var(--cell-hover); } .status { font-size: 24px; margin-bottom: 20px; height: 30px; } .buttons { display: flex; gap: 10px; margin-bottom: 20px; } .btn { padding: 10px 20px; font-size: 16px; background-color: var(--btn-bg); color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .btn:hover { background-color: var(--btn-hover); } .x { color: var(--x-color); } .o { color: var(--o-color); } </style> </head> <body> <div id="particles-js"></div> <h1>5x5 Tic-Tac-Toe</h1> <div class="game-container"> <div class="status" id="status">Your turn (X)</div> <div class="board" id="board"></div> <div class="buttons"> <button class="btn" id="resetBtn">Reset Game</button> <button class="btn" id="toggleTheme">Toggle Dark Mode</button> </div> </div> <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script> <script> document.addEventListener('DOMContentLoaded', () => { const board = document.getElementById('board'); const status = document.getElementById('status'); const resetBtn = document.getElementById('resetBtn'); const toggleTheme = document.getElementById('toggleTheme'); // Initialize particles.js particlesJS('particles-js', { "particles": { "number": { "value": 50, "density": { "enable": true, "value_area": 800 } }, "color": { "value": "#3498db" }, "shape": { "type": "circle", "stroke": { "width": 0, "color": "#000000" } }, "opacity": { "value": 0.5, "random": false, "anim": { "enable": false, "speed": 1, "opacity_min": 0.1, "sync": false } }, "size": { "value": 3, "random": true, "anim": { "enable": false, "speed": 40, "size_min": 0.1, "sync": false } }, "line_linked": { "enable": true, "distance": 150, "color": "#3498db", "opacity": 0.4, "width": 1 }, "move": { "enable": true, "speed": 2, "direction": "none", "random": false, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 600, "rotateY": 1200 } } }, "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": true, "mode": "grab" }, "onclick": { "enable": true, "mode": "push" }, "resize": true }, "modes": { "grab": { "distance": 140, "line_linked": { "opacity": 1 } }, "push": { "particles_nb": 4 } } }, "retina_detect": true }); // Game state const GRID_SIZE = 5; const TOTAL_CELLS = GRID_SIZE * GRID_SIZE; let gameBoard = Array(TOTAL_CELLS).fill(''); let gameActive = true; const PLAYER = 'X'; const BOT = 'O'; let currentPlayer = PLAYER; // Generate winning combinations for 5x5 board const winningCombinations = []; // Rows for (let i = 0; i < GRID_SIZE; i++) { const row = []; for (let j = 0; j < GRID_SIZE; j++) { row.push(i * GRID_SIZE + j); } winningCombinations.push(row); } // Columns for (let i = 0; i < GRID_SIZE; i++) { const col = []; for (let j = 0; j < GRID_SIZE; j++) { col.push(j * GRID_SIZE + i); } winningCombinations.push(col); } // Diagonals const diag1 = []; const diag2 = []; for (let i = 0; i < GRID_SIZE; i++) { diag1.push(i * GRID_SIZE + i); diag2.push(i * GRID_SIZE + (GRID_SIZE - 1 - i)); } winningCombinations.push(diag1); winningCombinations.push(diag2); // Initialize the board function initializeGame() { gameBoard = Array(TOTAL_CELLS).fill(''); gameActive = true; currentPlayer = PLAYER; status.textContent = 'Your turn (X)'; // Clear and recreate the board board.innerHTML = ''; for (let i = 0; i < TOTAL_CELLS; i++) { const cell = document.createElement('div'); cell.classList.add('cell'); cell.dataset.index = i; cell.addEventListener('click', handleCellClick); board.appendChild(cell); } } // Handle cell click function handleCellClick(e) { const index = parseInt(e.target.dataset.index); // Check if the cell is already filled or game is not active if (gameBoard[index] !== '' || !gameActive) { return; } // Update the cell with player's mark updateCell(index, PLAYER); // Check for win or draw if (checkWin(PLAYER)) { status.textContent = 'You win!'; gameActive = false; return; } if (checkDraw()) { status.textContent = 'Game ended in a draw!'; gameActive = false; return; } // Bot's turn status.textContent = "Bot's turn (O)"; // Delay bot's move for better UX setTimeout(() => { botMove(); if (checkWin(BOT)) { status.textContent = 'Bot wins!'; gameActive = false; return; } if (checkDraw()) { status.textContent = 'Game ended in a draw!'; gameActive = false; return; } status.textContent = 'Your turn (X)'; }, 500); } // Update cell with player's mark function updateCell(index, player) { gameBoard[index] = player; const cell = document.querySelector(`.cell[data-index="${index}"]`); cell.textContent = player; cell.classList.add(player.toLowerCase()); } // Bot's move logic function botMove() { // Check if bot can win in the next move for (let i = 0; i < TOTAL_CELLS; i++) { if (gameBoard[i] === '') { gameBoard[i] = BOT; if (checkWin(BOT)) { updateCell(i, BOT); return; } gameBoard[i] = ''; } } // Check if player can win in the next move and block for (let i = 0; i < TOTAL_CELLS; i++) { if (gameBoard[i] === '') { gameBoard[i] = PLAYER; if (checkWin(PLAYER)) { gameBoard[i] = ''; updateCell(i, BOT); return; } gameBoard[i] = ''; } } // Try to take center and adjacent cells if available const centerIndex = Math.floor(TOTAL_CELLS / 2); const centerCells = [ centerIndex, centerIndex - GRID_SIZE, // top centerIndex + GRID_SIZE, // bottom centerIndex - 1, // left centerIndex + 1, // right centerIndex - GRID_SIZE - 1, // top-left centerIndex - GRID_SIZE + 1, // top-right centerIndex + GRID_SIZE - 1, // bottom-left centerIndex + GRID_SIZE + 1 // bottom-right ]; const availableCenterCells = centerCells.filter(i => i >= 0 && i < TOTAL_CELLS && gameBoard[i] === '' ); if (availableCenterCells.length > 0) { const randomIndex = Math.floor(Math.random() * availableCenterCells.length); updateCell(availableCenterCells[randomIndex], BOT); return; } // Take any available cell const availableCells = gameBoard.map((cell, index) => cell === '' ? index : null ).filter(cell => cell !== null); if (availableCells.length > 0) { const randomIndex = Math.floor(Math.random() * availableCells.length); updateCell(availableCells[randomIndex], BOT); } } // Check for win function checkWin(player) { return winningCombinations.some(combination => { return combination.every(index => gameBoard[index] === player); }); } // Check for draw function checkDraw() { return !gameBoard.includes(''); } // Toggle dark mode toggleTheme.addEventListener('click', () => { document.body.classList.toggle('dark-mode'); // Update particles color based on theme const isDarkMode = document.body.classList.contains('dark-mode'); const particleColor = isDarkMode ? "#5dade2" : "#3498db"; const lineColor = isDarkMode ? "#5dade2" : "#3498db"; pJSDom[0].pJS.particles.color.value = particleColor; pJSDom[0].pJS.particles.line_linked.color = lineColor; // Refresh particles pJSDom[0].pJS.fn.particlesRefresh(); }); // Reset game resetBtn.addEventListener('click', initializeGame); // Initialize the game initializeGame(); }); </script> </body> </html>Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.