I made several trials but nothing works.
Blows there is the last trial but it also doesn't work.
Any clue how to solve?
// if (!gameField[y][x].isOpen){
// return;
// }
// else if (gameField[y][x].isFlag || gameField[y][x].isMine){
// return;
// }
// else if (isGameStopped == true){
// gameField[y][x].isOpen = false;
// }
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 = 0;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private boolean isGameStopped;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame() {
boolean isMine = false;
isGameStopped = false;
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (getRandomNumber(10) < 1) {
isMine = true;
countMinesOnField++;
}
gameField[x][y] = new GameObject(y, x, isMine);
setCellColor(x, y, Color.RED);
}
}
countFlags = countMinesOnField;
countMineNeighbors();
}
@Override
public void onMouseLeftClick(int x, int y){
openTile(x, y);
}
public void onMouseRightClick(int x, int y){
markTile(x, y);
}
private ArrayList<GameObject> getNeighbors(GameObject cell) {
ArrayList<GameObject> neighbourList = new ArrayList<GameObject>();
if (cell.x - 1 >= 0) {
//row above
if (cell.y - 1 >= 0)
neighbourList.add(gameField[cell.x - 1][cell.y - 1]);
neighbourList.add(gameField[cell.x - 1][cell.y]);
if (cell.y + 1 < SIDE)
neighbourList.add(gameField[cell.x - 1][cell.y + 1]);
}
if (cell.y - 1 >= 0) {
//same row
neighbourList.add(gameField[cell.x][cell.y - 1]);
if (cell.y + 1 < SIDE)
neighbourList.add(gameField[cell.x][cell.y + 1]);
}
if (cell.x + 1 < SIDE) {
//row below
if (cell.y - 1 >= 0)
neighbourList.add(gameField[cell.x + 1][cell.y - 1]);
neighbourList.add(gameField[cell.x + 1][cell.y]);
if (cell.y + 1 < SIDE)
neighbourList.add(gameField[cell.x + 1][cell.y + 1]);
}
return neighbourList;
}
private void countMineNeighbors() {
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (!gameField[x][y].isMine) {
for (GameObject z : getNeighbors(gameField[y][x])) {
if (z.isMine == true)
gameField[x][y].countMineNeighbors++;
}
}
}
}
}
private void openTile(int x, int y) {
gameField[y][x].isOpen = true;
setCellColor(x, y, Color.GREEN);
// if (!gameField[y][x].isOpen){
// return;
// }
// else if (gameField[y][x].isFlag || gameField[y][x].isMine){
// return;
// }
// else if (isGameStopped == true){
// gameField[y][x].isOpen = false;
// }
if (gameField[y][x].isMine) {
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
}
else if (!gameField[y][x].isMine && gameField[y][x].countMineNeighbors >= 1)
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
else if (gameField[y][x].isMine) {
setCellValue(x, y, MINE);
} else if (!gameField[y][x].isMine && gameField[y][x].countMineNeighbors == 0) {
setCellValue(x, y, "");
getNeighbors(gameField[y][x]);
int m = y, n = x;
if (m - 1 >= 0 && m - 1 < SIDE) {
if (!gameField[m - 1][n].isOpen)
openTile(n, m - 1);
if (n - 1 >= 0 && !gameField[m - 1][n - 1].isOpen)
openTile(n - 1, m - 1);
if (n + 1 < SIDE && !gameField[m - 1][n + 1].isOpen)
openTile(n + 1, m - 1);
}
if (m >= 0 && m < SIDE) {
if (n - 1 >= 0 && !gameField[m][n - 1].isOpen)
openTile(n - 1, m);
if (n + 1 < SIDE && !gameField[m][n + 1].isOpen)
openTile(n + 1, m);
}
if (m + 1 >= 0 && m + 1 < SIDE) {
if (!gameField[m + 1][n].isOpen)
openTile(n, m + 1);
if (n - 1 >= 0 && !gameField[m + 1][n - 1].isOpen)
openTile(n - 1, m + 1);
if (n + 1 < SIDE && !gameField[m + 1][n + 1].isOpen)
openTile(n + 1, m + 1);
}
}
}
private void markTile(int x, int y) {
GameObject gameObject = gameField[y][x];
if (gameObject.isOpen || (countFlags == 0 && !gameObject.isFlag)) {
return;
}
if (gameObject.isFlag) {
gameField[y][x].isFlag = false;
countFlags++;
setCellColor(x, y, Color.RED);
setCellValue(x, y, "");
}
else {
gameField[y][x].isFlag = true;
countFlags--;
setCellColor(x, y, Color.BLUE);
setCellValue(x, y, FLAG);
}
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.RED, "GAME OVER" , Color.BLACK, 40);
}
}