Overview
MinesweeperCLI is a terminal recreation of the classic puzzle game Minesweeper, developed entirely in Java without third-party frameworks. In addition to standard player-controlled gameplay, it includes an algorithmic solver bot capable of computing probabilistic moves and clearing boards autonomously.
The goal of the project was to exercise strict object-oriented architecture, algorithmic problem solving (constraint satisfaction), and terminal visualization using ANSI escape sequences.
Game Rules & Features
- Grid Customization: Configurable board dimensions (e.g., 9x9 Beginner, 16x16 Intermediate, 30x16 Expert, and custom dimensions) with user-defined mine quantities.
- First-Move Guarantee: The initial revealed cell is guaranteed to never be a mine; the minefield layout is generated dynamically after the player's first input.
- Recursive Zero-Expansion: Revealing a cell with zero adjacent mines triggers a recursive flood-fill algorithm that automatically uncovers neighboring safe cells.
- ANSI Color Output: Clean, formatted terminal display with color-coded adjacency numbers and status indicators.
The Solver Bot Algorithm
The integrated bot operates via a two-tier algorithmic evaluation cycle:
Solver Loop
│
▼
[Check Deterministic Moves]
├─ If remaining hidden neighbors == unflagged mines ➔ Flag all
└─ If remaining flags == cell number ➔ Reveal all remaining neighbors
│
▼ (No deterministic moves found)
[Probabilistic Analysis]
├─ Build system of linear equations across connected frontier tiles
├─ Calculate exact probability distribution for each candidate tile
└─ Choose cell with minimum mine likelihood (safest guess)
1. Deterministic Rule Matching
The solver continuously scans active frontier cells (uncovered cells adjacent to covered cells) to apply classic logical deductions:
- All Mines: If an uncovered number equals the exact count of unflagged hidden neighbors, every neighbor is flagged.
- All Safe: If an uncovered number already has all required flags placed around it, all other neighbors are safely revealed.
// Check deterministic safe moves based on active frontier constraints
public List<Cell> findDeterministicMoves(Board board) {
List<Cell> safeMoves = new ArrayList<>();
for (Cell cell : board.getFrontierCells()) {
if (cell.getRemainingHiddenNeighbors() == cell.getAdjacentMines()) {
cell.flagAllHiddenNeighbors();
} else if (cell.getPlacedFlags() == cell.getAdjacentMines()) {
safeMoves.addAll(cell.getUnflaggedHiddenNeighbors());
}
}
return safeMoves;
}
2. Constraint Satisfaction & Probabilities
When no trivial moves remain, the bot clusters active frontier cells into independent mathematical sub-problems. It enumerates valid mine assignments to compute exact probability percentages for each frontier cell, executing the move with the lowest statistical risk.
System Architecture
| Class | Responsibility |
|---|---|
Board | Maintains the 2D matrix of Cell objects and validates coordinate boundaries. |
Cell | Encapsulates state (hidden, flagged, revealed), mine presence, and adjacent mine counts. |
GameEngine | Controls game loop, input parsing, timer tracking, and victory/defeat conditions. |
MinesweeperBot | Analyzes frontier states, constructs constraint models, and issues optimal moves. |
TerminalRenderer | Renders the game board with ANSI colors and formatted column/row coordinates. |
Key Takeaways
- In-depth practical implementation of recursive flood-fill and graph traversal algorithms.
- Applied constraint satisfaction problem (CSP) modeling to turn-based game solving.
- Zero external library dependencies, compiling and running natively in any modern terminal.