Skip to content

A Python script that automatically plays Minesweeper, applying basic logic to find safe moves and mines, resorting to random guesses when stuck. πŸ€–πŸ’‘πŸŽ²

Notifications You must be signed in to change notification settings

BuiltWitAI/minesweeper-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Python Minesweeper Solver

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.

Features

  • 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).

How it Works

  1. Board Representation: The board.py module defines Cell and Board classes to represent the game state, handle mine placement, reveal/flag actions, and check for win/loss conditions.
  2. Solver Logic: The solver.py module contains the Solver 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 exactly N 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 has F adjacent cells flagged, and exactly N - F adjacent cells are still hidden, then those N - F hidden cells must be mines and can be flagged.
  3. Iteration: The solver repeatedly applies these rules until no more safe reveals or flags can be deduced from the current board state.
  4. 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.
  5. 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.

Usage

  1. Run the Solver: Execute the main script from your terminal:
    python main.py
  2. 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.
  3. 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).

Limitations

  • 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.

About

A Python script that automatically plays Minesweeper, applying basic logic to find safe moves and mines, resorting to random guesses when stuck. πŸ€–πŸ’‘πŸŽ²

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages