Write a C++ program that places mines in the 20 X 20 array and display it with t
ID: 3759734 • Letter: W
Question
Write a C++ program that places mines in the 20 X 20 array and display it with the number of mines in the 8 surrounding areas.
Section 1 Set a random number seed with srand(2461).
Section 2 Place mines in your field. Around 10 % of the field should be mines.
Section 3 Find the number of mines in the 8 surrounding areas.
Section 4 Display the mine field. Mine should be represented by “#” and other areas should be shown with the number of mines nearby.
Also, include a flowchart.
Output Example:
C:Windowslsystem32cmd.exe 0001#100000001111110 00011100000001#22#21 000000000000123#22#1 1 1 0 0 0 0 0 0 0 0 1 1 2 # 2 1 1 2 2 2 # 2 1 1 0 0 0 0 0 0 1 #212111#1 12#100000011101#1111 01111 2 2 100 0 000 111 0 0 0 11101##1000000001110 1#212221000000001#10 222#2221000012211110 # 1 1 1 2 # # 1 0 0 0 1 2 # # 20000 221012210001#4#20000 2 # 1 0 0 0 0 0 1 1 1 123210011 # 2 2 1 1 0 0 0 1 # 1 0 1 # 1 1112# 222#101121101223#23# 2#32201#100001#3#22# #22#1011100001121222 1 1 1 1 2 1 2 1 1 0 0 0 1 1 1 0 0 1 # 1 00001#2#10002#200111 0000112110002#200000 Press any key to continueExplanation / Answer
#include <iostream>
#define FIELD_SIZE 20
using namespace std;
int main()
{
int x, y;
char MineField[FIELD_SIZE][FIELD_SIZE];
for(int i = 0; i < FIELD_SIZE; i++)
for(int j = 0; j < FIELD_SIZE; j++)
MineField[i][j] = '.';
srand(time(0)); //Set a random number seed.
for(int i = 0; i < FIELD_SIZE * FIELD_SIZE * 0.1; i++) //Placing mines in 10% of the field.
{
x = rand() % FIELD_SIZE;
y = rand() % FIELD_SIZE;
MineField[x][y] = '#';
}
int count = 0;
if(MineField[0][0] != '#') //Counting Mines for top left corner cell.
{
if(MineField[0][1] == '#')
count++;
if(MineField[1][1] == '#')
count++;
if(MineField[1][0] == '#')
count++;
MineField[0][0] = 48+count;
}
for(int j = 1; j < FIELD_SIZE - 1; j++) //For the first row, other than the extreme cells.
{
count = 0;
if(MineField[1][j-1] == '#')
count++;
if(MineField[1][j] == '#')
count++;
if(MineField[1][j+1] == '#')
count++;
if(MineField[0][j-1] == '#')
count++;
if(MineField[0][j+1] == '#')
count++;
if(MineField[0][j] != '#')
MineField[0][j] = 48+count;
}
count = 0;
if(MineField[0][FIELD_SIZE - 1] != '#') //For the top right corner cell.
{
if(MineField[0][FIELD_SIZE-2] == '#')
count++;
if(MineField[1][FIELD_SIZE-2] == '#')
count++;
if(MineField[1][FIELD_SIZE-1] == '#')
count++;
MineField[0][FIELD_SIZE-1] = 48+count;
}
for(int i = 1; i < FIELD_SIZE - 1; i++) //For the cells from second row to penultimate row.
{
if(MineField[i][0] != '#') //For the first cell in each row.
{
count = 0;
if(MineField[i-1][0] == '#')
count++;
if(MineField[i+1][0] == '#')
count++;
if(MineField[i-1][1] == '#')
count++;
if(MineField[i][1] == '#')
count++;
if(MineField[i+1][1] == '#')
count++;
MineField[i][0] = 48+count;
}
for(int j = 1; j < FIELD_SIZE - 1; j++) //For the remaining cells.
{
count = 0;
if(MineField[i][j] == '#')
continue;
if(MineField[i-1][j-1] == '#')
count++;
if(MineField[i-1][j] == '#')
count++;
if(MineField[i-1][j+1] == '#')
count++;
if(MineField[i][j-1] == '#')
count++;
if(MineField[i][j+1] == '#')
count++;
if(MineField[i+1][j-1] == '#')
count++;
if(MineField[i+1][j] == '#')
count++;
if(MineField[i+1][j+1] == '#')
count++;
MineField[i][j] = 48+count;
}
if(MineField[i][FIELD_SIZE-1] != '#') //For the last cell in each row.
{
count = 0;
if(MineField[i-1][FIELD_SIZE-1] == '#')
count++;
if(MineField[i+1][FIELD_SIZE-1] == '#')
count++;
if(MineField[i-1][FIELD_SIZE-2] == '#')
count++;
if(MineField[i][FIELD_SIZE-2] == '#')
count++;
if(MineField[i+1][FIELD_SIZE-2] == '#')
count++;
MineField[i][FIELD_SIZE-1] = 48+count;
}
}
count = 0;
if(MineField[FIELD_SIZE-1][0] != '#') //For bottom left corner cell.
{
if(MineField[FIELD_SIZE-2][0] == '#')
count++;
if(MineField[FIELD_SIZE-2][1] == '#')
count++;
if(MineField[FIELD_SIZE-1][1] == '#')
count++;
MineField[FIELD_SIZE-1][0] = 48+count;
}
for(int j = 1; j < FIELD_SIZE - 1; j++) //For the last row, other than the extreme cells.
{
count = 0;
if(MineField[FIELD_SIZE-1][j-1] == '#')
count++;
if(MineField[FIELD_SIZE-1][j+1] == '#')
count++;
if(MineField[FIELD_SIZE-2][j-1] == '#')
count++;
if(MineField[FIELD_SIZE-2][j] == '#')
count++;
if(MineField[FIELD_SIZE-2][j+1] == '#')
count++;
if(MineField[FIELD_SIZE-1][j] != '#')
MineField[FIELD_SIZE-1][j] = 48+count;
}
count = 0;
if(MineField[FIELD_SIZE-1][FIELD_SIZE-1] != '#') //For the bottom right corner cell.
{
if(MineField[FIELD_SIZE-2][FIELD_SIZE-1] == '#')
count++;
if(MineField[FIELD_SIZE-2][FIELD_SIZE-2] == '#')
count++;
if(MineField[FIELD_SIZE-1][FIELD_SIZE-2] == '#')
count++;
MineField[FIELD_SIZE-1][FIELD_SIZE-1] = 48+count;
}
for(int i = 0; i < FIELD_SIZE; i++) //Displaying MineField.
{
for(int j = 0; j < FIELD_SIZE; j++)
printf("%c ",MineField[i][j]);
printf(" ");
}
printf("Press any key to continue. . . . ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.