je n'arrive vraiment pas à trouver la solution --'
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;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private int countFlags;
private boolean isGameStopped;
private int countClosedTiles = SIDE * SIDE;
private int score;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
@Override
public void onMouseLeftClick(int x, int y) {
openTile(x, y);
}
@Override
public void onMouseRightClick(int x, int y) {
if (!isGameStopped) markTile(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);
}
}
isGameStopped = false;
countMineNeighbors();
countFlags = countMinesOnField;
score = 0;
}
private void gameOver() {
showMessageDialog(Color.WHITE, "Game Over", Color.RED, 75);
isGameStopped = true;
}
private void win() {
showMessageDialog(Color.WHITE, "You Win, MF !", Color.BLUE, 75);
isGameStopped = true;
}
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 (gameField[y][x] == gameObject) {
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++) {
int countMineNeighbors = 0;
if (gameField[y][x].isMine == false) {
List<GameObject> cellNeighbors = new ArrayList<GameObject>();
cellNeighbors = getNeighbors( gameField[y][x]);
for (int i = 0; i < cellNeighbors.size(); i++) {
if (cellNeighbors.get(i).isMine == true)
countMineNeighbors += 1;
}
gameField[y][x].countMineNeighbors = countMineNeighbors;
} else continue;
}
}
}
private void openTile(int x, int y) {
if (!isGameStopped && gameField[y][x].isOpen == false) {
if (!gameField[y][x].isFlag) {
if (gameField[y][x].isMine == true) {
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
}
else {
int countMineNeighbors = gameField[y][x].countMineNeighbors;
gameField[y][x].isOpen = true;
setCellColor(x, y, Color.GREEN);
countClosedTiles -= 1;
score += 5;
if (countMineNeighbors != 0) {
setCellNumber(x, y, countMineNeighbors);
List<GameObject> cellNeighbors = new ArrayList<GameObject>();
cellNeighbors = getNeighbors(gameField[y][x]);
for (int i = 0; i < cellNeighbors.size(); i++) {
if (cellNeighbors.get(i).isMine == true)
continue;
if ((cellNeighbors.get(i).countMineNeighbors != 0) && (cellNeighbors.get(i).isOpen == false)) {
cellNeighbors.get(i).isOpen = true;
setCellNumber(cellNeighbors.get(i).x, cellNeighbors.get(i).y, cellNeighbors.get(i).countMineNeighbors);
setCellColor(cellNeighbors.get(i).x, cellNeighbors.get(i).y, Color.GREEN);
countClosedTiles -= 1;
score += 5;
}
if ((cellNeighbors.get(i).countMineNeighbors == 0) && (cellNeighbors.get(i).isOpen == false))
openTile(cellNeighbors.get(i).x, cellNeighbors.get(i).y);
}
} else {
setCellValue(x, y, "");
List<GameObject> cellNeighbors = new ArrayList<GameObject>();
cellNeighbors = getNeighbors(gameField[y][x]);
for (int i = 0; i < cellNeighbors.size(); i++) {
if (cellNeighbors.get(i).isMine == true)
continue;
if ((cellNeighbors.get(i).countMineNeighbors != 0) && (cellNeighbors.get(i).isOpen == false)) {
cellNeighbors.get(i).isOpen = true;
setCellNumber(cellNeighbors.get(i).x, cellNeighbors.get(i).y, cellNeighbors.get(i).countMineNeighbors);
setCellColor(cellNeighbors.get(i).x, cellNeighbors.get(i).y, Color.GREEN);
countClosedTiles -= 1;
score += 5;
}
if ((cellNeighbors.get(i).countMineNeighbors == 0) && (cellNeighbors.get(i).isOpen == false))
openTile(cellNeighbors.get(i).x, cellNeighbors.get(i).y);
}
}
setScore(score);
if (countClosedTiles == countMinesOnField) win();
}
}
}
}
private void markTile(int x, int y) {
if (gameField[y][x].isFlag == false) {
if (gameField[y][x].isOpen == false && countFlags > 0) {
gameField[y][x].isFlag = true;
setCellValue(x, y, FLAG);
setCellColor(x, y, Color.YELLOW);
countFlags -= 1;
}
} else if (gameField[y][x].isFlag == true) {
gameField[y][x].isFlag = false;
setCellValue(x, y, "");
setCellColor(x, y, Color.ORANGE);
countFlags += 1;
}
}
}