An implementation of Minesweeper in Unity, built on uGUI with a grid that resizes itself to fit the window.
Built with Unity 2019.2.8f1.
Written in 2020. Archived to GitHub in 2026.
Standard rules. Left-click reveals a cell, right-click cycles its marker, and the game is won once every non-mine cell has been revealed.
- Left-click: reveal a cell
- Right-click: cycle through flag, question mark, and unmarked
- N: new game
Revealed cells show a count of adjacent mines, colored per digit. Clicking a cell with no adjacent mines flood-fills outward through all connected empty cells and their borders. The counter in the top left shows mines remaining, which is the total mine count minus the flags currently placed.
A status emoji reacts to play. It looks nervous while the mouse is held down over a cell, and switches to a win or lose face when the game ends. On a loss every mine is revealed, and any flag left on a safe cell is redrawn struck through, as in the screenshot above.
Difficulty is chosen with the buttons along the top. Picking one starts a new game.
Mines aren't placed when the board is drawn. They're planted on the first click, which lets that click be excluded from the candidate positions. The protected set covers the clicked cell, its eight neighbours, and the four board corners, so the opening move always lands in open space rather than on a mine or an isolated single-cell reveal.
CantorPairUtility maps an (x, y) pair to a single int and back using a
Cantor pairing function. This lets the mine list and
the safe-spot set be flat List<int>s rather than collections of tuples or a second parallel grid.
Row count is derived at runtime from the screen height and the configured column count, and
AdjustGridLayoutCellSize drives the GridLayoutGroup so cells stay square as the aspect ratio
changes.
Each difficulty sets a column count and a mine density. The row count follows from the window size,
so the mine total works out to columns * rows * density.
| Difficulty | Columns | Mine density |
|---|---|---|
| Easy | 20 | 10% |
| Normal | 30 | 15% |
| Hard | 40 | 20% |
| Expert | 50 | 25% |
