An intelligent automated solver that plays Minesweeper using computer vision and probability-based decision-making.
- Computer Vision Detection: Uses OpenCV and Tesseract OCR to detect and analyze the Minesweeper game board
- Seven-Segment Display OCR: Custom OCR system for reading the mine counter display
- Intelligent Decision Making: Employs probability-based algorithms to make optimal moves
- Real-time Game Analysis: Continuously monitors the game state and adapts its strategy
- Automated Player: Fully automates gameplay by integrating screen capture, decision-making, and mouse control for seamless interaction with the Minesweeper game.
- Real-time Visualization: Displays dynamic updates of the solver's progress, including grid detection, cell states, and probability calculations, for an interactive experience.
- Visual Debugging Tools: Comprehensive visualization options for development and debugging
Watch the solver in action with real-time visualization enabled. The solver displays:
- Detected cell states and numbers
- Mine probability heatmaps
beginner_gameplay.mp4
Outcome: The solver successfully completes the game, showcasing its probability-based decision-making and constraint satisfaction algorithms.
Run the solver in a "headless" mode for a clean, visualizer-free experience:
Intermediate mode:
intermediate_gameplay.mp4
Expert mode:
expert_gameplay.mp4
Outcome: The solver demonstrates high efficiency and accuracy, finishing both modes without user intervention.
Unfortunately, some scenarios in Minesweeper are unsolvable without guessing. For example:
failed_gameplay.mp4
Outcome: The solver clicks the wrong cell, triggering a mine. This showcases the inherent challenge of luck-based situations in Minesweeper.
- Python 3.7+
- Clone the repository:
git clone https://github.com/supat-roong/minesweeper_solver.git
cd minesweeper_solver
- Install required packages:
pip install -r requirements.txt
-
Install Tesseract OCR (optional for better gameboard OCR):
- Windows: Download and install from GitHub Tesseract Releases
- Linux:
sudo apt-get install tesseract-ocr
- macOS:
brew install tesseract
- Open your favorite Minesweeper game.
- Run the solver:
python main.py
- Use the selection tool to select the game board region.
- The program will automatically solve the game!
Edit config.py
to customize:
- Minesweeper grid size
- Visualization options
- Tesseract OCR path
- Other solver parameters
-
Board Detection:
- Captures the screen region containing the game
- Uses computer vision to detect grid lines and cells
- Recognizes cell states (unopened, numbers, flags, mines)
-
Solver Logic:
- Maintains a game state matrix
- Calculates mine probabilities using neighbor constraints
- Uses three-pass probability refinement:
- Initial probability assignment
- Direct neighbor constraints
- Overlapping constraint analysis
-
Move Execution:
- Prioritizes definite safe moves
- Falls back to lowest probability cells when no safe moves exist
- Executes moves through mouse control
The program includes two types of visualization options:
- Real-time Visualization: Displays dynamic updates of the solver's progress, including grid detection, cell states, and probability calculations.
- Debug Visualization: Provides detailed analysis tools such as grid line detection, cell state recognition, mine probability heatmaps, and seven-segment OCR analysis.
Note: Only one visualization mode can be activated at a time due to main thread limitations. Enable your desired mode in config.py
.
The project is organized as follows:
minesweeper_solver/
├── src/ # Source code directory
│ ├── board_detector.py # Detects the Minesweeper game board using computer vision
│ ├── bomb_counter.py # Counts the number of total mines using visual recognition
│ ├── cell_detector.py # Identifies cell states (e.g., unopened, flagged, numbered)
│ ├── config.py # Configuration file for solver parameters and visualization options
│ ├── custom_dataclass.py # Defines custom data structures used throughout the project
│ ├── debugger.py # Provides debugging tools for grid detection and analysis
│ ├── grid_detector.py # Detects the grid layout of the Minesweeper board
│ ├── minesweeper_detector.py # Core module for integrating board detection logic
│ ├── player.py # Handles automated gameplay (mouse control and decision execution)
│ ├── screenshot.py # Captures the screen region containing the Minesweeper game
│ ├── seven_segment_ocr.py # Recognizes numbers in the seven-segment display
│ ├── solver.py # Implements the probability-based Minesweeper solving algorithm
│ └── visualizer.py # Handles real-time visualization of grid detection and probability calculations
└── main.py # Main entry point for running the solver
This project is licensed under the MIT License - see the LICENSE file for details.