I need this real quick, and people who answered this same previously asked by ot
ID: 3729644 • Letter: I
Question
I need this real quick, and people who answered this same previously asked by other students, actually those answers are wrong. Help me out with right answer!
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 indicates 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):
* . . . . . . . . *
. . . . . . * . . .
. . . . . . . . * *
. . . * * * . . * .
. . . * . . . . . .
Next generate and output the grid where each safe cell contains the number of neighboring bombs (above, below, left, right or diagonal).
For the example above, the output would be:
* 1 0 0 0 1 1 1 1 *
1 1 0 0 0 1 * 2 3 3
0 0 1 2 3 3 2 3 * *
0 0 2 * * * 1 2 * 3
0 0 2 * 4 2 1 1 1 1
Hint: To reduce the number of special cases that have to be considered, you should use an (m+2) by (n+2) array of integers. Rows 0 and m+1 as well as columns 0 and n+1 should be boundary cases which never contain bombs and are never printed but allow for an easier way of counting the number of neighboring bombs.
Since we are required to fix the size of arrays at compile time, you should declare the arrays to be of some large fixed size, say 50 by 50. For any given input values, you will only use the appropriate portion of the array.
Explanation / Answer
Since no language has been mentioned. Adding in c.
#include <stdio.h>
#include <stdlib.h>
double random()
{
return (double)rand() / (double)RAND_MAX ;
}
int main(void) {
char grid[50][50];
int count[50][50];
int m,n,i,j;
m=5;
n=5;
double p,t;
p=0.5;
//scanf("%d,%d,%lf",&m,&n,&p);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
//printf("test");
t=random();
if(t<p){
grid[i][j]='*';
}else{
grid[i][j]='.';
}
printf("%c",grid[i][j]);
}
printf(" ");
}
//printf("first done ")
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(grid[i][j]=='*'){
count[i][j]=-1;
count[i-1][j]++;
count[i][j-1]++;
count[i-1][j-1]++;
count[i+1][j]++;
count[i][j+1]++;
count[i+1][j+1]++;
}
}
}
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(count[i][j]==-1){
printf("*");
}else{
printf("%d",count[i][j]);
}
}
printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.