Seçkin Poyraz
M.D. & Full-Stack Web Developer

How to create a Minesweeper board in C++?

published on 11.05.2020, by @seckinthepoyraz

Hello everyone, today I am going to show you how to create a Minesweeper game-board in C++ programming language. This tutorial will cover up the basic algorithm of the game but will not cover how to make it playable or user-interactable.

So let's start with basic: What is Minesweeper?

Minesweeper is a historical single-player puzzle game, firstly introduced around 1960s. The objective of the game is to clear a rectangular board containing hidden "mines" or bombs without detonating any of them, with help from clues about the number of neighboring mines in each field.

2 bombs in the board and the neighboring fields around it

As you can see in the picture above, numbers are showing us how many bombs there are, in the neighbour fields.

So what we have to do is:

  • Firstly, we will create a 2D vector in our code with rows and columns given by the user.
  • Then, we will create random bombs with the number of bombs given by the user again.
  • After that, we will add the numbers around each bomb.
  • Lastly, we will show the board in the command prompt.

Let's create our Minesweeper Class in C++. We have 2 files: Minesweeper.h and Minesweeper.cpp

Minesweeper.h

We have created our main class and the prototypes of the methods. Now we will go step by step to create our methods in Minesweeper.cpp

Minesweeper.cpp (constructor and destructor)

We have declared the constructor for our class and initialize the private variables with the values given by the user. After that, we have told our program to run the methods below in order. Now let's take a look at these methods.

Minesweeper.cpp (create_table)

We create the table by resizing the vector with the values given by the user. Now it's time to add some random bombs.

Minesweeper.cpp (add_bombs)

This algorithm is designed by me so let me explain. First, we create a vector of integers that is size of the table. Let's assume the size is 20 rows * 10 columns = 200 Then our vector of integers should look: [1,2,3,4....200]

Then we will shuffle this vector with the shuffle function. And we will seed the shuffle function with the system clock so that it runs correctly. After the shuffling, let's assume the vector is now: [20,32,50,120,153,....1,12]

Now user tells us that he needs 3 bombs for example. So we get the first 3 values of the vector, which is [20,32,50] in this case. Now we have bombs at 20th, 32nd and 50th squares. Simple as that. In the next method, we will see how to get the row and the column of these squares.

Minesweeper.cpp (find_column/find_row)

This is simple math here. We have to find the column at first, otherwise we cannot find the row.

find_column formula is: the remainder of (square/total_column) But if the square is equals to total_column which will give the output of 0, then we set the column of it to the total_column.

find_row formula is: [ (square-column) / total_column ] + 1

Minesweeper.cpp (add_numbers)

Here is where the magic happens. We are looping through each of the bombs in the vector of bombs. Then we calculate the row and the column of the bomb. After that, we add +1 at the neighbours of the bomb if the neighbour is not out of bounds.

Minesweeper.cpp (show_table)

In this method, we are printing the vector in the Command Prompt by checking if the square is a bomb, a number or empty. Then we also print out the bomb locations.

Now we are all set to run our program. We need main.cpp to run the program of course so let's create that.

main.cpp

We are performing an infinite loop with 3 seconds sleep time to see those brand-new randomly created boards.

output of the program

That's it! We have perfectly created our app. You can use this algorithm, implement it, change it or do whatever you'd like to do. I have this project on my Github Repository and you can fork it there too. As you might have guessed, I love Minesweeper. There is a Minesweeper app that I have created in Javascript. You can play it too.

Thank you for your time, and see you again in the next blog posts. Take care!