Can you make a minesweeper game in html
- User
Can you make a minesweeper game in html
Assistant
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Minesweeper</title> <style> body { font-family: Arial, sans-serif; text-align: center; background-color: #f0f0f0; } #game-container { margin: 20px auto; display: inline-block; background-color: #bdbdbd; padding: 10px; border: 3px solid #7b7b7b; border-radius: 5px; } #grid { display: grid; grid-template-columns: repeat(10, 30px); grid-gap: 1px; } .cell { width: 30px; height: 30px; background-color: #c0c0c0; border: 2px outset #f0f0f0; display: flex; align-items: center; justify-content: center; font-weight: bold; cursor: pointer; } .revealed { border: 1px solid #7b7b7b; background-color: #d1d1d1; } .mine { background-color: #ff0000; } .flagged { background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill="red" d="M4 2h1v12H4V2zm1 0h6l-2 4 2 4H5V2z"/></svg>'); background-repeat: no-repeat; background-position: center; } #game-info { margin: 10px 0; font-size: 18px; } #mine-count, #timer { display: inline-block; background-color: #000; color: #f00; padding: 5px 10px; margin: 0 10px; font-family: monospace; font-size: 20px; border: 2px inset #7b7b7b; } button { margin: 10px; padding: 8px 15px; font-size: 16px; cursor: pointer; } .number-1 { color: blue; } .number-2 { color: green; } .number-3 { color: red; } .number-4 { color: darkblue; } .number-5 { color: darkred; } .number-6 { color: teal; } .number-7 { color: black; } .number-8 { color: gray; } </style> </head> <body> <h1>Minesweeper</h1> <div id="game-container"> <div id="game-info"> <div id="mine-count">010</div> <button id="reset">😊</button> <div id="timer">000</div> </div> <div id="grid"></div> </div> <script> const GRID_SIZE = 10; const MINE_COUNT = 10; let grid = []; let gameOver = false; let flagsPlaced = 0; let revealedCells = 0; let timerInterval; let seconds = 0; function initGame() { // Reset variables grid = []; gameOver = false; flagsPlaced = 0; revealedCells = 0; seconds = 0; clearInterval(timerInterval); document.getElementById('timer').textContent = '000'; document.getElementById('mine-count').textContent = String(MINE_COUNT).padStart(3, '0'); document.getElementById('reset').textContent = '😊'; // Create empty grid for (let y = 0; y < GRID_SIZE; y++) { grid[y] = []; for (let x = 0; x < GRID_SIZE; x++) { grid[y][x] = { isMine: false, isRevealed: false, isFlagged: false, adjacentMines: 0 }; } } // Place mines randomly let minesPlaced = 0; while (minesPlaced < MINE_COUNT) { const x = Math.floor(Math.random() * GRID_SIZE); const y = Math.floor(Math.random() * GRID_SIZE); if (!grid[y][x].isMine) { grid[y][x].isMine = true; minesPlaced++; } } // Calculate adjacent mines for (let y = 0; y < GRID_SIZE; y++) { for (let x = 0; x < GRID_SIZE; x++) { if (!grid[y][x].isMine) { grid[y][x].adjacentMines = countAdjacentMines(x, y); } } } // Render the grid renderGrid(); } function countAdjacentMines(x, y) { let count = 0; for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { const nx = x + dx; const ny = y + dy; if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) { if (grid[ny][nx].isMine) { count++; } } } } return count; } function renderGrid() { const gridElement = document.getElementById('grid'); gridElement.innerHTML = ''; for (let y = 0; y < GRID_SIZE; y++) { for (let x = 0; x < GRID_SIZE; x++) { const cell = document.createElement('div'); cell.classList.add('cell'); cell.dataset.x = x; cell.dataset.y = y; if (grid[y][x].isRevealed) { cell.classList.add('revealed'); if (grid[y][x].isMine) { cell.classList.add('mine'); cell.textContent = '💣'; } else if (grid[y][x].adjacentMines > 0) { cell.textContent = grid[y][x].adjacentMines; cell.classList.add(`number-${grid[y][x].adjacentMines}`); } } else if (grid[y][x].isFlagged) { cell.classList.add('flagged'); } cell.addEventListener('click', () => handleCellClick(x, y)); cell.addEventListener('contextmenu', (e) => { e.preventDefault(); handleRightClick(x, y); }); gridElement.appendChild(cell); } } } function handleCellClick(x, y) { if (gameOver || grid[y][x].isRevealed || grid[y][x].isFlagged) { return; } // Start timer on first click if (revealedCells === 0) { startTimer(); } // Reveal the cell revealCell(x, y); // Check for game over if (grid[y][x].isMine) { endGame(false); } else { // Auto-reveal empty cells if (grid[y][x].adjacentMines === 0) { revealAdjacentCells(x, y); } // Check for win checkWinCondition(); } renderGrid(); } function handleRightClick(x, y) { if (gameOver || grid[y][x].isRevealed) { return; } // Start timer on first right-click if (revealedCells === 0) { startTimer(); } // Toggle flag if (grid[y][x].isFlagged) { grid[y][x].isFlagged = false; flagsPlaced--; } else { grid[y][x].isFlagged = true; flagsPlaced++; } // Update mine counter document.getElementById('mine-count').textContent = String(MINE_COUNT - flagsPlaced).padStart(3, '0'); renderGrid(); } function revealCell(x, y) { if (grid[y][x].isRevealed || grid[y][x].isFlagged) { return; } grid[y][x].isRevealed = true; revealedCells++; } function revealAdjacentCells(x, y) { for (let dy = -1; dy <= 1; dy++) { for (let dx = -1; dx <= 1; dx++) { const nx = x + dx; const ny = y + dy; if (nx >= 0 && nx < GRID_SIZE && ny >= 0 && ny < GRID_SIZE) { if (!grid[ny][nx].isRevealed && !grid[ny][nx].isFlagged) { revealCell(nx, ny); if (grid[ny][nx].adjacentMines === 0) { revealAdjacentCells(nx, ny); } } } } } } function checkWinCondition() { const totalCells = GRID_SIZE * GRID_SIZE; if (revealedCells === totalCells - MINE_COUNT) { endGame(true); } } function endGame(isWin) { gameOver = true; clearInterval(timerInterval); // Reveal all mines for (let y = 0; y < GRID_SIZE; y++) { for (let x = 0; x < GRID_SIZE; x++) { if (grid[y][x].isMine) { grid[y][x].isRevealed = true; } } } // Update smiley face document.getElementById('reset').textContent = isWin ? '😎' : '😵'; } function startTimer() { timerInterval = setInterval(() => { seconds++; document.getElementById('timer').textContent = String(Math.min(seconds, 999)).padStart(3, '0'); }, 1000); } // Initialize the game document.getElementById('reset').addEventListener('click', initGame); initGame(); </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.