Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[1] In this milestone, implement the bodies of the following methods within the MineSweeper class. The following are methods that should be used: public static

[1] In this milestone, implement the bodies of the following methods within the MineSweeper class.

The following are methods that should be used:

public static int placeMines(boolean[][] mines, Random randGen) public static int numNearbyMines( boolean [][]mines, int row, int col) public static void showMines(char[][] map, boolean[][] mines) public static boolean allSafeLocationsSwept(char[][] map, boolean[][] mines) 

The algorithm for the main method should change to call these new methods.

a. Create one instance of Random that is used for all games. Set the seed to be the SEED constant found in Config.java for reproducible results which are useful for testing and grading.

b. Create a 2 dimensional boolean array, the same size as the map, that will store whether there is a mine at each location. A true value at a location indicates that location has a mine.

c. Create a game loop that continues until the player either wins or loses.

i. Output the number of mines in the map

ii. After getting a location to sweep, if there is a mine

Then update the map with SWEPT_MINE.

Call showMines to show all the mines on the map.

Print out the simple map and the "Sorry" message.

iii. Otherwise, no mine in the location then

Determine how many nearby mines there are.

Display that number as a char in the map.

If allSafeLocationsSwept then show, print out the simple map and the "You Win!" message.

Here is the code I have already completed for the first part of the program (Hopefully all the variable will tie in together):

import java.util.Scanner;

import java.util.Random;

//Creates class named MineSweeper//

public class MineSweeper {

Scanner in = new Scanner(System.in);

int width = 0;

/**

* This is the main method for Mine Sweeper game!

* This method contains the within game and play again loops and calls

* the various supporting methods.

*

* @param args (unused)

*/

public static void main(String[] args) {

String prompt = "";

Scanner in = new Scanner(System.in);

int width = 0;

int height = 0;

System.out.println("Welcome to Mine Sweeper!");

width = promptUser(in, "What width of map would you like (3 - 20): ", 3, 20); height = promptUser(in, "What height of map would you like (3 - 20): ", 3, 20);

char[][] map = new char[height][width];

eraseMap(map);

simplePrintMap(map);

int row = promptUser(in, "row: ", 1, height); int col = promptUser(in, "column: ", 1, width);

map[row - 1][col - 1] = Config.NO_NEARBY_MINE;

simplePrintMap(map);

System.out.print("Would you like to play again (y/n)? ");

char ans = in.next().charAt(0);

while(ans == 'y');{

System.out.println("Thank you for playing Mine Sweeper!");

}

Random randGen = new Random(Config.SEED);

}

public static int promptUser(Scanner in, String prompt, int min, int max) {

int userInput = 0;

System.out.print(prompt);

if (in.hasNextInt()) {

userInput = in.nextInt();

}

while (userInput < min || userInput > max) {

System.out.println("Expected a number from " + min + " to " + max + ".");

userInput = in.nextInt();

}

return userInput;

}

public static void eraseMap(char[][] map) {

int i,j, row = map.length, col = map[0].length;

for(i = 0; i < row;i++) {

for(j = 0; j < col;j++)

map[i][j]='.';

}

}

public static void simplePrintMap(char[][] map) {

for(char[] row : map){

for(char val : row)

System.out.print(" " + val);

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

Students also viewed these Databases questions

Question

truth table of 1 bit full adder circuit

Answered: 1 week ago

Question

x-3+1, x23 Let f(x) = -*+3, * Answered: 1 week ago

Answered: 1 week ago

Question

=+ Who do you think is right? Why?

Answered: 1 week ago