A Python implementation of the classic Minesweeper game with an AI solver that uses logical deduction to play automatically.
- Full Minesweeper game implementation with customizable board size
- Classic Windows-style UI with emoji faces and counters
- Multiple play modes: Human, AI solver, and Random clicker
- AI solver that uses logical deduction and priority-based decision making
- Clean architecture with separation of concerns between game logic and UI
- Python 3.9+
- pygame
- heapdict
- Clone the repository:
git clone https://github.com/yourusername/MinesweeperAI.git
cd MinesweeperAI- Install the required packages:
pip install pygame heapdictRun the game with one of the following modes:
# Play manually
python main.py --mode human
# Watch the AI solver play
python main.py --mode ai
# Watch random clicking strategy
python main.py --mode random- Left Click: Reveal a tile
- Right Click: Flag/unflag a tile
- Shift + Left Click: Chord (reveal all non-flagged neighbors of a satisfied numbered tile)
- Click Emoji Face: Restart the game
main.py: Entry point of the applicationminesweeper/game.py: Main game class and constantsgrid.py: Grid management and bomb placement logictile.py: Tile representation and neighbor connectionsapi.py: API for interacting with the game (used by both UI and AI)ai/solver.py: AI solver implementation
assets/: Game images and sprites
The AI solver uses a priority queue to process tiles based on their potential for making progress:
- Priority 0: Satisfied tiles (where all bombs are flagged)
- Priority 1: Satisfiable tiles (where all hidden neighbors must be bombs)
- Priority 2: Tiles that require more information
The solver continuously:
- Picks the highest priority tile from the queue
- Flags bombs when identified with certainty
- Reveals safe tiles when identified
- Uses "chording" to efficiently reveal multiple tiles at once
- Makes a random move when logical deduction is insufficient
You can customize the game by modifying constants in minesweeper/game.py:
ROWS, COLS = 16, 16 # Grid dimensions
N_BOMBS = 40 # Number of bombsContributions are welcome! Feel free to submit a Pull Request.
- Inspired by the classic Windows Minesweeper game
- Built with pygame for rendering
