Question

Write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an...

Write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an M-by-N boolean array where each entry is occupied with probability p. In the minesweeper game, occupied cells represent bombs and empty cells represent safe cells. Print out the array using an asterisk for bombs and a period for safe cells. Then, replace each safe square with the number of neighboring bombs (above, below, left, right, or diagonal) and print out the solution. Try to write the code so that you have a few special cases as possible to deal with, by using an (M+2)-by-(N+2) boolean array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is:

import java.util.*;

public class Minesweeper// class declaration
{
private int m,n;// m for length and n for width
private float p;// for storing prob

private boolean[][] map;// boolean array for storing the map

public void setValues(int len, int wid, float prob)// setting the values of m, n, p and initializing the map
{
m = len;
n = wid;
p = prob;

map = new boolean[m+2][n+2];

for(int i=0; i<m+2; i++)// initially setting all the values to false
{
for(int j=0; j<n+2; j++)
map[i][j] = false;
}

fillArray();

}

private void fillArray()// method to fill the array with the probability p
{
Random r = new Random();// for generating random number
for(int i=1; i<m+1; i++)
{
for(int j=1; j<n+1; j++)
{
if(r.nextFloat() < p)// comparing whether the generated random number is less than p or not
map[i][j] = false;
else
map[i][j] = true;
}
}
}

private int getNumBombs(int i, int j)// method to return the number of bombs surrounding a safe square
{
int count = 0;

if(map[i-1][j])// upper
count++;
if(map[i+1][j])// lower
count++;
if(map[i][j-1])// left
count++;
if(map[i][j+1])// right
count++;
if(map[i-1][j-1])// upper left
count++;
if(map[i+1][j+1])// down right
count++;
if(map[i-1][j+1])// upper right
count++;
if(map[i+1][j-1])// lower left
count++;

return count;

}

public void printArray()// method to print array
{
for(int i=1; i<m+1; i++)
{
for(int j=1; j<n+1; j++)
{
if(map[i][j])
System.out.print("* ");
else
System.out.print(". ");
}
System.out.println();
}
System.out.println();

for(int i=1; i<m+1; i++)
{
for(int j=1; j<n+1; j++)
{
if(map[i][j])// if it's a bomb then print *
System.out.print("* ");
else
System.out.print(getNumBombs(i,j) + " ");// else print the number of bombs around
}
System.out.println();
}
System.out.println();

}

public static void main(String[] args)
{
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);

float p = Float.parseFloat(args[2]);

Minesweeper m1 = new Minesweeper();

m1.setValues(m , n , p);
m1.printArray();

}

}

Add a comment
Know the answer?
Add Answer to:
Write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT