Hello everyone! Today, I'm going to show you how to build a Minesweeper game board in C++. This tutorial covers the core algorithm behind the game, but will not cover making it playable or interactive.
Let's start with the basics: What is Minesweeper?
Minesweeper is a classic single-player puzzle game, first introduced in the 1960s. The objective is to clear a rectangular board containing hidden mines without detonating any of them, using numerical clues that indicate how many mines are adjacent to each cell.
As shown in the picture above, each number tells us how many bombs are present in the surrounding fields.
Here's what we need to do:
- First, we'll create a 2D vector with the number of rows and columns provided by the user.
- Then, we'll randomly place bombs according to the bomb count specified by the user.
- After that, we'll calculate and assign numbers to the cells surrounding each bomb.
- Finally, we'll display the board in the command prompt.
Let's create our Minesweeper Class in C++. We have 2 files: Minesweeper.h and Minesweeper.cpp
We've defined our main class along with the method prototypes. Now we'll go through each method step by step in Minesweeper.cpp.
We've defined the constructor, initialized the private variables with the user-provided values, and instructed the program to execute each method in order. Now let's take a closer look at those methods.
We initialize the table by resizing the vector using the dimensions provided by the user. Next, it's time to place some bombs at random.
This algorithm is one of my own design, so let me walk you through it. First, we create an integer vector with as many elements as there are cells on the board. Assuming a size of 20 rows × 10 columns = 200, the vector would look like: [1, 2, 3, 4 ... 200]
We then shuffle this vector using the shuffle function, seeded with the system clock to ensure randomness. After shuffling, the vector might look something like: [20, 32, 50, 120, 153, ... 1, 12]
If the user requests 3 bombs, we simply take the first 3 values — [20, 32, 50] — meaning bombs are placed at the 20th, 32nd, and 50th cells. Simple as that. In the next method, we'll see how to determine the row and column of each of those cells.
This is straightforward math. The column must be found first, as it's required to calculate the row.
The find_column formula is: the remainder of (square ÷ total_columns). However, if the square number is an exact multiple of total_columns — which would yield 0 — we set the column to total_columns instead.
The find_row formula is: [ (square − column) ÷ total_columns ] + 1
This is where the magic happens. We iterate over each bomb in the bombs vector, calculate its row and column, then increment by +1 all neighboring cells — as long as they're within the bounds of the board.
In this method, we print the board to the command prompt by checking whether each cell is a bomb, a number, or empty. We also print the bomb locations at the end.
We're all set to run the program. We just need a main.cpp to tie everything together.
We run an infinite loop with a 3-second delay between iterations so we can watch freshly generated random boards appear one after another.
And that's it! The board generator is complete. Feel free to use this algorithm, build on it, modify it, or take it in whatever direction you like. The full project is available on my GitHub repository — feel free to fork it. And if you're a Minesweeper fan like me, I also built a fully playable version in JavaScript. Give it a try!
Thank you for your time, and I'll see you in the next post. Take care!