grok-3-mini-beta· xAI
LMArena Human Preference 140K
Generate me a tictactoe against an AI game only in css,html,js. Make it look stunning…
- User
Generate me a tictactoe against an AI game only in css,html,js. Make it look stunning with configurable settings.
- Assistant
Below is a complete Tic-Tac-Toe game implemented using only HTML, CSS, and JavaScript. The game features:
- A 3x3 grid for the classic Tic-Tac-Toe board.
- Human vs. AI gameplay: The human player makes moves by clicking on the board, and the AI responds automatically.
- Configurable settings:
- Difficulty levels: Easy (random moves), Medium (blocks and wins when possible), and Hard (unbeatable using the Minimax algorithm).
- First player: Choose whether the human or AI goes first.
- Stunning visuals: I've used modern CSS techniques like gradients, animations, shadows, and responsive design to make it visually appealing. The board has hover effects, smooth transitions for moves, and a clean, modern layout.
The code is self-contained and can be copied into a single HTML file for immediate use. Here's a quick overview:
- HTML: Structures the game board, settings, and messages.
- CSS: Handles styling, animations, and responsiveness.
- JavaScript: Manages game logic, user interactions, AI moves, and settings.
How to Play:
- Select the difficulty and first player from the settings.
- Click "Start Game" to begin.
- Click on an empty cell to make your move (if it's your turn).
- The AI will respond immediately (with a short delay for realism).
- The game displays messages for wins, draws, and turns.
- Click "Reset" to start over with the current settings.
Full Code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Stunning Tic-Tac-Toe vs AI</title> <style> /* General Styles */ body { margin: 0; font-family: 'Arial', sans-serif; background: linear-gradient(135deg, #4a90e2, #9013fe); /* Stunning gradient background */ color: #fff; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; } #game-container { background: rgba(255, 255, 255, 0.1); /* Semi-transparent overlay for depth */ border-radius: 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); /* Subtle shadow for a floating effect */ padding: 40px; text-align: center; backdrop-filter: blur(10px); /* Modern blur effect */ transition: transform 0.3s ease; /* Smooth hover animation */ } #game-container:hover { transform: scale(1.02); /* Slight zoom on hover for interactivity */ } h1 { font-size: 2.5em; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2); /* Text shadow for elegance */ margin-bottom: 20px; } /* Settings Section */ #settings { margin-bottom: 30px; display: flex; flex-direction: column; gap: 15px; } #settings label { font-size: 1.2em; } #settings select, #settings button { padding: 10px; font-size: 1em; border-radius: 8px; border: none; background: #fff; color: #4a90e2; cursor: pointer; transition: background 0.3s, transform 0.3s; } #settings select:hover, #settings button:hover { background: #4a90e2; color: #fff; transform: translateY(-2px); /* Lift effect on hover */ } /* Board Styles */ #board { display: grid; grid-template-columns: repeat(3, 120px); grid-gap: 10px; margin: 20px auto; background: rgba(255, 255, 255, 0.2); /* Subtle board background */ border-radius: 15px; padding: 10px; box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.1); /* Inner glow for depth */ } .cell { width: 120px; height: 120px; background: #fff; /* Cell background */ display: flex; align-items: center; justify-content: center; font-size: 2.5em; font-weight: bold; border-radius: 10px; cursor: pointer; transition: all 0.3s ease; /* Smooth transitions */ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Shadow for 3D effect */ animation: cellAppear 0.5s ease; /* Animation on load */ } .cell:hover { background: #f0f0f0; transform: scale(1.05); /* Slight scale on hover */ } .cell.x { color: #ff6347; /* Tomato for X */ animation: markAppear 0.5s ease; /* Animation for X */ } .cell.o { color: #1e90ff; /* Dodger Blue for O */ animation: markAppear 0.5s ease; /* Animation for O */ } @keyframes cellAppear { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } } @keyframes markAppear { from { opacity: 0; transform: scale(0); } to { opacity: 1; transform: scale(1); } } /* Message and Button Styles */ #message { font-size: 1.5em; margin: 20px 0; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2); } #reset-button { padding: 12px 24px; font-size: 1em; border-radius: 8px; border: none; background: #ff6347; color: #fff; cursor: pointer; transition: background 0.3s, transform 0.3s; } #reset-button:hover { background: #ff4757; transform: translateY(-2px); } /* Responsive Design */ @media (max-width: 600px) { #board { grid-template-columns: repeat(3, 80px); grid-gap: 5px; } .cell { width: 80px; height: 80px; font-size: 2em; } #game-container { padding: 20px; } h1 { font-size: 2em; } } </style> </head> <body> <div id="game-container"> <h1>Tic-Tac-Toe vs AI</h1> <div id="settings"> <label for="difficulty">Difficulty:</label> <select id="difficulty"> <option value="easy">Easy</option> <option value="medium">Medium</option> <option value="hard">Hard</option> </select> <label for="first-player">First Player:</label> <select id="first-player"> <option value="human">Human</option> <option value="ai">AI</option> </select> <button id="start-button">Start Game</button> </div> <div id="board"> <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 id="message"></div> <button id="reset-button">Reset Game</button> </div> <script> const boardElement = document.getElementById('board'); const cells = document.querySelectorAll('.cell'); const messageElement = document.getElementById('message'); const startButton = document.getElementById('start-button'); const resetButton = document.getElementById('reset-button'); const difficultySelect = document.getElementById('difficulty'); const firstPlayerSelect = document.getElementById('first-player'); let board = Array(9).fill(null); // Game board state let currentPlayer = 'X'; // Current player ('X' or 'O') let gameActive = true; // Is the game ongoing? let aiPlayer = 'O'; // AI's mark let humanPlayer = 'X'; // Human's mark function startGame() { board = Array(9).fill(null); gameActive = true; cells.forEach(cell => { cell.textContent = ''; cell.classList.remove('x', 'o'); }); const firstPlayer = firstPlayerSelect.value; // 'human' or 'ai' if (firstPlayer === 'human') { humanPlayer = 'X'; aiPlayer = 'O'; currentPlayer = 'X'; messageElement.textContent = 'Your turn'; } else { humanPlayer = 'O'; aiPlayer = 'X'; currentPlayer = 'X'; // AI goes first messageElement.textContent = 'AI is thinking...'; setTimeout(aiMove, 500); // Delay for AI's first move } } function handleCellClick(event) { const index = event.target.dataset.index; if (board[index] || !gameActive || currentPlayer !== humanPlayer) return; board[index] = humanPlayer; event.target.textContent = humanPlayer; event.target.classList.add(humanPlayer.toLowerCase()); if (checkWin(humanPlayer)) { messageElement.textContent = 'You Win!'; gameActive = false; return; } if (checkDraw()) { messageElement.textContent = 'It\'s a Draw!'; gameActive = false; return; } currentPlayer = aiPlayer; messageElement.textContent = 'AI is thinking...'; setTimeout(aiMove, 500); // AI delay } function aiMove() { let index; const difficulty = difficultySelect.value; if (difficulty === 'easy') { index = getRandomMove(); } else if (difficulty === 'medium') { index = getMediumMove(); } else { // Hard index = minimax(board, aiPlayer).index; } board[index] = aiPlayer; cells[index].textContent = aiPlayer; cells[index].classList.add(aiPlayer.toLowerCase()); if (checkWin(aiPlayer)) { messageElement.textContent = 'AI Wins!'; gameActive = false; return; } if (checkDraw()) { messageElement.textContent = 'It\'s a Draw!'; gameActive = false; return; } currentPlayer = humanPlayer; messageElement.textContent = 'Your turn'; } function getRandomMove() { const emptyIndices = board.map((val, idx) => val === null ? idx : null).filter(val => val !== null); return emptyIndices[Math.floor(Math.random() * emptyIndices.length)]; } function getMediumMove() { for (let i = 0; i < 9; i++) { if (board[i] === null) { board[i] = aiPlayer; if (checkWin(aiPlayer)) { board[i] = null; return i; // AI can win } board[i] = null; } } for (let i = 0; i < 9; i++) { if (board[i] === null) { board[i] = humanPlayer; if (checkWin(humanPlayer)) { board[i] = null; return i; // Block human } board[i] = null; } } return getRandomMove(); // Otherwise, random } function minimax(board, player) { const emptyIndices = board.filter(x => x === null); if (checkWin(humanPlayer)) return { score: -10 }; if (checkWin(aiPlayer)) return { score: 10 }; if (emptyIndices.length === 0) return { score: 0 }; const moves = []; for (let i = 0; i < 9; i++) { if (board[i] === null) { board[i] = player; const nextPlayer = player === aiPlayer ? humanPlayer : aiPlayer; const result = minimax(board, nextPlayer); moves.push({ index: i, score: result.score }); board[i] = null; } } let bestMove; if (player === aiPlayer) { let bestScore = -Infinity; for (let move of moves) { if (move.score > bestScore) { bestScore = move.score; bestMove = move; } } } else { let bestScore = Infinity; for (let move of moves) { if (move.score < bestScore) { bestScore = move.score; bestMove = move; } } } return bestMove; } function checkWin(player) { const wins = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]; return wins.some(combination => combination.every(index => board[index] === player)); } function checkDraw() { return board.every(cell => cell !== null); } cells.forEach(cell => cell.addEventListener('click', handleCellClick)); startButton.addEventListener('click', startGame); resetButton.addEventListener('click', startGame); </script> </body> </html>Key Features:
- Animations: Cells and marks fade in with scale effects for a polished feel.
- Responsiveness: The board adjusts size on smaller screens (e.g., mobile devices).
- Interactivity: Buttons and cells have hover effects and transitions.
- AI Intelligence: The Minimax algorithm ensures the Hard difficulty is unbeatable.
- Edge Cases: Prevents invalid moves, handles draws, and resets the game properly.
You can save this as an
.htmlfile and open it in any modern browser. If you'd like any modifications, such as adding more settings or custom themes, let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.