o4-mini-2025-04-16· OpenAI LMArena Human Preference 140K
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 simple 4×4 Tic-Tac-Toe where you play X and the bot plays O. The bot logic is basic: it will take a winning move if available, block your winning move if needed, otherwise pick a random empty cell.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>4×4 Tic-Tac-Toe vs Bot</title> <style> body { font-family: sans-serif; display: flex; flex-direction: column; align-items: center; margin-top: 30px; } #board { display: grid; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(4, 80px); gap: 5px; } .cell { width: 80px; height: 80px; display: flex; justify-content: center; align-items: center; font-size: 2em; background: #f0f0f0; cursor: pointer; user-select: none; transition: background 0.2s; } .cell:hover { background: #ddd; } #message { margin: 20px; font-size: 1.2em; } #resetBtn { padding: 8px 16px; font-size: 1em; } </style> </head> <body> <div id="message">Your turn! (X)</div> <div id="board"></div> <button id="resetBtn">Restart</button> <script> const SIZE = 4; let board = []; let gameOver = false; const boardEl = document.getElementById('board'); const msgEl = document.getElementById('message'); const resetBtn = document.getElementById('resetBtn'); // Initialize function init() { board = Array(SIZE).fill().map(() => Array(SIZE).fill('')); gameOver = false; msgEl.textContent = "Your turn! (X)"; render(); } // Render the board function render() { boardEl.innerHTML = ''; for (let r = 0; r < SIZE; r++) { for (let c = 0; c < SIZE; c++) { const cell = document.createElement('div'); cell.className = 'cell'; cell.textContent = board[r][c]; cell.addEventListener('click', () => onCellClick(r, c)); boardEl.append(cell); } } } // Handle a player click function onCellClick(r, c) { if (gameOver || board[r][c] !== '') return; playerMove(r, c); if (!gameOver) { setTimeout(botMove, 200); } } // Player places 'X' function playerMove(r, c) { board[r][c] = 'X'; render(); if (checkWin('X')) { msgEl.textContent = "You win! 🎉"; gameOver = true; } else if (isDraw()) { msgEl.textContent = "Draw!"; gameOver = true; } else { msgEl.textContent = "Bot's turn (O)…"; } } // Bot logic: win > block > random function botMove() { // 1) Winning move let move = findBestMove('O'); // 2) Block player's winning if (!move) move = findBestMove('X'); // 3) Random if (!move) { const empties = []; for (let r = 0; r < SIZE; r++) for (let c = 0; c < SIZE; c++) if (board[r][c] === '') empties.push({r, c}); move = empties[Math.floor(Math.random() * empties.length)]; } board[move.r][move.c] = 'O'; render(); if (checkWin('O')) { msgEl.textContent = "Bot wins! 💻"; gameOver = true; } else if (isDraw()) { msgEl.textContent = "Draw!"; gameOver = true; } else { msgEl.textContent = "Your turn! (X)"; } } // Find a move that causes `player` to win immediately function findBestMove(player) { for (let r = 0; r < SIZE; r++) { for (let c = 0; c < SIZE; c++) { if (board[r][c] === '') { board[r][c] = player; if (checkWin(player)) { board[r][c] = ''; return { r, c }; } board[r][c] = ''; } } } return null; } // Check win for given symbol function checkWin(p) { // rows & cols for (let i = 0; i < SIZE; i++) { if (board[i].every(v => v === p)) return true; if (board.map(row => row[i]).every(v => v === p)) return true; } // diag TL→BR if ([...Array(SIZE)].every((_, i) => board[i][i] === p)) return true; // diag BL→TR if ([...Array(SIZE)].every((_, i) => board[SIZE-1-i][i] === p)) return true; return false; } // Check draw function isDraw() { return board.flat().every(cell => cell !== ''); } // Hook up reset resetBtn.addEventListener('click', init); // Start! init(); </script> </body> </html>How it works
- We create a 4×4 grid of divs.
- Clicking an empty cell places your “X” and checks for a win/draw.
- If the game continues, the bot looks for:
- A winning “O” move
- Else a blocking “X” move
- Else a random empty cell
- We check for wins on rows, columns, and the two main diagonals.
- A “Restart” button resets the board.
Enjoy playing against your simple AI! 😊
- User
add dark mode, particles, add 5x5 grid
- Assistant
Below is a full 5×5 Tic-Tac-Toe you can copy & paste. It includes:
• Dark-mode toggle
• A simple particle background
• You vs. a basic “bot” (win->block->random)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>5×5 Tic-Tac-Toe + Dark Mode + Particles</title> <style> :root { --bg: #f0f0f0; --fg: #333; --cell-bg: #fff; --cell-hover: #e0e0e0; --btn-bg: #444; --btn-fg: #fff; } body.dark { --bg: #1a1a1a; --fg: #ddd; --cell-bg: #2a2a2a; --cell-hover: #3a3a3a; --btn-bg: #ddd; --btn-fg: #222; } * { box-sizing: border-box; margin: 0; padding: 0; } body { background: var(--bg); color: var(--fg); font-family: sans-serif; overflow: hidden; } #controls { position: fixed; top: 10px; left: 50%; transform: translateX(-50%); display: flex; gap: 10px; z-index: 10; } button { padding: 8px 16px; background: var(--btn-bg); color: var(--btn-fg); border: none; cursor: pointer; border-radius: 4px; font-size: 0.9em; } button:hover { opacity: 0.9; } #message { position: fixed; top: 60px; left: 50%; transform: translateX(-50%); font-size: 1.2em; z-index: 10; } #board { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: grid; grid-template: repeat(5, 60px) / repeat(5, 60px); gap: 5px; z-index: 5; } .cell { width: 60px; height: 60px; background: var(--cell-bg); display: flex; align-items: center; justify-content: center; font-size: 2em; cursor: pointer; user-select: none; transition: background 0.2s; } .cell:hover { background: var(--cell-hover); } canvas { position: fixed; top: 0; left: 0; z-index: 0; } </style> </head> <body> <canvas id="particle-canvas"></canvas> <div id="controls"> <button id="resetBtn">↻ Restart</button> <button id="darkBtn">🌙 Dark Mode</button> </div> <div id="message">Your turn! (X)</div> <div id="board"></div> <script> (() => { // --- CONFIG --- const SIZE = 5; // grid size const PARTICLE_COUNT = 80; // number of particles // --- STATE --- let board = []; let gameOver = false; let dark = false; const boardEl = document.getElementById('board'); const msgEl = document.getElementById('message'); const resetBtn = document.getElementById('resetBtn'); const darkBtn = document.getElementById('darkBtn'); const canvas = document.getElementById('particle-canvas'); const ctx = canvas.getContext('2d'); let particles = []; // --- INITIALIZATION --- function initGame() { board = Array(SIZE).fill().map(() => Array(SIZE).fill('')); gameOver = false; msgEl.textContent = "Your turn! (X)"; renderBoard(); } function renderBoard() { boardEl.innerHTML = ''; board.forEach((row, r) => { row.forEach((cell, c) => { const div = document.createElement('div'); div.className = 'cell'; div.textContent = cell; div.onclick = () => onCellClick(r, c); boardEl.append(div); }); }); } // --- PLAYER MOVE --- function onCellClick(r, c) { if (gameOver || board[r][c]) return; makeMove(r, c, 'X'); if (!gameOver) { setTimeout(botTurn, 200); } } function makeMove(r, c, p) { board[r][c] = p; renderBoard(); if (checkWin(p)) { msgEl.textContent = p === 'X' ? "You win! 🎉" : "Bot wins! 🤖"; gameOver = true; } else if (isDraw()) { msgEl.textContent = "Draw! 🤝"; gameOver = true; } else { msgEl.textContent = p === 'X' ? "Bot's turn… (O)" : "Your turn! (X)"; } } // --- BOT LOGIC --- function botTurn() { // 1) Win if possible let mv = findCriticalMove('O'); // 2) Block X if (!mv) mv = findCriticalMove('X'); // 3) Else random if (!mv) { const empties = []; board.forEach((row, r) => row.forEach((cell, c) => { if (!cell) empties.push({r,c}); }) ); mv = empties[Math.floor(Math.random()*empties.length)]; } makeMove(mv.r, mv.c, 'O'); } function findCriticalMove(p) { for (let r=0; r<SIZE; r++) { for (let c=0; c<SIZE; c++) { if (!board[r][c]) { board[r][c] = p; if (checkWin(p)) { board[r][c] = ''; return {r, c}; } board[r][c] = ''; } } } return null; } // --- WIN & DRAW CHECK --- function checkWin(p) { // rows & cols for (let i=0; i<SIZE; i++) { if (board[i].every(v=>v===p)) return true; if (board.map(r=>r[i]).every(v=>v===p)) return true; } // main diag if ([...Array(SIZE)].every((_,i)=> board[i][i]===p)) return true; // anti-diag if ([...Array(SIZE)].every((_,i)=> board[SIZE-1-i][i]===p)) return true; return false; } function isDraw() { return board.flat().every(v=>v); } // --- DARK MODE --- darkBtn.onclick = () => { dark = !dark; document.body.classList.toggle('dark', dark); darkBtn.textContent = dark ? '☀️ Light Mode' : '🌙 Dark Mode'; }; // --- RESET --- resetBtn.onclick = initGame; // --- PARTICLES SETUP --- function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } window.addEventListener('resize', resizeCanvas); resizeCanvas(); class Particle { constructor() { this.reset(); } reset() { this.x = Math.random() * canvas.width; this.y = canvas.height + Math.random()*100; this.vy = 0.5 + Math.random(); this.size = 1 + Math.random()*2; this.alpha = 0.1 + Math.random()*0.3; } update() { this.y -= this.vy; if (this.y < -10) this.reset(); } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, 2*Math.PI); ctx.fillStyle = `rgba(${dark?200:50},${dark?200:50},${dark?200:50},${this.alpha})`; ctx.fill(); } } function initParticles() { particles = Array(PARTICLE_COUNT).fill().map(() => new Particle()); requestAnimationFrame(animate); } function animate() { ctx.clearRect(0,0,canvas.width,canvas.height); particles.forEach(p => { p.update(); p.draw(); }); requestAnimationFrame(animate); } // --- START EVERYTHING --- initGame(); initParticles(); })(); </script> </body> </html>How it works
- We bumped
SIZEto 5 → a 5×5 grid. - Added a Dark-mode toggle that flips a
body.darkclass and switches CSS variables. - A tiny particle system draws drifting dots (canvas underlay).
- The bot still does:
• win if it can → block you → pick random.
Enjoy your new, sleeker 5×5 Tic-Tac-Toe! 🎮✨
- We bumped
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.