# The Weekly Challenge 126: Minesweeper Game (C++ Solutions)

Part 2 of The Weekly Challenge 126: You are given a rectangle with points marked with either x or *. Please consider the x as a land mine. Write a script to print a rectangle with numbers and x as in the Minesweeper game.

```#include <cstdlib>
#include <iostream>

/*
* You are given a rectangle with points
* marked with either x or *. Consider
* the x as a land mine. Write a script
* to print a rectangle with numbers
* and x as in the Minesweeper game.
*/

class MinesweeperGame{
private:
char** initialize_grid(int, int);
public:
void make_print_grid(int, int);
};

char** MinesweeperGame::initialize_grid(int m, int n){
char** grid = new char*[m];
for(int i = 0; i < m; i++){
grid[i] = new char[n];
for(int j = 0; j < n; j++){
grid[i][j] = '*';
if(rand() / double(RAND_MAX) < 1 / 3.0)
grid[i][j] = 'x';
}
}
return grid;
}

void MinesweeperGame::make_print_grid(int m, int n){
char** grid = initialize_grid(m, n);
std::cout << "Input:" << std::endl;
for(int i = 0; i < m; i++){
std::cout << "\t";
for(int j = 0; j < n; j++){
std::cout << grid[i][j] << " ";
}
std::cout << std::endl;
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == '*'){
int mine_count = 0;
if(i >= 1 && j >= 1 && grid[i - 1][j - 1] == 'x')
mine_count++;
if(i >= 1 && grid[i - 1][j] == 'x')
mine_count++;
if(i >= 1 && j < n - 1 && grid[i - 1][j + 1] == 'x')
mine_count++;
if(j >= 1 && grid[i][j - 1] == 'x')
mine_count++;
if(j < n - 1 && grid[i][j + 1] == 'x')
mine_count++;
if(i < m - 1 && j >= 1 && grid[i + 1][j - 1] == 'x')
mine_count++;
if(i < m - 1 && grid[i + 1][j] == 'x')
mine_count++;
if(i < m - 1 && j < n - 1 && grid[i + 1][j + 1] == 'x')
mine_count++;
char temp[1];
sprintf(temp, "%d", mine_count);
grid[i][j] = *temp;
}
}
}
std::cout << "Output:" << std::endl;
for(int i = 0; i < m; i++){
std::cout << "\t";
for(int j = 0; j < n; j++){
std::cout << grid[i][j] << " ";
}
std::cout << std::endl;
}
}

int main(int argc, char** argv){
srand(time(NULL));
int m = atoi(argv[1]);
int n = atoi(argv[2]);
MinesweeperGame mn;
mn.make_print_grid(m, n);
}
```

Example output:
```\$ g++ -o cxx/ch-2 cxx/ch-2.cxx
\$ ./cxx/ch-2 5 10
Input:
* * * * * * x * * *
* x * x * * * x x x
x * x x * * x * * x
x * x * * * x * * x
x * x * * * x * * *
Output:
1 1 2 1 1 1 x 3 3 2
2 x 4 x 2 2 3 x x x
x 5 x x 2 2 x 4 5 x
x 6 x 4 1 3 x 3 2 x
x 4 x 2 0 2 x 2 1 1
```

### Recent Posts from This Journal

• #### A Simple Binary Tree in Perl

This Binary Tree implementation uses Class::Struct and only defines find() and insert() operations, along with a simple print function for…

• #### The Weekly Challenge 127: Conflict Intervals (C++ Solutions)

Part 2 of The Weekly Challenge 127: You are given two sets with unique numbers. Write a script to figure out if they are disjoint. #include

• #### The Weekly Challenge 127: Disjoint Lists (C++ Solutions)

Part 1 of The Weekly Challenge 127: You are given two sets with unique numbers. Write a script to figure out if they are disjoint. #include

Comments for this post were disabled by the author