An example of a game in progress 1A21 1 Assignment Your assignment will not invo
ID: 3935189 • Letter: A
Question
An example of a game in progress 1A21 1 Assignment Your assignment will not involve any game playing but you will be required to generate the underlying grid for a game of minesweeper. Your program should read three input values. The first two, m and n represent the dimensions. The game will be played on an m by n board. The third input, p, is a double value between 0 and 1 which represents the probability that a cell contains a mine. (Make sure you validate this input value) Your program should first produce an m by n grid where 1 indicates the presence of a mine and 0 indicate s a safe cell. For each cell, you should generate a random value between 0 and 1. If the value is less than p, you should place a mine in that cell. You should output this grid using an asterisk to indicate the presence of a bomb and a dot to indicate a safe cell. For example, if m 5, n 10 and p 0.3 the output could be as follows (it may differ depending on the random numbers generated):Explanation / Answer
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int nextBool(double probability)
{
return (rand() / (double)RAND_MAX) < probability;
}
char a[50][50];
int b[50][50];
int main(void) {
srand(time(NULL));
int m,n;
double p;
scanf("%d%d%lf",&m,&n,&p);
for(int i=0;i<m+2;i++)
for(int j=0;j<n+2;j++) {
a[i][j]='.';
b[i][j]=0;
}
for(int i=1;i<m+1;i++) {
for(int j=1;j<n+1;j++) {
int k = nextBool(p);
if(k==1) {
a[i][j]='`';
} else {
a[i][j]='.';
}
}
}
for(int i=1;i<m+1;i++) {
for(int j=1;j<n+1;j++) {
if(a[i][j]=='.')
{
if(a[i-1][j-1]=='`')
b[i][j]++;
else if(a[i-1][j]=='`')
b[i][j]++;
else if(a[i-1][j+1]=='`')
b[i][j]++;
else if(a[i][j-1]=='`')
b[i][j]++;
else if(a[i][j+1]=='`')
b[i][j]++;
else if(a[i+1][j-1]=='`')
b[i][j]++;
else if(a[i+1][j]=='`')
b[i][j]++;
else if(a[i+1][j+1]=='`')
b[i][j]++;
}
}
}
for(int i=1;i<m+1;i++) {
for(int j=1;j<n+1;j++) {
if(a[i][j]=='.') {
printf("%d",b[i][j]);
} else {
printf("*");
}
} printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.