KatInterview

Object-Oriented Design of a Minesweeper Game with Per-Cell Mine Probabilities

System DesignModel answer
MediumModel answerSystem Designobject-oriented-designstate-machineflood-fill

Last updated

Design an OO Minesweeper where each cell carries an independent mine probability, covering initialization, class model, click/print APIs, state transitions, and testability.

You are asked to design (in object-oriented terms, with code-level APIs) a single-player Minesweeper game played on a rectangular grid of cells.

The twist: at construction time the caller supplies, for every cell, a probability prob in [0, 1] representing the independent chance that the cell is a mine. Your design must explain:

  1. How those per-cell probabilities drive generation of the actual mine layout and the initial cell states.
  2. The core classes (game, board, cell, plus any collaborators), each one's responsibilities, the relationships between them, and the public APIs for:
    • (a) printing the current visible board state, and
    • (b) handling a user click.
  3. The high-level state transitions for a click: losing when a mine is hit, otherwise the reveal behavior (flood reveal for zero-adjacent-mine cells) delegated to the board's algorithm.
  4. How invalid clicks (out of bounds) and duplicate clicks (already revealed or flagged) are handled.
  5. How the design stays maintainable and testable despite the randomness.

Assume in-memory, single-player operation; boards can be large (up to roughly 10^6 cells), so reveal logic must not be naive recursion. You may add flagging as a cheap extension.

Hints

Hint 1

Separate the decision of *where the mines are* from the board itself — an injected layout strategy lets you swap random generation for a fixed mask in tests.

Hint 2

Precompute each cell's adjacent-mine count once at construction so clicks are pure state transitions, not repeated scans.

Hint 3

Model the game as an explicit status enum (in progress / won / lost) and return a result enum from click rather than printing or throwing.