Build a Minesweeper Game with JavaScript

2026-12-02来源 dev.toEnglish

Minesweeper is a logic puzzle disguised as a clicking game: a grid hides mines, numbers tell you how many mines touch each square, and one wrong click ends the run. It is a strong beginner project because it teaches 2D arrays, neighbour counting, recursion for flood fill, and right-click handling.

The board is a 2D array. Each cell knows whether it has a mine, whether it is revealed, whether it is flagged, and how many mines touch it. Mines are placed randomly after the first click, so the first click can never lose the game — a small kindness the original game also uses:

const ROWS = 9, COLS = 9, MINES = 10; let board = []; function createBoard() { board = Array.from({ length: ROWS }, () => Array.from({ length: COLS }, () => ({ mine: false, revealed: false, flagged: false, count: 0, })) ); } function placeMines(safeRow, safeCol) { let placed = 0; while (placed < MINES) { const r = Math.floor(Math.random() * ROWS); const c = Math.floor(Math.random() * COLS); if (board[r][c].mine) continue; if (r === safeRow && c === safeCol) continue; board[r][c].mine = true; placed++; } countNeighbours(); } 

Counting neighbours is the heart of the game. For each cell, look at the eight surrounding squares and count the mines:

function countNeighbours() { for (let r = 0; r < ROWS; r++) { for (let c = 0; c < COLS; c++) { let n = 0; for (let dr = -1; dr <= 1; dr++) { for (let dc = -1; dc <= 1; dc++) { const nr = r + dr, nc = c + dc; if (nr < 0 || nr >= ROWS || nc < 0 || nc >= COLS) continue; if (board[nr][nc].mine) n++; } } board[r][c].count = n; } } } 

Revealing a cell with zero neighbouring mines should open the whole empty area at once. That is flood fill — a recursive reveal that stops at numbered cells:

function reveal(r, c) { const cell = board[r][c]; if (cell.revealed || cell.flagged) return; cell.revealed = true; if (cell.mine) return gameOver(); if (cell.count === 0) { for (let dr = -1; dr <= 1; dr++) { for (let dc = -1; dc <= 1; dc++) { const nr = r + dr, nc = c + dc; if (nr < 0 || nr >= ROWS || nc < 0 || nc >= COLS) continue; reveal(nr, nc); } } } render(); } 

Right-click flags a cell. Listen for the contextmenu event, call preventDefault so the browser menu does not appear, and toggle the flag:

boardElement.addEventListener("contextmenu", (e) => { e.preventDefault(); const cell = e.target.closest(".cell"); if (!cell) return; const { r, c } = cell.dataset; board[r][c].flagged = !board[r][c].flagged; render(); }); 

The win condition is simple: the game is won when every non-mine cell is revealed. Check it after each reveal, and show a message with the time taken. Ways to extend it: add difficulty levels (beginner 9x9/10 mines, intermediate 16x16/40, expert 30x16/99), add a timer that starts on the first click, or add a "chord" click that reveals all neighbours of a numbered cell when the right flags are placed. The flood-fill recursion here is the same algorithm used in paint-bucket tools and pathfinding, so it is worth understanding deeply.

原文:https://www.a8q9.cn/en/article/build-a-minesweeper-game-with-javascript-a04d70.html

相关文章