This project implements a simple command-line Minesweeper game along with an automated solver algorithm in Python. The solver uses basic logical deduction rules and resorts to random guessing when no certain moves can be determined.
- Minesweeper Game: A basic text-based Minesweeper implementation.
- Logical Solver: An algorithm that attempts to solve the game automatically.
- Identifies and flags cells guaranteed to be mines.
- Identifies and reveals cells guaranteed to be safe.
- Random Guessing: When no logically certain moves are available, the solver makes a random guess among the remaining hidden cells.
- Step-by-Step Visualization: The solver's progress is printed to the console step-by-step with a configurable delay.
- Configurable Board: Easily change the board dimensions (width, height) and the number of mines in
main.py
. - Safe First Click: Mines are placed after the solver's first click, ensuring the first move is always safe (as in most standard Minesweeper implementations).
- Board Representation: The
board.py
module definesCell
andBoard
classes to represent the game state, handle mine placement, reveal/flag actions, and check for win/loss conditions. - Solver Logic: The
solver.py
module contains theSolver
class. Its core logic relies on two basic Minesweeper rules applied iteratively around revealed numbered cells:- Rule 1 (All Mines Flagged): If a revealed cell showing number
N
has exactlyN
adjacent cells flagged, then all other adjacent hidden cells are guaranteed to be safe and can be revealed. - Rule 2 (All Hidden are Mines): If a revealed cell showing number
N
hasF
adjacent cells flagged, and exactlyN - F
adjacent cells are still hidden, then thoseN - F
hidden cells must be mines and can be flagged.
- Rule 1 (All Mines Flagged): If a revealed cell showing number
- Iteration: The solver repeatedly applies these rules until no more safe reveals or flags can be deduced from the current board state.
- Guessing: If the solver reaches a state where neither Rule 1 nor Rule 2 can identify a certain move for any revealed number cell, it identifies all remaining hidden, unflagged cells and randomly selects one to reveal.
- Game Loop: The
main.py
script initializes the board and solver, then runs a loop where the solver makes a move, the board state is updated and displayed, and the process repeats until the game is won or lost.
- Run the Solver: Execute the main script from your terminal:
python main.py
- Observe: The program will print the initial empty board, and then proceed step-by-step, showing the board state after each move made by the solver. It will indicate whether the move was based on logic (revealing a safe cell, flagging a mine) or a guess.
- Configuration: To change the game parameters, edit the constants at the top of
main.py
:BOARD_WIDTH
: The width of the game board.BOARD_HEIGHT
: The height of the game board.NUM_MINES
: The total number of mines on the board.STEP_DELAY_SECONDS
: The pause duration (in seconds) between solver steps for easier visualization (set to 0 for maximum speed).
- Not Guaranteed to Win: Minesweeper is NP-complete. When the solver encounters situations where no logical deductions are possible, it must guess. Guesses can be incorrect, leading to a loss. This algorithm does not guarantee a win.
- Basic Logic Only: The solver implements only the most fundamental Minesweeper deduction rules. It does not employ more advanced techniques like constraint satisfaction programming or analyzing patterns across multiple interacting number cells simultaneously.
- Random Guessing: The guessing strategy is purely random among the available hidden cells. It does not calculate probabilities to make more informed guesses.