DevSupporter
DevSupporter

Welcome back

Please enter your details to sign in to your account

Forgot password?
Sign in

Minesweeper

BFSDFSIntermediate

Clear the board without hitting mines. Auto-reveal uses BFS flood fill.

๐Ÿ’ฃ Mines40
โฑ Time0
โญ Best-

  • Left click to reveal a cell
  • Right click (or long press on mobile) to place/remove a flag
  • Numbers show how many mines are adjacent to that cell
  • Empty cells auto-reveal using flood fill โ€” no need to click each one
  • Your first click is always safe
  • Flag all mines and reveal all safe cells to win

1. BFS Flood Fill โ€” Auto Reveal

When you click an empty cell (0 adjacent mines), the game automatically reveals all connected empty cells using Breadth-First Search (BFS).

function revealCells(
  board: Cell[][],
  startR: number,
  startC: number
): Cell[][] {
  const newBoard = board.map(row => row.map(cell => ({ ...cell })));
  const queue: [number, number][] = [[startR, startC]];
  const visited = new Set<string>();

  while (queue.length > 0) {
    const [r, c] = queue.shift()!;
    const key = `${r},${c}`;
    if (visited.has(key)) continue;
    visited.add(key);

    const cell = newBoard[r][c];
    if (cell.status === 'flagged' || cell.isMine) continue;

    cell.status = 'revealed';

    if (cell.adjacentMines === 0) {
      for (const [dr, dc] of DIRECTIONS) {
        const nr = r + dr;
        const nc = c + dc;
        if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) {
          if (!visited.has(`${nr},${nc}`)) queue.push([nr, nc]);
        }
      }
    }
  }
  return newBoard;
}
2. Mine Placement โ€” Safe First Click

Mines are placed after the first click. The clicked cell and its 8 neighbors are excluded from mine placement, guaranteeing the first click always opens a safe area.

function placeMines(
  rows: number,
  cols: number,
  mineCount: number,
  firstClick: { r: number; c: number }
): Set<string> {
  const excluded = new Set<string>();
  for (let dr = -1; dr <= 1; dr++)
    for (let dc = -1; dc <= 1; dc++)
      excluded.add(`${firstClick.r + dr},${firstClick.c + dc}`);

  const mines = new Set<string>();
  while (mines.size < mineCount) {
    const r = Math.floor(Math.random() * rows);
    const c = Math.floor(Math.random() * cols);
    const key = `${r},${c}`;
    if (!excluded.has(key)) mines.add(key);
  }
  return mines;
}
3. Adjacent Mine Counting โ€” O(1) per cell

For each cell, we check all 8 neighbors and count mines. Using a Set for mine positions gives O(1) lookup.

function calcAdjacentMines(
  r: number,
  c: number,
  mines: Set<string>,
  rows: number,
  cols: number
): number {
  return DIRECTIONS.reduce((count, [dr, dc]) => {
    const nr = r + dr;
    const nc = c + dc;
    if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) {
      if (mines.has(`${nr},${nc}`)) count++;
    }
    return count;
  }, 0);
}

2026 ยฉ DevSupporter - Playground for Developers by DevSupporter