Visualizations
467 / 500

467 · Algorithms

Minesweeper Oracle

Every hidden cell shows its exact mine probability while an AI plays the safest move.

Each revealed number is a constraint on its hidden neighbours, so the frontier becomes a small constraint satisfaction problem. The oracle splits it into independent regions, counts every consistent mine layout in each by backtracking with pruning, and tallies the counts by how many mines a layout uses. Layouts are not equally likely: one that uses k mines leaves the rest to be spread over the U unconstrained cells in C(U, M - k) ways, so convolving the regions' tables and weighting by that binomial gives the exact posterior for every cell, drawn as a heat tint from mint to red. The AI flags every certain mine, reveals every proven-safe cell, and when nothing is certain it pauses on the least dangerous cell, shows the odds and takes the guess.

Try it. Click to reveal and right-click or Shift-click to flag; clicking a satisfied number reveals around it. Toggle the oracle overlay (O) or the AI (A), press Space for a single AI move, N for a new game and D to switch between Beginner, Intermediate and Expert. Hover a hidden cell to read its probability. Leave it alone and the AI takes over again.

  • Exact constraint counting by backtracking
  • Binomial weighting of unconstrained cells
  • Component convolution in log space
  • Incremental search spread over frames
  • Seven-segment LED drawing

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a Minesweeper game in JavaScript and the HTML canvas element where every hidden cell is tinted by its exact probability of being a mine, and an AI plays by always clicking the safest cell. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a 16 x 16 board with 40 mines on a canvas that stays sharp on high-DPI screens. Place the mines after the first click so it is always safe, flood-fill openings from zero cells, and support right-click flags.
- The frontier is every hidden cell next to a revealed number, and each number says how many mines its hidden neighbours hold. Enumerate every mine assignment to the frontier that satisfies all the numbers, with backtracking that abandons a branch as soon as any number is over or under its count.
- Assignments are not equally likely. One that uses k mines leaves the rest to be spread over the U hidden cells that touch no number, in C(U, remaining - k) ways, so weight it by that binomial (use log factorials). A cell's probability is its weighted share of assignments with a mine there; the unconstrained cells share the leftover expected mines.
- Tint hidden cells from green (safe) to red (certain mine) and print the percentage on frontier cells.

Once that works, make it beautiful and smart:
- Add an AI that reveals 0% cells, flags 100% cells, and otherwise clicks the safest cell, with an animated cursor and a label like 'safest is 18.5%'.
- Split the frontier into independent groups, enumerate each separately and combine their tables by convolution, so Expert boards stay fast.
- Animate openings as a ripple and add a seven-segment mine counter.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as spreading the search over animation frames for huge frontiers, measuring the AI's win rate over thousands of games, or breaking ties between equally safe cells by expected information gain.
PreviousAction PotentialHodgkin-Huxley spikes race down an axon while every ion channel opens and shuts. NextMode 7 TrackThe per-scanline floor trick of 16-bit kart racers, with a debug view of every row.

Related visualizations

  • Constraint SolverAlgorithms A sudoku solver reasons in pencil, guesses in ink, and erases when it is wrong.
  • Maze SolverAlgorithms A maze carves itself, then A* hunts for the way out.
  • Nonogram ForgeAlgorithms Paints a pixel picture, proves its clues have one answer, then solves it line by line.
  • MCTS BloomAlgorithms Four-in-a-row against Monte Carlo Tree Search, its search tree blooming as a flower.
  • Perfect SnakeAlgorithms A snake AI that can never crash, cutting safe shortcuts until it fills every cell.
  • Sliding Puzzle SolverGames IDA* with pattern databases solves the 15-puzzle in the fewest possible moves.
  • Dancing LinksAlgorithms Knuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.
  • Rebound RobotsAlgorithms Robots slide until they hit something. A* finds the fewest moves to the target.
  • Wave Function CollapseAlgorithms A tile-based world that assembles itself one consistent cell at a time.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs