Play5sDeveloper notes

Minesweeper Logic: Safe First Click, Chording and a Solver

Play5s Minesweeper places its mines only after the first tap, keeps the tapped cell and its neighbours clear, and uses one solver for three jobs: the Hint button, the chord check and the No-guess mode. The solver reads only the numbers the player can see, in three layers of deduction, so a hint is always fair. No-guess boards are made by testing a layout with that solver and moving mines out of any spot where it stalls.

This note covers the rules, the controls and the solver of the Minesweeper page.

Why is the first tap always safe?

The board does not exist until the player touches it. createGame builds empty arrays and a flag, generated, set to false. The first applyOpen call sees that flag and only then places the mines. The placement function builds a safe zone from the tapped cell and its neighbours, takes every other cell as a pool, shuffles the first mine count entries of the pool with a partial Fisher-Yates pass, and marks those cells as mines.

The safe zone is the useful part. Wikipedia's Minesweeper article notes that in some variants the first click is guaranteed to be safe, and some further guarantee that every adjacent cell is safe as well. Play5s does both. The first tap lands on a zero, so it opens an area, and a player never begins with a lonely number and a guess.

A flag before the first tap is refused for the same reason: there is no board to flag yet. After that, opening a cell runs a breadth-first fill. Every zero cell opens its eight neighbours, skipping flagged cells and mines, and the queue continues through any new zeros. The game is won when the number of opened cells equals the number of safe cells. The remaining mines are then flagged automatically, so the counter lands on zero.

The four boards
ModeSizeMines
Beginner9 by 910
Intermediate16 by 1640
Expert30 by 1699
No-guess16 by 1640

How do flags and chords work on a phone?

A mouse has a right button, and a phone has none. The grid's pointer handlers treat a right-click as the contextmenu event, which flags a closed cell and chords an opened number. A middle-button press chords as well. On touch, a press that stays on a closed cell for 320 milliseconds plants a flag, and the handler gives a short buzz where the browser supports navigator.vibrate, 15 milliseconds long.

Two guards keep a long press from becoming a mistake. If the finger moves more than 10 pixels the timer is cancelled, so a scroll or a slide never plants a flag. And when the press does flag, the click event that follows on release is swallowed with a one-shot variable, so the same touch does not also open the cell it just flagged.

Chording is the experienced player's shortcut. When the number of flags around an opened number equals that number, one chord opens every closed, unflagged neighbour at once. The rules are in applyChord: the cell must be opened, its number above zero, the flag count exactly right and at least one target left. A wrong flag is the player's risk, because the chord opens the cell the flag was hiding, and if that cell holds a mine the game is lost, as in the standard game. Numbers that are ready to chord get an is-chordable class. The keyboard has its own path, with arrow keys to move, Enter or Space to open and G to flag.

How does the solver deduce a safe cell?

Three opened numbers above five closed cells121x1safex2minex3safex4minex5safe1 : x1 + x2 + x3 = 12 : x2 + x3 + x4 = 21 : x3 + x4 + x5 = 12 minus 1 (left): x4 - x1 = 1so x4 mine, x1 safe2 minus 1 (right): x2 - x5 = 1so x2 mine, x5 safe, then x3 safeClosed cell x1 to x5; each number counts the mines in the three cells under it.
Three numbers above five closed cells121x1safex2minex3safex4minex5safe1 : x1 + x2 + x3 = 12 : x2 + x3 + x4 = 21 : x3 + x4 + x5 = 12 minus 1 : x4 - x1 = 1so x4 mine, x1 safe2 minus 1 (other side) :x2 - x5 = 1so x2 mine, x5 safe, then x3 safeEach number counts the mines in thethree closed cells under it.
The classic 1-2-1 along a wall. Subtracting one number from another leaves a single unknown cell, and the rest follows.

The solver receives a view of the board, one number per cell: 0 to 8 for an opened number, -1 for a closed cell, -2 for a cell already known to be a mine. It never receives the real mine layout. deduceStep tries three layers and stops at the first that finds something:

  1. Counting. If every mine is already accounted for, all closed cells are safe. If the closed cells exactly equal the mines left, they are all mines. Then, for each opened number, the solver subtracts the mines it already knows about. If nothing remains, the unknown neighbours are safe. If the remainder equals the unknown neighbours, they are all mines. Anything in between becomes a constraint: these cells hold exactly this many mines.
  2. Subtraction. For two constraints A and B that share a cell, the solver compares the cells that belong to only one of them. If B needs as many more mines than A as B has private cells, then every private cell of B is a mine and every private cell of A is safe. This one rule handles the patterns players name, such as 1-2-1 and 1-1 on a wall, without recursion.
  3. Enumeration. The frontier is split into connected pieces, and for each piece of at most 20 cells (MAX_COMPONENT_CELLS) the solver tries every consistent assignment of mines, capped at 12,000 recursion nodes. A cell that is a mine in every solution is a mine, and one that is a mine in none is safe. The total mine count is added to the checks, which matters near the end of a game.

The picture shows layer 2 on the textbook case. With the numbers 1, 2 and 1 above five closed cells, the first number says its three cells hold one mine and the second says its own three hold two. Subtracting leaves the fourth cell with one more mine than the first, which can only mean a mine at the fourth and none at the first. The same step from the other side finds the second cell as a mine and the fifth as safe.

Why can the Hint button not cheat?

The hint calls findLogicalHint, which builds its view only from the cells the player has opened. It ignores the player's flags, so a wrong flag cannot steer it, and it runs up to eight passes of deduceStep, adding the mines it proves as it goes. It prefers, in order, a proven safe cell that is still closed, a proven mine that is not yet flagged, and finally a number that is ready to chord, and for that last one it checks that every flag around the number is itself proven. If none of the three exists it returns nothing, because the numbers on the board do not prove any move.

That fairness is deliberate. A hint that peeked at the board would sometimes point to a safe cell that no amount of reasoning could justify, and the player would learn nothing from it. A hint made from visible numbers is a lesson: every one can be reproduced with a pencil. The same idea shapes the hints in Sudoku, where a hint names the technique, and the other logic puzzles. The overview of how a card Hint works shows the contrast: a card Hint walks a fixed list of moves.

How is a board made so that it never needs a guess?

The fourth mode, No-guess, is 16 by 16 with 40 mines and a promise: a player who reasons correctly never has to gamble. The generator keeps the promise by testing, not by construction. generateBoard works in a loop of up to 24 attempts. Each attempt places a fresh layout with the safe zone around the first tap, then asks solveKnownBoard to play it from that tap using only the deduction layers above. If the solver finishes, the board is accepted.

If the solver stalls, it reports the cells where it stopped: the stalled frontier and the closed cells beyond it, which no number touches. The generator then moves one mine from the stuck frontier to a safe cell in the unreachable interior, or the reverse, and tries again. It allows ten such swaps per attempt. Moving a mine away from the frontier breaks a local 50-50, and moving it into the interior hides it from the numbers. The comment in the source credits this idea to Simon Tatham's Portable Puzzle Collection, and calls the case where all 24 attempts fail extremely rare, under one in ten thousand.

The check runs once, at the moment the first tap lands, because that is when the safe zone is known. The other three modes skip it and place mines in a single pass. The four modes are on the Minesweeper page, which is one of the puzzles on Play5s, and No-guess is the one where the check above runs.