In the game of Minesweeper, a player searches for hidden bombs on a rectangular grid. The game board is represented by a grid of booleans marking bomb locations. A grid value is true if there is a bomb at that location, false otherwise. A user can click on any cell they choose. The game is lost when the user clicks on a cell containing a bomb. The game is won when all cells not containing bombs have been opened and the only remaining cells are those containing bombs.

Given such a grid of bomb locations, the method createCountGrid() constructs a new grid of integers storing the count of bombs in each neighborhood. The neighborhood for a location includes the location itself and its eight adjacent locations. In the returned grid, each value will be a number from 0 to 9.

If passed the boolean grid on the left, createCountGrid() returns the grid of int values on the right:

Here are the example grids: see image.

The examples below demonstrate how to compute the countGrid from the bombGrid.

A. In "Example A" one can see the cell [0][0] has a count of 1 because the only adjacent cell containing a bomb is [1][1].

B. In "Example B" one can see the cell [1][2] has a count of 0 because there are no adjacent cells containing a bomb.

C. In "Example C" one can see the cell [1][1] has a count of 4 because there are 4 adjacent cells containing a bomb. [0][0] , [0][2], [2][0], [2][1]

D. In "Example D" one can see the cell [1][1] has a count of 3 because there are 3 adjacent cells containing a bomb (including the cell itself). [1][1] , [2][0], [2][2]

Examples: see image.

OBJECTIVES

1. Implement the Grid class shown below in the UML, so it can be tested via zyBook

  • Implement the private methods createBombGrid() and createCountGrid() as follows:
    • createBombGrid() Creates the boolean 10 x 10 bombGrid shown on the left and randomly places 25 bombs in the grid.
    • createCountGrid() Creates the int 10 x 10 countGrid shown on the right based on the bomb placement in the bombGrid.
  • Implement the default constructor so that it initializes the variables and calls the 2 private methods createBombGrid() and createCountGrid()
  • Implement the getNumRows() , getNumColumns() , getNumBombs()
  • Implement getBombGrid(), which returns a copy of the 2 dimensional boolean array.
  • Implement getCountGrid(), which returns a copy of the 2 dimensional int array.
  • Implement isBombAtLocation(int row, int column) so that it returns true if a bomb is in the cell, false otherwise.
  • Implement getCountAtLocation(int row, int column) so that it returns the sum of the surrounding adjacent bombs counted by retrieving it from the countGrid.

2. Create a GUI using the java Swing package so that it behaves as follows.

  • Display the grid, matching the dimensions as specified in the Grid class, hiding the content of all the cells.
  • Each cell in the GUI's grid should be a JButton with its own ActionListener.
  • When a cell (aka JButton) is clicked there should be one of the following actions triggered.
    • If there is a bomb the game is over and the entire content of the grid is revealed to the player so that cells containing bombs display a bomb, and cells without bombs show their count.
    • If there is no bomb in the cell, the cell's count is revealed from the countGrid to display the number surrounding adjacent bombs.
    • If the cell is the last one without a bomb to be revealed, the game has been won.
  • When the game is over the user should be informed via JOptionPane whether they won or lost and be offered the option to play again.
    • If the user chooses "Yes" to play again, a new instance of the Grid should be created and displayed to play again from step 2a.
    • If the user chooses "No" to not play again, all windows are closed and the application exits.

Extra Credit:

1. Implement the overloaded constructors for the Grid class shown below in the UML

  • Implement Grid(int rows, int columns), so that it initializes the variables using the passed in values, and calls the 2 private methods createBombGrid() and createCountGrid(). The number of bombs should be 25.
  • Implement Grid(int rows, int columns, int numBombs), so that it initializes the variables using the passed in values, and calls the 2 private methods createBombGrid() and createCountGrid(). The number of bombs should be equal to the value passed in.

2. When a user clicks on a cell that has a 0 count reveal adjacent cells as follows:

  • All adjacent and connected 0 cells are revealed
  • All (non-bomb) cells immediately adjacent to the revealed 0 cells are also revealed.

CLASS Grid
- bombGrid : boolean[][]
- countGrid : int[][]
- numRows : int
- numColumns : int
- numBombs : int
+ Grid() : // 10rows, 10 columns, (10 x 10), 25 bombs
+ Grid(int rows, int columns): // (rows x columns Grid), default value of 25 bombs
+ Grid(int rows, int columns, int numBombs): // (rows x columns Grid), numBombs as specified
+ getNumRows(): int
+ getNumBombs(): int
+ getBombGrid(): boolean[][]
+ getCountGrid(): int[][]
+ isBombAtLocation(int row, int column): boolean
+ getCountAtLocation(int row, int column): int
- createBombGrid(): void // called by constructors to create and populate the bombGrid
- createCountGrid(): void // called by constructors to create and populate countGrid from bombGrid

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.