Question
Java Foundations write an expand method that, when a correctly guessed non-mine location has zero adjacent mines, the guess is effectively expanded in all directions
Java Foundations
write an expand method that, when a correctly guessed non-mine location has zero adjacent mines, the guess is effectively expanded in all directions until a non-zero number of adjacent mines is encountered. For the example above, the resulting board for this project would look like this when peeking:
To implement the expand() method, you must use a Stack. You can use any generic stack implementation including standard Java classes like java.util.Stack, the textbooks ArrayStack or your own Stack as long as it only contains the basic stack methods that we discussed in classthe Stack should not have Minesweeper-specific features. All of your classes and methods must have Javadoc comments preceding them. These comments should provide a brief description of the behavior and, in the case of methods, describe the input arguments and return value.
Cell.java
public class Cell {
// instance variables private char val; private boolean isMine; private boolean isChecked; // constructor
public Cell() { val = '-'; isMine = false; isChecked = false; }
// getters and setters public char getVal() { return val; }
public void setVal(char val) { this.val = val; }
public boolean isIsMine() { return isMine; }
public void setIsMine(boolean isMine) { this.isMine = isMine; }
public boolean isChecked() { return isChecked; }
public void checked() { isChecked = true; }
} GameBoard.java
import java.util.Random; import java.util.Scanner;
public class GameBoard {
// instance variables private boolean userPeek = true; private int noOfMines = 10; private int minesGuessed = 0; private Cell[][] cells; private Scanner input;
// constructor public GameBoard() { input = new Scanner(System.in); cells = new Cell[10][10]; minesGuessed = 0; }
// function to initilize game board public void initializeBoard() { for (Cell[] cell : cells) { for (int j = 0; j
// function to place mines at random places public void placeMinesAtRandomPlaces() { Random placer = new Random(); int minesPlaced = 0; int cellRow, cellColumn;
while (minesPlaced
// function to checking adjacent mines private int adjacentMinesTally(int cellRow, int cellColumn) { int mineCount = 0; for (int iterI = cellRow - 1; iterI
// function to check if mine exists public boolean checkMineExists(int cellRow, int cellColumn, char userResponse) { if (userResponse == 'y') { if (cells[cellRow][cellColumn].isIsMine()) { cells[cellRow][cellColumn].setVal('M'); minesGuessed++; return true; } else { return false; } } else if (!cells[cellRow][cellColumn].isIsMine()) { updateBoard(cellRow, cellColumn); return true; } else { return false; } }
// function to update board private void updateBoard(int cellRow, int cellColumn) { if (cellRow == 0 || cellRow == cells.length - 1 || cellColumn == 0 || cellColumn == cells.length - 1) { return; } if (cells[cellRow][cellColumn].isIsMine() || cells[cellRow][cellColumn].isChecked()) { return; } int count = adjacentMinesTally(cellRow, cellColumn); cells[cellRow][cellColumn].setVal((char) (count + '0')); cells[cellRow][cellColumn].checked(); if (count == 0) { for (int iterI = -1; iterI
// function to display Board public String displayBoard(boolean peek) { StringBuilder sb = new StringBuilder(" "); for (int iterI = 1; iterI
// function to run the game functionality public void run() { System.out.println("Hi! Welcome to MineSweeper!"); System.out.print("Would you like to play a game? (y): "); char userResponse = input.nextLine().toLowerCase().charAt(0); while (userResponse == 'y') { initializeBoard(); placeMinesAtRandomPlaces(); if (playMineGame()) { System.out.println("You win!"); } else { System.out.println("Boom! You lose."); System.out.println(displayBoard(userPeek)); } System.out.println("Thank you for playing MineSweeper."); System.out.print("Would you like to play another game? (y): "); userResponse = input.nextLine().toLowerCase().charAt(0); } }
// function to play game private boolean playMineGame() { boolean continueGame = true; while (continueGame) { System.out.println(displayBoard(!userPeek)); askForPeek(); continueGame = checkCellForUser() && !gameWon(); } return gameWon(); }
// function to ask for peek from user private void askForPeek() { System.out.print("Would you like to peek? (y): "); char userResponse = input.nextLine().toLowerCase().charAt(0); if (userResponse == 'y') { System.out.println(displayBoard(userPeek)); } }
// function to check cell for mine private boolean checkCellForUser() { int cellRow = getRowNumber(); int cellColumn = getColumnNumber(); System.out.print("Does Row " + cellRow + " and Column " + cellColumn + " contain a mine? (y): "); char userResponse = input.nextLine().toLowerCase().charAt(0); return checkMineExists(cellRow, cellColumn, userResponse); }
// function to get row private int getRowNumber() { String prompt = "Please enter a Row number: "; String error = "Invalid Row number."; return getANumber(prompt, 8, error); }
// function to get column private int getColumnNumber() { String prompt = "Please enter a Column number: "; String error = "Invalid Column number."; return getANumber(prompt, 8, error); }
// function to get number from user private int getANumber(String prompt, int maxSize, String error) { int usrNum = 0; System.out.print(prompt); String tmp = input.nextLine().trim(); if (validateNum(tmp)) { usrNum = Integer.parseInt(tmp); } while (usrNum maxSize) { System.out.println(error); System.out.print(prompt); tmp = input.nextLine().trim(); if (validateNum(tmp)) { usrNum = Integer.parseInt(tmp); } } return usrNum; }
// function to check if enter input is number or not. private boolean validateNum(String num) { for (int iterI = 0; iterI
// function to check if game won or not. private boolean gameWon() { return minesGuessed == 10; } } Minesweeper.java
public class Minesweeper {
// main function public static void main(String[] args) { GameBoard board = new GameBoard(); board.run(); }
}
and it would look like this when not peekingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started