In: Computer Science
Hey! I'm stuck with this task from the Princeton course on Coursera.
Write a program Minesweeper.java that takes three integer
command-line arguments m, n, and k and prints an m-by-n grid of
cells with k mines, using asterisks for mines and integers for the
neighboring mine counts (with two space characters between each
cell). To do so,
Generate an m-by-n grid of cells, with exactly k of the mn cells
containing mines, uniformly at random.
For each cell not containing a mine, count the number of
neighboring mines (above, below, left, right, or diagonal).
I don't really know how to place the mines randomly in the two dimensional array that is the grid, nor how to count the number of neighbouring mines. I've been stuck for days now. Could someone help?
Hello here is the code for the above problem. Hope it helps. I have added comments for better understanding. If you are satisfied with the answer, do consider upvoting!
Minesweeper.java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
//returns true if the point is within bounds and has
arr has '*' as value at that x,y.
public static boolean issafe(char arr[][],int x,int
y,int n,int m)
{
return x>=1 && x<=n
&& y>=1 && y<=m && arr[x][y] ==
'*';
}
//finds number of neighbours which are '*'.
public static int findNeighbours(char arr[][],int
x,int y,int n,int m)
{
int count = 0;
if(issafe(arr,x-1,y,n,m))
count++;
if(issafe(arr,x-1,y+1,n,m))
count++;
if(issafe(arr,x,y+1,n,m))
count++;
if(issafe(arr,x+1,y+1,n,m))
count++;
if(issafe(arr,x+1,y,n,m))
count++;
if(issafe(arr,x+1,y-1,n,m))
count++;
if(issafe(arr,x,y-1,n,m))
count++;
if(issafe(arr,x-1,y-1,n,m))
count++;
return count;
}
public static void main(String[] args) {
//using reader to take n,m,k as
input;
Scanner reader = new
Scanner(System.in);
//used to generate random
numbers.
Random r = new Random();
System.out.println("Enter
n");
int n = reader.nextInt();
System.out.println("Enter
m");
int m = reader.nextInt();
System.out.println("Enter
k");
int k = reader.nextInt();
//defining 2-d matrix
arr[1-n][1-m];
char arr[][] = new
char[n+1][m+1];
int tmp = k;
int row_num = r.nextInt(n) +
1;
int column_num = r.nextInt(m) +
1;
//generating k mines
randomly.
while(tmp>0)
{
//keep
generating row and column numbers till the time we have
//a position
which does not contain '*'.
while(arr[row_num][column_num]=='*')
{
//generates random number between [1-n].
row_num = r.nextInt(n)+1;
//generates random number between [1-m].
column_num = r.nextInt(m)+1;
}
//setting
generated position as '*'.
arr[row_num][column_num] = '*';
tmp--;
}
for(int i=1;i<=n;i++)
{
for(int
j=1;j<=m;j++)
{
if(arr[i][j]!='*')
{
//function returns the number
of neighbours which are '*';
int count =
findNeighbours(arr,i,j,n,m);
//converts the number to char
value. Ex: 2 is converted to '2';
arr[i][j] =
(char)(count+48);
}
}
}
//printing the resulting 2-d
matrix.
System.out.println("Final
result:");
for(int i=1;i<=n;i++)
{
for(int
j=1;j<=m;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT: