I have troubles with passing 3rd and 4th task. Can I ask for code review?
package com.codegym.games.minesweeper;
public class GameObject {
public int x;
public int y;
public boolean isMine;
public int countMineNeighbors;
public boolean isOpen = true;
public boolean isFlag;
public GameObject(int x, int y, boolean isMine) {
this.x = x;
this.y = y;
this.isMine = isMine;
}
}
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import java.util.ArrayList;
import java.util.List;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField = 0;
private int countFlags;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
@Override
public void onMouseLeftClick(int x, int y) {
openTile(x, y);
}
private void createGame() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
boolean isMine = getRandomNumber(10) < 1;
if (isMine) {
countMinesOnField++;
}
gameField[y][x] = new GameObject(x, y, isMine);
setCellColor(x, y, Color.ORANGE);
}
}
countMineNeighbors();
countFlags = countMinesOnField;
}
private List<GameObject> getNeighbors(GameObject gameObject) {
List<GameObject> result = new ArrayList<>();
for (int y = gameObject.y - 1; y <= gameObject.y + 1; y++) {
for (int x = gameObject.x - 1; x <= gameObject.x + 1; x++) {
if (y < 0 || y >= SIDE) {
continue;
}
if (x < 0 || x >= SIDE) {
continue;
}
if (x == gameObject.x && y == gameObject.y) {
continue;
}
result.add(gameField[y][x]);
}
}
return result;
}
private void countMineNeighbors() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
GameObject thisField = gameField[y][x];
if (!thisField.isMine) {
List<GameObject> neighborsList = getNeighbors(thisField);
int numberOfMines = 0;
for (GameObject neighbors : neighborsList) {
if (neighbors.isMine) {
numberOfMines++;
thisField.countMineNeighbors = numberOfMines;
}
}
}
}
}
}
private void openTile(int x, int y) {
gameField[y][x].isOpen = true;
setCellColor(x, y, Color.GREEN);
if (!gameField[y][x].isMine && gameField[y][x].countMineNeighbors >= 1) {
setCellNumber(gameField[y][x].x, gameField[y][x].y, gameField[y][x].countMineNeighbors);
}
if (gameField[y][x].isMine) {
setCellValue(x, y, MINE);
} else if (gameField[y][x].countMineNeighbors == 0 && !gameField[y][x].isMine) {
setCellValue(x, y, "");
for (GameObject gameObject : getNeighbors(gameField[y][x])) {
if (!gameObject.isOpen) {
openTile(gameObject.x, gameObject.y);
if (gameObject.countMineNeighbors != 0 && !gameField[y][x].isMine) {
setCellNumber(gameObject.x, gameObject.y, gameObject.countMineNeighbors);
}
}
}
} else {
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
}
}
}