Difficulty: Medium Topics: Flood Fill (DFS/BFS), 2D Grid, Recursion Key Concepts: Game Loop, Recursion, Object States.
Phase 1: Requirements Gathering
Goals
Design the classic Minesweeper game.
Support core gameplay: Reveal cells, flag mines, win/loss conditions.
Handle "Flood Fill" for empty areas.
1. Who are the actors?
Player: Interacts with the grid.
Game Engine: Manages rules and board state.
2. What are the must-have features? (Core)
Grid Setup: Initialize N*N grid with M mines.
Click (Reveal): Reveal a cell.
If Mine -> Game Over.
If Empty (0 adj) -> Reveal all adjacent empty cells (Recursive).
If Number -> Show number.
Flag: Mark a cell as a potential mine.
Win Condition: All non-mine cells are revealed.
3. What are the constraints?
Efficiency: First click should never be a mine (Optional "fairness" rule).
Recursion: Ensure no stack overflow for large grids (or use BFS).
Phase 2: Use Cases
UC1: Start Game
Actor: Player Flow:
Player selects difficulty (e.g., 10x10, 10 mines).
System places mines randomly.
System pre-calculates adjacency numbers (optional, or calculate on fly).
System displays masked grid.
UC2: Click Cell
Actor: Player Flow:
Player clicks (r, c).
System checks state:
If Flagged or Revealed -> Ignore.
If Mine -> LOSE.
If Number -> Reveal number.
If Empty (0) -> Trigger Flood Fill to reveal cluster.
System checks Win Condition (TotalCells - RevealedCount == NumMines).
System updates UI.
Phase 3: Class Diagram
Step 1: Core Entities
MinesweeperGame: Controller.
Board: The Grid.
Cell: Individual square.
UML Diagram
Phase 4: Design Patterns
1. Composite Pattern (Grid Structure)
Description: Treats individual objects and compositions of objects uniformly.
Why used: The Board manages a grid of Cells. Operations like "Reveal" can propagate from the Board to a Cell, and recursively to neighbor Cells (Flood Fill), treating the grid structure as a unified whole.
2. State Pattern
Description: Allows an object to alter its behavior when its internal state changes.
Why used: The Game has distinct states (PLAYING, WON, LOST). The response to a user click depends entirely on the current state (e.g., clicks are ignored if the game is LOST).
Phase 5: Code Key Methods
Java Implementation
Phase 6: Discussion
Algorithms
Q: Why Flood Fill?
A: "It efficiently checks connectivity. When a 0 is clicked, all connected 0s (and their boundary numbers) are safe to reveal. DFS/BFS both work. Time Complexity O(Total Cells)."
Fairness
Q: How to ensure first click is never a mine?
A: "Delay mine placement until after the first click.
User clicks (r, c).
Place mines randomly, ensuring (r, c) and its neighbors are skipped.
Calculate numbers.
Proceed with reveal."
Scalability
Q: Infinite Minesweeper?
A: "If the board is infinite, we can't pre-allocate a 2D array. Use a HashMap<String, Cell> key="r,c" to store only generated chunks/cells."
SOLID Principles Checklist
S (Single Responsibility): Board manages grid logic, Game manages flow.
O (Open/Closed): New cell types (e.g., "Mega Mine") could extend Cell.
L (Liskov Substitution): N/A.
I (Interface Segregation): N/A.
D (Dependency Inversion): N/A (Self-contained logic).