Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Current Program looks like this: import java.util.Random; import java.util.Arrays; import java.util.Scanner; /** * This class contains the program to execute a text version of the

image text in transcribedCurrent Program looks like this:

import java.util.Random;

import java.util.Arrays;

import java.util.Scanner;

/**

* This class contains the program to execute a text version of the MineSweeper game.

*

* @author

*

*/

public class MineSweeper extends Config {

/**

* 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) {

while(true){

String resp;

Scanner in = new Scanner(System.in);

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

int width = promptUser(in, "What width of map would"

+ " you like (3 - 20): ", MIN_SIZE, MAX_SIZE);

int height = promptUser(in, "What height of map would"

+ " you like (3 - 20): ", MIN_SIZE, MAX_SIZE);

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

boolean[][] mines = new boolean[height][width];

eraseMap(map);

Random randGen = new Random(Config.SEED);

int totalMines = placeMines(mines, randGen);

while(true){

System.out.println();

System.out.println("Mines: " + totalMines);

simplePrintMap(map);

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

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

if(mines[row-1][column-1] ) {

map[row-1][column-1] = Config.SWEPT_MINE;

showMines(map,mines);

simplePrintMap(map);

System.out.println("Sorry, you lost.");

break;

}

else {

int nearby = numNearbyMines(mines, row-1, column-1);

map[row-1][column-1] = (char) (nearby + 48);

if(allSafeLocationsSwept(map, mines)) {

showMines(map,mines);

simplePrintMap(map);

System.out.println("You Win!");

break;

}

}

}

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

resp = in.nextLine();

resp = resp.trim();

if(resp.toLowerCase().charAt(0)=='y') {

continue;

}

else {

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

break;

}

}

}

/**

* This method prompts the user for a number, verifies that it is between min

* and max, inclusive, before returning the number.

*

* If the number entered is not between min and max then the user is shown

* an error message and given another opportunity to enter a number.

* If min is 1 and max is 5 the error message is:

* Expected a number from 1 to 5.

*

* If the user enters characters, words or anything other than a valid int then

* the user is shown the same message. The entering of characters other

* than a valid int is detected using Scanner's methods (hasNextInt) and

* does not use exception handling.

*

* Do not use constants in this method, only use the min and max passed

* in parameters for all comparisons and messages.

* Do not create an instance of Scanner in this method, pass the reference

* to the Scanner in main, to this method.

* The entire prompt should be passed in and printed out.

*

* @param in The reference to the instance of Scanner created in main.

* @param prompt The text prompt that is shown once to the user.

* @param min The minimum value that the user must enter.

* @param max The maximum value that the user must enter.

* @return The integer that the user entered that is between min and max,

* inclusive.

*/

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

int inp;

System.out.print(prompt);

while (true) {

if (in.hasNextInt()) {

inp = in.nextInt();

if(inp>=min && inp

in.nextLine();

return inp;

}

else {

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

}

}

else {

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

}

in.nextLine();

}

}

/**

* This initializes the map char array passed in such that all

* elements have the Config.UNSWEPT character.

* Within this method only use the actual size of the array. Don't

* assume the size of the array.

* This method does not print out anything. This method does not

* return anything.

*

* @param map An allocated array. After this method call all elements

* in the array have the same character, Config.UNSWEPT.

*/

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

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

for(i=0;i

for(j=0;j

map[i][j]= UNSWEPT;

return;

}

/**

* This prints out a version of the map without the row and column numbers.

* A map with width 4 and height 6 would look like the following:

* . . . .

* . . . .

* . . . .

* . . . .

* . . . .

* . . . .

* For each location in the map a space is printed followed by the

* character in the map location.

* @param map The map to print out.

*/

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

for(char[] row : map){

for(char val : row)

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

System.out.println();

}

return;

}

/**

* This prints out the map. This shows numbers of the columns

* and rows on the top and left side, respectively.

* map[0][0] is row 1, column 1 when shown to the user.

* The first column, last column and every multiple of 5 are shown.

*

* To print out a 2 digit number with a leading space if the number

* is less than 10, you may use:

* System.out.printf("%2d", 1);

*

* @param map The map to print out.

*/

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

return; //FIXME

}

/**

* This method initializes the boolean mines array passed in. A true value for

* an element in the mines array means that location has a mine, false means

* the location does not have a mine. The MINE_PROBABILITY is used to determine

* whether a particular location has a mine. The randGen parameter contains the

* reference to the instance of Random created in the main method.

*

* Access the elements in the mines array with row then column (e.g., mines[row][col]).

*

* Access the elements in the array solely using the actual size of the mines

* array passed in, do not use constants.

*

* A MINE_PROBABILITY of 0.3 indicates that a particular location has a

* 30% chance of having a mine. For each location the result of

* randGen.nextFloat()

* determines whether that location has a mine.

*

* This method does not print out anything.

*

* @param mines The array of boolean that tracks the locations of the mines.

* @param randGen The reference to the instance of the Random number generator

* created in the main method.

* @return The number of mines in the mines array.

*/

public static int placeMines(boolean[][] mines, Random randGen) {

int minesNum =0;

for(int i=0;i

for(int j=0;j

mines[i][j] = (randGen.nextFloat()

if(mines[i][j]) minesNum++;

}

}

return minesNum;

}

/**

* This method returns the number of mines in the 8 neighboring locations.

* For locations along an edge of the array, neighboring locations outside of

* the mines array do not contain mines. This method does not print out anything.

*

* If the row or col arguments are outside the mines array, then return -1.

* This method (or any part of this program) should not use exception handling.

*

* @param mines The array showing where the mines are located.

* @param row The row, 0-based, of a location.

* @param col The col, 0-based, of a location.

* @return The number of mines in the 8 surrounding locations or -1 if row or col

* are invalid.

*/

public static int numNearbyMines(boolean[][] mines, int row, int col) {

int nearbyMines = 0;

if (row >= 0 && row = 0 && col

for (int i = row - 1; i

for (int j = col - 1; j

if (i >= 0 && i = 0 && j

if (i != row || j != col) {

if (mines[i][j] == true)

nearbyMines++;

}

}

}

}

} else {

return -1;

}

return nearbyMines;}

// If there exists a mine on a surrounding tile, add one to

// "nearbyMines"

// Test if the user selection is valid, if it is, test surrounding tiles (if they are valid)

/**

* This updates the map with each unswept mine shown with the Config.HIDDEN_MINE

* character. Swept mines will already be mapped and so should not be changed.

* This method does not print out anything.

*

* @param map An array containing the map. On return the map shows unswept mines.

* @param mines An array indicating which locations have mines. No changes

* are made to the mines array.

*/

public static void showMines(char[][] map, boolean[][] mines) {

for(int i=0;i

for(int j=0;j

if(map[i][j] == UNSWEPT && mines[i][j]) {

map[i][j] = HIDDEN_MINE;

}

}

}

return;

}

/**

* Returns whether all the safe (non-mine) locations have been swept. In

* other words, whether all unswept locations have mines.

* This method does not print out anything.

*

* @param map The map showing touched locations that is unchanged by this method.

* @param mines The mines array that is unchanged by this method.

* @return whether all non-mine locations are swept.

*/

public static boolean allSafeLocationsSwept(char[][] map, boolean[][] mines) {

for(int i=0;i

for(int j=0;j

if(map[i][j] == UNSWEPT && !mines[i][j]) {

return false;

}

}

}

return true;

}

/**

* This method sweeps the specified row and col.

* - If the row and col specify a location outside the map array then

* return -3 without changing the map.

* - If the location has already been swept then return -2 without changing

* the map.

* - If there is a mine in the location then the map for the corresponding

* location is updated with Config.SWEPT_MINE and return -1.

* - If there is not a mine then the number of nearby mines is determined

* by calling the numNearbyMines method.

* - If there are 1 to 8 nearby mines then the map is updated with

* the characters '1'..'8' indicating the number of nearby mines.

* - If the location has 0 nearby mines then the map is updated with

* the Config.NO_NEARBY_MINE character.

* - Return the number of nearbyMines.

*

* @param map The map showing swept locations.

* @param mines The array showing where the mines are located.

* @param row The row, 0-based, of a location.

* @param col The col, 0-based, of a location.

* @return the number of nearby mines, -1 if the location is a mine, -2 if

* the location has already been swept, -3 if the location is off the map.

*/

public static int sweepLocation(char[][] map, boolean[][] mines, int row, int col) {

return -99; //FIXME

}

/**

* This method iterates through all 8 neighboring locations and calls sweepLocation

* for each. It does not call sweepLocation for its own location, just the neighboring

* locations.

* @param map The map showing touched locations.

* @param mines The array showing where the mines are located.

* @param row The row, 0-based, of a location.

* @param col The col, 0-based, of a location.

*/

public static void sweepAllNeighbors(char [][]map, boolean[][] mines, int row, int col) {

return; //FIXME

}

}

Milestone 3: Sweeping 1. In this milestone, implement the bodies of the following methods within the Minesweeper class. See the detailed method headers for precisely how the methods should work. public static int sweepLocation (char[] ] map, boolean [] [] mines, int row, int col) public static void sweepAllNeighbors (char [] [] map, boolean [] [] mines, int row, int col) public static void printMap (char [] [] map) 2. The enhanced printMap method shows the numbers on the top and left of the map as it is printing it out. 3. The main method algorithm changes as follows. a. Within the main method, instead of determining how many nearby mines there are and showing that number in the map, call sweepLocation and if there are no mines neighboring that location then call sweepAINeighbors. b. Replace calls to simplePrintMap with printMap

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

MFDBS 89 2nd Symposium On Mathematical Fundamentals Of Database Systems Visegrad Hungary June 26 30 1989 Proceedings

Authors: Janos Demetrovics ,Bernhard Thalheim

1989th Edition

3540512519, 978-3540512516

More Books

Students also viewed these Databases questions

Question

What are the jobs responsibilities and duties?

Answered: 1 week ago

Question

Distinguish between filtering and interpreting. (Objective 2)

Answered: 1 week ago