gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
Can you make a minesweeper game in html
- User
Can you make a minesweeper game in html
- Assistant
Absolutely! Below is a simple implementation of a Minesweeper game using HTML, CSS, and JavaScript. You can copy and paste this into an
.htmlfile and open it in your browser to play.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Minesweeper Game</title> <style> body { font-family: Arial, sans-serif; text-align: center; background: #f0f0f0; } h1 { margin-top: 20px; } #game { display: inline-block; margin-top: 20px; } .row { display: flex; } .cell { width: 30px; height: 30px; border: 1px solid #888; background: #bdbdbd; font-size: 18px; line-height: 30px; text-align: center; cursor: pointer; user-select: none; transition: background 0.2s; } .cell.revealed { background: #e0e0e0; cursor: default; } .cell.mine { background: #ff4d4d; color: #fff; } .cell.flagged { background: #ffd700; color: #333; } #status { margin-top: 15px; font-size: 20px; font-weight: bold; } #reset { margin-top: 10px; padding: 6px 16px; font-size: 16px; cursor: pointer; } </style> </head> <body> <h1>Minesweeper</h1> <div id="game"></div> <div id="status"></div> <button id="reset">Reset</button> <script> // Game settings const ROWS = 9; const COLS = 9; const MINES = 10; let board = []; let revealed = []; let flagged = []; let gameOver = false; let cellsRevealed = 0; function initGame() { board = []; revealed = []; flagged = []; gameOver = false; cellsRevealed = 0; document.getElementById('status').textContent = ''; createBoard(); renderBoard(); } function createBoard() { // Initialize arrays for (let r = 0; r < ROWS; r++) { board[r] = []; revealed[r] = []; flagged[r] = []; for (let c = 0; c < COLS; c++) { board[r][c] = 0; revealed[r][c] = false; flagged[r][c] = false; } } // Place mines let minesPlaced = 0; while (minesPlaced < MINES) { let r = Math.floor(Math.random() * ROWS); let c = Math.floor(Math.random() * COLS); if (board[r][c] !== 'M') { board[r][c] = 'M'; minesPlaced++; } } // Calculate numbers for (let r = 0; r < ROWS; r++) { for (let c = 0; c < COLS; c++) { if (board[r][c] === 'M') continue; let count = 0; for (let dr = -1; dr <= 1; dr++) { for (let dc = -1; dc <= 1; dc++) { let nr = r + dr, nc = c + dc; if (nr >= 0 && nr < ROWS && nc >= 0 && nc < COLS) { if (board[nr][nc] === 'M') count++; } } } board[r][c] = count; } } } function renderBoard() { const gameDiv = document.getElementById('game'); gameDiv.innerHTML = ''; for (let r = 0; r < ROWS; r++) { const rowDiv = document.createElement('div'); rowDiv.className = 'row'; for (let c = 0; c < COLS; c++) { const cellDiv = document.createElement('div'); cellDiv.className = 'cell'; cellDiv.dataset.row = r; cellDiv.dataset.col = c; if (revealed[r][c]) { cellDiv.classList.add('revealed'); if (board[r][c] === 'M') { cellDiv.classList.add('mine'); cellDiv.textContent = '💣'; } else if (board[r][c] > 0) { cellDiv.textContent = board[r][c]; cellDiv.style.color = getNumberColor(board[r][c]); } } else if (flagged[r][c]) { cellDiv.classList.add('flagged'); cellDiv.textContent = '🚩'; } cellDiv.oncontextmenu = (e) => { e.preventDefault(); if (!revealed[r][c] && !gameOver) { flagged[r][c] = !flagged[r][c]; renderBoard(); } }; cellDiv.onclick = () => { if (!revealed[r][c] && !flagged[r][c] && !gameOver) { revealCell(r, c); renderBoard(); checkWin(); } }; rowDiv.appendChild(cellDiv); } gameDiv.appendChild(rowDiv); } } function revealCell(r, c) { if (r < 0 || r >= ROWS || c < 0 || c >= COLS || revealed[r][c] || flagged[r][c]) return; revealed[r][c] = true; cellsRevealed++; if (board[r][c] === 'M') { gameOver = true; revealAllMines(); document.getElementById('status').textContent = '💥 Game Over!'; } else if (board[r][c] === 0) { // Reveal neighbors for (let dr = -1; dr <= 1; dr++) { for (let dc = -1; dc <= 1; dc++) { if (dr !== 0 || dc !== 0) { revealCell(r + dr, c + dc); } } } } } function revealAllMines() { for (let r = 0; r < ROWS; r++) { for (let c = 0; c < COLS; c++) { if (board[r][c] === 'M') revealed[r][c] = true; } } renderBoard(); } function checkWin() { if (gameOver) return; let safeCells = ROWS * COLS - MINES; let revealedCount = 0; for (let r = 0; r < ROWS; r++) { for (let c = 0; c < COLS; c++) { if (revealed[r][c] && board[r][c] !== 'M') revealedCount++; } } if (revealedCount === safeCells) { gameOver = true; document.getElementById('status').textContent = '🎉 You Win!'; revealAllMines(); } } function getNumberColor(num) { const colors = ['#000', '#1976d2', '#388e3c', '#d32f2f', '#7b1fa2', '#fbc02d', '#0288d1', '#c2185b', '#616161']; return colors[num] || '#000'; } document.getElementById('reset').onclick = initGame; // Start the game initGame(); </script> </body> </html>
How to play:
- Left-click to reveal a cell.
- Right-click (or long-press on mobile) to flag/unflag a cell.
- Click "Reset" to start a new game.
Let me know if you want a different board size or more features!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.