## What is Minesweeper ?

We play Minesweeper on a square board and we have to click on the board on the cells which do not have a mine. And obviously we don’t know where mines are. If a cell where a mine is present is clicked then we lose, else we are still in the game.

It might seem like a complete luck-based game (you are lucky if you don’t step over any mine over the whole game and unlucky if you have stepped over one). But this is not a complete luck based game. Instead you can win almost every time if you follow the hints given by the game itself

To kn

### Hints for Winning the Game

• When we click on a cell having adjacent mines in one or more of the surrounding eight cells, then we get to know how many adjacent cells have mines in them. So we can do some logical guesses to figure out which cells have mines.
• If you click on a cell having no adjacent mines (in any of the surrounding eight cells) then all the adjacent cells are automatically cleared, thus saving our time.
• So we can see that we don’t always have to click on all the cells not having the mines (total number of cells – number of mines) to win. If we are lucky then we can win in very short time by clicking on the cells which don’t have any adjacent cells having mines.

## Program

```#include<iostream>
#include<cstdlib>
#define ROW_SIZE 10// Board Height
#define COL_SIZE 10 // Board Width
#define MINE_COUNT 10 //No of Mines
using namespace std;
class MineSweeper{
private :
static const char MINE = 'M'; // Letter M indicate as Mine
char boardValue[ROW_SIZE][COL_SIZE];
bool isBoardEnabled[ROW_SIZE][COL_SIZE];
public :
MineSweeper(){
for(int x = 0; x < ROW_SIZE; x++){ //Initialze the Board with 0
for(int y =0; y < COL_SIZE; y++){
boardValue[x][y] = '0';
isBoardEnabled[x][y] = true; // here true means not visited location
}
}
generateRandomMine();
calculateNeighbourCount();
printBoardValue();
}
void makeZeroDisable(int x, int y){
if(boardValue[x][y] != '0'){
return ;
}
isBoardEnabled[x][y] = false;
if(x > 0 && y > 0 && boardValue[x - 1][y - 1] == '0' && isBoardEnabled[x-1][y-1]){//Up Left
makeZeroDisable(x - 1, y - 1);
}
if(x > 0 && boardValue[x - 1][y] == '0' && isBoardEnabled[x-1][y]){//Up
makeZeroDisable(x - 1, y);
}
if(x > 0 && (y + 1) < COL_SIZE && boardValue[x - 1][y + 1] == '0' && isBoardEnabled[x-1][y+1]){//Up Right
makeZeroDisable(x - 1, y + 1);
}
if((x + 1) < ROW_SIZE && (y + 1) < COL_SIZE && boardValue[ x + 1][y + 1] == '0' && isBoardEnabled[x+1][y+1]){ //Down Right
makeZeroDisable(x + 1, y + 1);
}
if((x + 1) < ROW_SIZE && boardValue[x + 1][y] == '0' && isBoardEnabled[x+1][y]){//Down
makeZeroDisable(x + 1, y);
}
if(y > 0 && (x + 1) < ROW_SIZE && boardValue[x + 1][y - 1] == '0' && isBoardEnabled[x+1][y-1]){//Down Left
makeZeroDisable(x + 1, y - 1);
}
if(y > 0 && boardValue[x][y - 1] == '0' && isBoardEnabled[x][y-1]){//Left
makeZeroDisable(x, y - 1);
}
if((y + 1) < COL_SIZE && boardValue[x][y + 1] == '0' && isBoardEnabled[x][y + 1]){//Right
makeZeroDisable(x, y + 1);
}
}
bool checkWin(){;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COL_SIZE; j++){
if(isBoardEnabled[i][j] != false && boardValue[i][j] != MINE){
return false;
}
}
}
return true;
}
void selectBoardValue(int xValue, int yValue){
if(boardValue[xValue][yValue] == MINE){
makeAllMineEnable();
printBoardValue();
cout << "\n\n-----You Lose-----\n-----Game Over-----\n\n\n";
exit(-1);
} else if(boardValue[xValue][yValue] != '0' && isBoardEnabled[xValue][yValue]){
isBoardEnabled[xValue][yValue] = false;
printBoardValue();
} else if(boardValue[xValue][yValue] == '0' && isBoardEnabled[xValue][yValue]){
makeZeroDisable(xValue, yValue);
printBoardValue();
}
if(checkWin()){
cout << "\n\nCongrats!! You Win\n\n";
exit(0);
}
}
void calculateNeighbourCount(){

for(int x = 0; x < ROW_SIZE; x++){
for( int y = 0; y < COL_SIZE; y++){
int neighbourCount = 0;
if(boardValue[x][y] != MINE){
if(x > 0 && y > 0 && boardValue[x - 1][y - 1] == MINE){//Up Left
neighbourCount++;
}
if(x > 0 && boardValue[x - 1][y] == MINE){//Up
neighbourCount++;
}
if(x > 0 && (y + 1) < COL_SIZE && boardValue[x - 1][y + 1] == MINE ){//Up Right
neighbourCount++;
}
if((x + 1) < ROW_SIZE && (y + 1) < COL_SIZE && boardValue[ x + 1][y + 1] == MINE){ //Down Right
neighbourCount++;
}
if((x + 1) < ROW_SIZE && boardValue[x + 1][y] == MINE){//Down
neighbourCount++;
}
if(y > 0 && (x + 1) < ROW_SIZE && boardValue[x + 1][y - 1] == MINE){//Down Left
neighbourCount++;
}
if(y > 0 && boardValue[x][y - 1] == MINE){//Left
neighbourCount++;
}
if((y + 1) < COL_SIZE && boardValue[x][y + 1] == MINE){//Right
neighbourCount++;
}
boardValue[x][y] = neighbourCount + 48; // add 48 to make number as a character
}
}
}

}
void generateRandomMine(){
int x = rand() % ROW_SIZE;
int y = rand() % COL_SIZE;
boardValue[x][y] = MINE;
for(int i = 1; i < MINE_COUNT; i++){
while(1){
x = rand() % ROW_SIZE;
y = rand() % COL_SIZE;
if(boardValue[x][y] != MINE)
break;
}
boardValue[x][y] = MINE;
}
}
void printBoardValue()
{
system("CLS");
cout << "Letter 'M' Indicate as MINE\nBoard Size : " << ROW_SIZE << " x " <<COL_SIZE;
cout << "\nTotal No Of MINES : " << MINE_COUNT <<"\n\n*";
for(int j = 0; j < COL_SIZE; j++){
cout << "   "<<j;
}
cout <<"\n---";
for(int j = 0; j < COL_SIZE; j++){
cout << "----";
}
cout <<"\n";
for(int i = 0; i < ROW_SIZE; i++){
cout << i << "|  ";
for(int j = 0; j < COL_SIZE; j++){
if( !(isBoardEnabled[i][j]) ){
cout << boardValue[i][j] << "   ";
} else {
cout << "x   ";
}
}
cout << "\n";
}
}
void makeAllMineEnable() {
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COL_SIZE; j++){
if(boardValue[i][j] == MINE){
isBoardEnabled[i][j] = false;
}
}

}
}
};
int main(){
MineSweeper playerNo1;
int row,column;
while(1){
cout << "\n\nEnter Your Move (Row , Column): ";
cin >> row >> column;
playerNo1.selectBoardValue(row, column);
}
return 0;
}```

### OUTPUT SCREEN :

#### Initial Board Look ### In Between the Game ### Game Over ### Game Winning ## You Might Also Like….

Find Straight City | Goldman Sachs

My Zoho Interview Experience | August 2019 ### Hari Prasath R

You know who I am :)