I can't pass the last requirements. Maybe it's the sentence "and the last revealed cell is not a mine" - that I don't do right ?..
Anyone knows?
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 static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField;
private int countFlags;
private boolean isGameStopped;
private int countClosedTiles = SIDE * SIDE; // 81
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
// showMessageDialog(Color.BLUE, String.valueOf(countClosedTiles), Color.BLACK, 10);
}
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;
isGameStopped = false;
}
private void openTile(int x, int y) {
// if .. return nothing.
if (gameField[y][x].isOpen || gameField[y][x].isFlag || isGameStopped == true) return;
gameField[y][x].isOpen = true;
--countClosedTiles;
// showMessageDialog(Color.BLUE, String.valueOf(countClosedTiles), Color.BLACK, 10);
if (gameField[y][x].isMine) {
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
return;
}
else {
if (gameField[y][x].countMineNeighbors == 0) {
setCellValue(x, y, "");
setCellColor(x, y, Color.YELLOW);
ArrayList<GameObject> neighbors = (ArrayList<GameObject>) getNeighbors(gameField[y][x]);
for (GameObject neighbor : neighbors) {
if (neighbor.isOpen) continue; // skip already open neighbors
openTile(neighbor.x, neighbor.y);
}
} else {
setCellColor(x, y, Color.YELLOW);
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
}
}
if (countMinesOnField == countClosedTiles) {
win();
}
}
private void markTile(int x, int y) {
if (isGameStopped == false) {
if (gameField[y][x].isOpen == true || countFlags == 0 && gameField[y][x].isFlag == false) {
} else if (gameField[y][x].isFlag == true) {
gameField[y][x].isFlag = false;
countFlags = countFlags+1;
setCellValue(x, y, "");
setCellColor(x, y, Color.ORANGE);
} else {
gameField[y][x].isFlag = true;
countFlags = countFlags-1;
setCellValue(x, y, FLAG);
setCellColor(x, y, Color.YELLOW);
}
}
}
@Override
public void onMouseLeftClick(int x, int y) {
if (isGameStopped) {
showMessageDialog(Color.RED, "GAME OVER", Color.BLACK, 60);
return;
}
openTile(x,y);
}
@Override
public void onMouseRightClick(int x, int y) {
if (isGameStopped) {
showMessageDialog(Color.RED, "GAME OVER", Color.BLACK, 60);
return;
}
markTile(x,y);
}
private void gameOver() {
isGameStopped=true;
showMessageDialog(Color.RED, "GAME OVER", Color.BLACK, 60);
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.GREEN, "YOU WIN", Color.BLACK, 60);
}
private void countMineNeighbors(){
for(int y = 0; y < SIDE; y++){
for(int x = 0; x < SIDE; x++){
if(!gameField[y][x].isMine){
for(GameObject obj : getNeighbors(gameField[y][x])){
if(obj.isMine){gameField[y][x].countMineNeighbors++;}
}
}
}
}
}
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;
}
}