Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3606648 • Letter: #

Question

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

The following are methods that should be used:

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.

[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.

Explanation / Answer

import java.util.Arrays;

import java.util.Random;

public class MineSweeper {

static int mWidth = 3; // width of the minefield

static int mHeight = 3; // height of the minefield

static int mMines = 3; // number of mines

static char[][] mMinefield; // 2-dimensional array of chars for our board

static Random random;

public MineSweeper() {

System.out.println("Generating minefield...");

random=new Random();

mMinefield = new char[mHeight][mWidth];

System.out.println("Clearing minefield...");

clearMinefield();

System.out.println("Placing mines...");

boolean[][] minearray = new boolean[][] { { false, true, false }, { false, true, false },

{ false, true, false },

};

mMines = placeMines(minearray, random);

System.out.println("MineSweeper.MineSweeper()" + mMines);

System.out.println(Arrays.deepToString(minearray));

/* placeMines() */;

System.out.println("Show mines...");

showMines(mMinefield, minearray);

boolean status=allSafeLocationsSwept(mMinefield, minearray);

if (status) {

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

}

else

System.out.println("Sorry…");

}

public void placeMines() {

int minesPlaced = 0;

Random random = new Random(); // this generates random numbers for us

while (minesPlaced < mMines) {

int x = random.nextInt(mWidth); // a number between 0 and mWidth - 1

int y = random.nextInt(mHeight);

// make sure we don't place a mine on top of another

if (mMinefield[y][x] != '*' && mMinefield[y][x] != '*') {

mMinefield[y][x] = '*';

minesPlaced++;

}

}

}

public static boolean allSafeLocationsSwept(char[][] map,

boolean[][] mines){

int sweptcount=0;

int totalmove=mMinefield.length*mMinefield[0].length-mMines;

while(sweptcount!=totalmove){

int x = random.nextInt(mWidth); // a number between 0 and mWidth - 1

int y = random.nextInt(mHeight);

if (mMinefield[y][x] == '*') {

return false;

}

sweptcount++;

}

return true;

}

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

int minesPlaced = 0;

Random random = randGen; // this generates random numbers for us

int x = random.nextInt(mWidth); // a number between 0 and mWidth - 1

int y = random.nextInt(mHeight);

// make sure we don't place a mine on top of another

/*if ( mines[x][y]) {

mMinefield[y][x] = '*';

minesPlaced++;

}*/

for (int i = 0; i < mines.length; i++) {

for (int j = 0; j < mines[i].length; j++) {

if (mines[i][j]) {

mMinefield[i][j] = '*';

minesPlaced++;

}

}

}

return minesPlaced;

}

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

return minesNear(row, col);

}

public void clearMinefield() {

// Set every grid space to a space character.

for (int y = 0; y < mHeight; y++) {

for (int x = 0; x < mWidth; x++) {

mMinefield[y][x] = ' ';

}

}

}

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

calculateHints();

for (int y = 0; y < mHeight; y++) {

for (int x = 0; x < mWidth; x++) {

System.out.print(mMinefield[y][x]);

}

System.out.print(" ");

}

}

public static void calculateHints() {

for (int y = 0; y < mHeight; y++) {

for (int x = 0; x < mWidth; x++) {

if (mMinefield[y][x] != '*') {

mMinefield[y][x] = (char) ('0'+ minesNear(y, x));

}

}

}

}

public static int minesNear(int y, int x) {

int mines = 0;

// check mines in all directions

mines += mineAt(y - 1, x - 1); // NW

mines += mineAt(y - 1, x); // N

mines += mineAt(y - 1, x + 1); // NE

mines += mineAt(y, x - 1); // W

mines += mineAt(y, x + 1); // E

mines += mineAt(y + 1, x - 1); // SW

mines += mineAt(y + 1, x); // S

mines += mineAt(y + 1, x + 1); // SE

if (mines > 0) {

return mines;

} else {

return 0;

}

}

// returns 1 if there's a mine a y,x or 0 if there isn't

public static int mineAt(int y, int x) {

// we need to check also that we're not out of array bounds as that

// would

// be an error

if (y >= 0 && y < mHeight && x >= 0 && x < mWidth && mMinefield[y][x] == '*') {

return 1;

} else {

return 0;

}

}

// this starts the command line application

public static void main(String[] args) {

MineSweeper mineSweeper = new MineSweeper();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote