Design a Minesweeper Bot That Decides Which Cell to Flag or Reveal Next
RunloopSoftware Engineer · Technical ScreenLast updated
Design a bot that plays Minesweeper and maximizes its chance of winning. The question tests how the bot turns revealed numbers into constraints, finds cells that are certainly mines or certainly safe, estimates the risk of every hidden cell when it must guess, and keeps each move fast and measurable over many games.
Design a bot that plays Minesweeper. The bot's goal is to win as often as possible, which means clearing the board without ever revealing a mine. The core of the problem: once some cells have been opened and show numbers, how does the bot decide which hidden cell to flag as a mine next, or which one to open?
A possible interface (adjust it as you clarify):
Move = tuple[str, int, int] # ("reveal" or "flag", row, col)
class MinesweeperBot:
def __init__(self, rows: int, cols: int, total_mines: int | None): ...
def next_move(self, board: list[list[str]]) -> Move: ...
The game calls next_move with the visible board, applies the returned move, and calls it again until the game is won or lost.
Constraints and Clarifications
- Use the standard rules. The board is a rectangular grid, and some cells hide mines; the bot never sees where they are.
- Revealing a mine loses the game. The game is won when every cell without a mine has been revealed.
- A revealed safe cell shows how many mines lie among its neighbors: the up to eight cells that touch it horizontally, vertically or diagonally.
- A flag is the player's own marker for a mine. Placing one reveals nothing.
- Board notation:
#is a hidden cell,Fa flagged cell, and0to8a revealed number.
Clarifying Questions
- Does the bot know the total number of mines on the board?
- Is the first reveal guaranteed to be safe, or even guaranteed to open an area of zeros?
- When a 0 is revealed, does the game open its neighbors automatically, or does the bot have to do it?
- Are mines placed uniformly at random, so that every layout matching the visible board is equally likely?
- How large can the board be, and is there a time limit per move?
- Is success judged on a single game or as the win rate over many games?
Part 1 — Moves that are certain
Given the visible board, find every hidden cell that is certainly a mine and every hidden cell that is certainly safe. State the rules and implement them. Then apply them to this board of 3 rows and 5 columns, where row 2 was opened by an earlier reveal of a 0:
col 0 col 1 col 2 col 3 col 4
row 0 # # # # #
row 1 1 1 2 1 1
row 2 0 0 0 0 0
Which cells of row 0 can the bot flag, and which can it reveal, without any risk?
What This Part Should Cover
- How a revealed number and the flags around it become a constraint on the hidden cells
- The rules that settle cells from one number, and at least one rule that combines overlapping numbers
- Repeating the rules until nothing changes, with the correct result on the example board
Part 2 — Choosing a cell when nothing is certain
Often no hidden cell is certainly safe, and the bot must take a risk to continue. Describe how the bot estimates, for every hidden cell, the chance that it holds a mine, and which cell it then reveals. The estimate must also cover hidden cells that touch no revealed number.
What This Part Should Cover
- A risk estimate that treats overlapping numbers together rather than one number at a time
- How hidden cells away from the revealed area, and the total mine count, enter the estimate
- The rule for picking the cell, including ties
- The cost of an exact computation, and how to keep it tractable
Part 3 — The complete bot and how to judge it
Put the pieces together into a bot that plays from the first reveal until the game ends. Cover the first move, the order in which the bot considers its options each turn, the running time of one turn, and how you would show that your bot wins more often than a simpler one.
What This Part Should Cover
- The first move and the per-turn decision order
- Running time per move and how it grows with the board and the revealed boundary
- An evaluation plan: simulated games, a baseline, win rate, and an analysis of losses
Hints
Hint 1
Write the numbers as equations: Each revealed number states how many mines lie among its hidden neighbors, once the flags around it are subtracted. On this board no single number settles any cell on its own; look at numbers whose hidden neighbors overlap.
Hint 2
Look past the nearest number: A hidden cell next to a 1 that has three hidden neighbors is not automatically a one-in-three risk. Consider which complete placements of the remaining mines agree with everything visible, and how likely each of them is.
Hint 3
Win rate is a statistic: One game proves little. Decide what you would simulate, what you would compare against, and what you would record to find out why the bot loses the games it loses.
Follow-up questions
- Two hidden cells are stuck as a 50/50 that no other reveal can inform. When should the bot take that guess, and can the total mine count ever settle it?
- Two candidate cells carry almost the same risk, but revealing one of them is likely to unlock a large part of the board. How do you trade risk against information?
- The board is very large and the revealed boundary forms one long connected region. How do you keep each move within a time limit?
- The total mine count is not shown. What changes in your estimates?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.