Minesweeper Game with built-in rule-based artificial intelligence (AI). Made with Python (ver 3.10) and Tkinter module.
- 1 – board
- 2 – mode selector, available modes:
- 1 – easy mode, board size = 9×9, number of bombs = 10
- 2 – medium mode, board size = 16×16, number of bombs = 40
- 3 – hard mode, board size = 16×32, number of bombs = 99
- 3 – game reset button
- 4 – cells left to flag
- 5 – call the AI for help
For unopened cells:
- Left click = Open the cell
- Right click = Flag the cell
- Left click = Open the cells adjacent to the clicked one if the number of adjacent flags is the same as the number inside the clicked cell
- Left click = Remove the flag
- Right click = Remove the flag
The AI is like a player – it does not know all the right moves. It can give a safe move, a risky move, or a completely random move.
To find a safe move, the AI is checking if the opened cells satisfy the conditions:
- If the number of flagged cells is the same as the number inside the cell they are adjacent to, then other adjacent cells contain no bombs.
- If the number of unopened cells is the same as the number inside the cell they are adjacent to (minus the number of the adjacent opened cells), then all of these unopened cells contain bombs.
If a safe move cannot be found that way, the AI checks for intersections. Consider the example below:
It is clear that the cells x1, x2, x3 contain 2 bombs. Since x2 and x3 can only contain one bomb, x1 definitely contains a bomb.
If a safe move cannot be made then the AI considers all possible arrangements of bombs in the cell that are adjacent to the opened ones. Then it computes a weighted average of times when a bomb is inside each observed cell. If a cell contains a bomb in all possible arrangements, the cell is flagged. Else, the AI opens on the cell with the smallest probability of it containing a bomb.
At the beginning of the game, when no moves are made yet, the AI can only make a random move.
Note
The AI does not check if the player flagged the correct cell. A wrong move will affect all the further outputs.
The AI’s logic is based on the strategies described in “Minesweeper: A Statistical and Computational Analysis” by Andrew Fowler and Andrew Young.
It takes awhile for the AI to calculate all possible moves if the opened cells are too far apart.

