Well, this article is relating to my last article on tictactoe game and this is because of similar logic being used.
The objective of the game is to clear a board that contains hidden "mines" or "bombs" without detonating any of them, and they are randomly placed in a 9x9 grid (beginners level). On every right opeing there's a clue which indicate how many bombs are in the adjacent cells.
At any point, you can perform 2 actions:
Once you have revealed all cells that do not contain a bomb, you win the game. Also before going forward it is recommended that you understand how to play Minesweeper practically! and also understand the concept of bitwise operations as this article would rely heavily on it.
Given a grid of N rows and M columns to play the game, we'd represent this grid as bits. We'd denote each cell as an 7-bit integer and here is the data that'd be stored:
Note: We can use bitwise operations to extract or set relevant bits. The relevant masks would be defined later in this article.
By default, the game is played on a 9x9 grid and a maximum of 10 bombs. However, you can change these later and I'd include settings to easily make changes. As a brief guidline, the standard levels of Minesweeper are:
Beginner | Intermediate | Expert | |
---|---|---|---|
Grid size | 9 x 9 | 16 x 16 | 16 x 30 |
Max bombs | 10 | 40 | 90 |
So here is a visual example of a 9 x 9 grid and how it'd be represented in bits.
So firstly, we need a way to generate a game and add bombs at random; and here is how we'd implement this in python.
I'd define certain constants which would be used through out this code.
And here defining a simple Minesweeper class to simply help hold some property variables and also making it easy to tweek.
On getting this far, you might be able to picture some of the diagrams prevously drawn, in code. Though not the full picture yet but very soon you'd get it. Since evey game needs a bomb and clues, we'd need to generate these randomly. Here is how we'd implement it:
The function placeBomb
places a bomb in a random cell. If you look closely you'd see a little recursion which makes sure that a bomb is place in a unique cell, since there is a possiblity of a random cell being generated more than once. So from the above code we can only generate a grid with bombs on it.
Clues indicate how many bombs are in the adjacent cells. So we'd need to know the cells adjacent if a particular cell and also count how many of these cells are bombs.
Solving this problem is far simpilar using a 2d array since you can easily do math on the (row, col). Here each cell is represented differently, in other words each cell has an id
like so:
If we want the adjacent cells for cell 30
which from the above grid would look like:
To get the top cells, We'd need to check if the current row isn't the first row since there is noting above the first row. Also you'd realize that the top cell is just the current_cell - number_of_columns
in this case the top cell of 30
is 21
which is 30 - 9
and the bottom cell of 30
is current_cell + number_of_columns
which in this case is 39
. You'd also realize that the diagonal cells are the cells by the sides of the top and bottom cell. So to get the top diagonals we'd just minus and add 1 to the top cell, also to get the bottom
diagonals we'd minus and add 1 to the bottom cell.
Here I would define a property function that gets adjacent cells:
Now, I think you understand how the adjacent cells are gotten. We can now move forward with adding the clues.
Running the current code here is what we have to far.
I think the game is a little bit ready and it's not complete if a play can't make a move. So basically a player should be able to flag a cell and also reveal a cell. The functions here are just basically setting and checking bits. In this case of flag
its setting the 7th bit.
The full code is available on github.
Final Project: Minesweeper
Assignment 1: minesweeper, MIPS minesweeper
Implementation of Minesweeper Game
How to program MineSweeper in Python
Optimized tic-tac-toe game in Solidity
Bit Operations
If you found this helpful, consider following me on Twitter @0xosas.