This repository contains a robust, object-oriented Minesweeper engine built with Java. The application is designed with a focus on algorithmic efficiency, clean separation of concerns, and a "fail-safe" user experience. Key features include a recursive-safe area reveal and a guaranteed safe first move through deferred grid generation.
The engine utilizes a Dispatcher class to manage the application lifecycle. This separates the initial configuration (mine count validation) from the active game loop, allowing for a clean transition from the setup phase to the interactive gameplay phase.
To ensure professional-grade gameplay, the engine implements Lazy Initialization.
- The Logic: The internal board (mine placement and neighbor-clue calculation) is not generated until the player issues their first
freecommand. - The Benefit: The player's initial coordinates are passed to the generator to be explicitly excluded from the mine-distribution pool, guaranteeing that the first move is never a mine.
For the "cascade" effect—where clicking an empty cell reveals a large safe area—the engine employs a Breadth-First Search (BFS) algorithm.
- Implementation: Utilizes a
Queue<List<Integer>>and aseen[][]boolean matrix to track visited cells. - Stability: By opting for an iterative BFS over a recursive Depth-First Search (DFS), the engine avoids
StackOverflowErrorrisks, making it stable for significantly larger grid dimensions.
- Dual-Board System: Maintains a
savedBoard(the ground truth) and acurrBoard(the player's visible UI). This allows for non-destructive cell marking (*) and unmarking without losing the underlying neighbor-clue data. - Optimized Lookups: Uses
HashSet<List<Integer>>for mine coordinate tracking, ensuring O(1) constant-time complexity for verifying moves against mine locations.
- Two Winning Conditions: Supports winning by either correctly flagging all mines or revealing every safe cell on the board.
- Dual-Input Commands: Supports
free(reveal) andmine(flag/unflag) commands via a Cartesian coordinate system. - Input Robustness: Includes a
Validationlayer to handle malformed input, out-of-bounds coordinates, and invalid mine density requests.
- Language: Java 17+
- Concepts: OOP, BFS (Breadth-First Search), Lazy Initialization, State Management, Java Collections Framework (Set, Queue, LinkedList).
- Clone:
git clone https://github.com/yourusername/minesweeper-java.git - Compile:
javac minesweeper/*.java - Run:
java minesweeper.Main
This project is licensed under the CC BY-NC 4.0 License - see the LICENSE file for details.