This project implements the classic Minesweeper game with an AI assistant that can make intelligent moves based on logical inference. The game is built with Python and uses PyGame for the graphical interface.
Minesweeper is a single-player puzzle game where the objective is to clear a rectangular board containing hidden mines without detonating any of them. The player uses clues about the number of neighboring mines in each field to determine where the mines are.
This implementation includes a graphical interface and an AI assistant that can make intelligent moves based on logical deduction. You can see the knowledge building and in the console output as the AI makes a move.
The project consists of two main Python files:
-
minesweeper.py
- Contains the core game logic and AI implementation:Minesweeper
class - Manages the game state and rulesSentence
class - Represents logical statements for the AIMinesweeperAI
class - Implements the AI logic using knowledge-based approach
-
runner.py
- Handles the game's graphical interface using Pygame:- Manages user input
- Renders the game board
- Coordinates between the game logic and AI
The game follows standard Minesweeper rules with a slight modification: diagonal cells are not considered neighbors.
- Game Board: An 8x8 grid with 8 mines randomly placed
- Cell States:
- Hidden (default)
- Revealed (showing the number of adjacent mines)
- Flagged (marked by the player as a potential mine)
- Game End Conditions:
- Win: All mines are correctly flagged
- Lose: A mine is revealed
The AI uses a knowledge-based approach with propositional logic to make safe moves whenever possible.
The AI's knowledge is represented as a collection of sentences, where each sentence consists of:
- A set of cells
- A count of how many mines are in those cells
- Basic Knowledge: When a cell is revealed, the AI creates a sentence about its neighboring cells
- Inference Rules:
- If count = 0, all cells in the set are safe
- If count = size of the set, all cells in the set are mines
- If set1 ⊆ set2, then (set2 - set1) = (count2 - count1)
- Safe Move: Choose a cell known to be safe
- Random Move: If no safe move is available, make a random move among cells not known to be mines
The Minesweeper
class handles the game logic:
- Initialization: Sets up the board with randomly placed mines
- Cell Evaluation: Determines if a cell is a mine
- Neighboring Mines: Calculates how many mines are adjacent to a cell
- Win Condition: Checks if all mines have been correctly flagged
The Sentence
class represents logical statements for the AI:
- Structure: A set of cells and a count of mines within those cells
- Inference: Methods to determine known mines and safe cells
- Updates: Methods to update knowledge when cells are marked as mines or safe
The MinesweeperAI
class implements the AI's decision-making process:
- Knowledge Base: Maintains a list of sentences
- Knowledge Updates: Adds new information when a cell is revealed
- Inference Engine: Applies logical deduction to identify safe cells and mines
- Decision Making: Methods to make safe moves or random moves
add_knowledge(cell, count)
: Updates the AI's knowledge when a cell is revealedmark_safe(cell)
: Marks a cell as safe and updates all knowledge accordinglymark_mine(cell)
: Marks a cell as a mine and updates all knowledge accordinglymake_safe_move()
: Returns a known safe move if availablemake_random_move()
: Returns a random move if no safe move is available
If we have two sentences:
{A, B, C} = 1
{A, B} = 1
We can infer:
{C} = (1 - 1) = 0, meaning C is safe
This implementation demonstrates how propositional logic can be used to create an AI that plays Minesweeper intelligently, making safe moves whenever possible based on logical deduction.
- Install the requirements with
pip install -r requirements.txt
- Run the game with
python runner.py
- Start the Game: Run
python runner.py
to launch the game - Controls:
- Left-click a cell to reveal it
- Right-click a cell to flag/unflag it as a mine
- Click "AI Move" to let the AI make a move 9
- Click "Reset" to start a new game
- Objective: Mark all mines with flags without revealing any mines