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
  • Write Java program that takes an integer N from the command line and creates N by...

    Write Java program that takes an integer N from the command line and creates N by N boolean array a[][] such that a[i][j] is true if i and j are relatively prime (have no common factors) and false otherwise. print the output using * to represent true and a space to represent false and include row and column numbers. (use sieving)

  • Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer...

    Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer array of length n, where each value is between is 1 and n, and displays true if there are any duplicate values, false otherwise. CODE IN JAVA and NO IMPORT STATEMENTS CAN BE USED

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • 1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether...

    1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether there is a command line argument of “-amount” followed by an amount number. It will print out that number amount. For all other cases, it will print out -1. For example, if you run this program with correct arguments as follows, it will print out 1.99, 0.75 and 1.1 respectively CheckAmount -amount 1.99 CheckAmount -help -amount 0.75 CheckAmount -help -check -amount 1.1 -verbose And...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write a full program that will accept exactly  three command line arguments that can be interpreted as...

    Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program. Suppose your program is called Add.java. Then running the command java Add 1 2 3 will output 6, and running java Add, for example, will cause the program to shut down. Consider the following recursive method: public static int mystery (int n)...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • In C please Create a program that takes two integers as command line arguments (num, length)...

    In C please Create a program that takes two integers as command line arguments (num, length) and outputs a list of the first length multiples of num. num should be included in the returned array. For example: ./a.out 7 5 -> [7, 14, 21, 28, 35] ./a.out 12, 10 -> [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] ./a.out 17, 6 -> [17, 34, 51, 68, 85, 102] Ma Word starts with a vowel add "yay" to the...

  • Write a C or C++ program A6p1.c[pp] that accepts two command line arguments n and m where n is an integer between 2 and 6 inclusive and m can be assumed to be a multiple of 60 (e.g. 60,120,etc). Generate a string of m random upper case English characters

    Write a C or C++ program A6p1.c[pp] that accepts two command line arguments n and m where n is an integer between 2 and 6 inclusive and m can be assumed to be a multiple of 60 (e.g. 60,120,etc). Generate a string of m random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string of m chars in place into an off-by-one lower case version (i.e. ‘A’→’b’, ‘B’→’c’, ‘C’→’d’,…, ‘Y’→’z’, ‘Z’→’a’). You should divide this conversion task among the n threads as evenly as possible. Print out the string both...

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