At this stage, we'll deal with the logic to reveal cells. Let's create a corresponding method and a variable that will store the cell state (revealed or hidden).
When revealing a cell, the method will:
- draw a mine in the cell if it contains a mine;- display the number of mined neighbors if the cell does not contain a mine;
- mark the cell as revealed in the model;
- change the cell color. For convenience, we suggest adding the mine symbol to a separate constant. When the a cell be revealed? Correct! When you click on it with the left mouse button. To handle this click event, the engine has a special method: onMouseLeftClick(int, int).
Let's override it in our class. It will call the method that reveals a cell.
And, of course, run the program and enjoy the results of your work.
Requirements:
- The MinesweeperGame class must have a private static final String MINE field that is initialized when it is declared. For example, you can use the UTF-16 symbol for a bomb: "\uD83D\uDCA3".
- The MinesweeperGame class must have a private void openTile(int x, int y) method.
- The openTile(int, int) method must draw MINE if the gameObject at the passed coordinates is a mine. Use the setCellValue(int, int, String) method.
- The openTile(int, int) method must draw the number of adjacent mines if the gameObject at the passed coordinates is not a mine. Use the setCellNumber(int, int, int) method.
- The GameObject class must have a public boolean isOpen field.
- The openTile(int, int) method should set the isOpen field on the gameField element equal to true and use the setCellColor(int, int, Color) method to draw the cell background. For example, using Color.GREEN.
- The MinesweeperGame class must override the Game parent class's onMouseLeftClick(int, int) method.
- The onMouseLeftClick(int, int) method must call the openTile(int, int) method.
package com.codegym.games.minesweeper;
public class GameObject {
public int x, y;
public boolean isMine;
public int countMineNeighbors;
public boolean isOpen;
public GameObject(int a, int b, boolean m){
x = a;
y = b;
isMine = m;
}
}