This project is a Python implementation of the classic Minesweeper game, enhanced with an AI-powered solver. The AI uses logical reasoning to deduce the positions of mines and safe cells. The game is designed with a graphical interface created using Pygame.
-
Classic Minesweeper Gameplay:
- Reveal cells to find mines.
- Right-click to flag cells as mines.
- Avoid detonating a mine to win the game.
-
AI Solver:
- The AI uses a knowledge base and logical reasoning to identify safe cells or mines.
- Incorporates:
- Safe move deduction: Based on known information, marks certain cells as safe.
- Random move fallback: If no logical move is available, the AI makes a random move.
- Inference system: The AI learns from every revealed cell and updates its knowledge base.
-
Graphical User Interface:
- Interactive grid for gameplay.
- Visual elements for flags, mines, and revealed cells.
- Dynamic buttons for:
- Autoplaying with AI.
- Showing AI inferences (safe and mine predictions).
- Resetting the game.
-
Autoplay:
- Watch the AI automatically solve the Minesweeper board step-by-step.
- Python 3.x
- Install the following Python libraries:
pip install pygame
minesweeper.py: Contains the game logic and AI logic.main.py: Runs the graphical interface using Pygame.- Assets:
- Fonts: Located in
assets/fonts/(e.g.,OpenSans-Regular.ttf). - Images: Located in
assets/images/(e.g.,flag.png,mine.png).
- Fonts: Located in
-
Clone or Download the Repository:
- Clone the repository or download it as a zip file.
-
Navigate to the Project Directory:
cd <project-directory>
-
Run the Game:
python runner.py
Mark all the mines on the board without triggering any of them.
- Left-Click: Reveal a cell.
- Right-Click: Flag a cell as a mine.
- Buttons:
- Autoplay: Lets the AI take over and solve the board.
- AI Move: Allows the AI to make a single move.
- Reset: Resets the game to start over.
- Show Inference: Highlights AI's safe and mine predictions.
- Flag all mines correctly without triggering any.
- Clicking on a mine detonates it, ending the game.
-
Knowledge Base:
- The AI maintains a set of logical sentences about the game board.
- Each sentence represents a relationship between a group of cells and the number of mines in them.
-
Inference:
- The AI deduces:
- Safe cells: Cells that can be revealed without risk.
- Mines: Cells that must contain mines.
- It uses techniques like subset reasoning to infer new information.
- The AI deduces:
-
Fallback:
- When no safe move can be logically deduced, the AI chooses a random unexplored cell.
-
MinesweeperClass:- Handles the game board and mine placement.
- Tracks the number of nearby mines for any cell.
- Provides utility methods for checking mines and determining win/loss states.
-
SentenceClass:- Represents logical statements about cells and their mine counts.
- Provides methods for marking cells as safe or mines, enabling the AI to refine its knowledge base.
-
MinesweeperAIClass:- Implements the AI logic to deduce safe cells and mines.
- Maintains knowledge of all moves made, safe cells, and mines.
- Dynamically updates its knowledge base with each new revelation.
-
Game Setup:
- Initializes the Minesweeper board and AI.
- Loads assets (e.g., images and fonts) for rendering.
-
Graphical User Interface:
- Displays the Minesweeper board, cells, and interactive buttons.
- Highlights flagged cells, revealed cells, and inferred safe/mine cells.
-
Gameplay Loop:
- Handles user input for cell clicks and button presses.
- Integrates AI to make safe or random moves during autoplay.
📂 Project Folder
├── 📂 assets
│ ├── 📂 fonts
│ │ └── OpenSans-Regular.ttf
│ ├── 📂 images
│ ├── flag.png
│ ├── mine.png
│ ├── mine-red.png
├── .gitignore
├── minesweeper.py
├── README.py
└── runner.py
- Add support for customizable grid sizes and mine counts through a user interface.
- Improve AI inference for edge cases with low mine densities.
- Include a leaderboard to track game completions and performance metrics.
- Pygame: For creating an intuitive library to design 2D games.
- Inspiration: The classic Minesweeper game and AI algorithms based on logical reasoning.