not working
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
import java.util.ArrayList;
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 = 0;
private int countFlags = 0;
private boolean isGameStopped;
@Override
public void initialize()
{
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame()
{
for (int x = 0; x < SIDE; x++)
{
for (int y = 0; y < SIDE; y++)
{
if (getRandomNumber(10) == 1)
{
gameField[y][x] = new GameObject(x, y, true);
countMinesOnField++;
}
else
{
gameField[y][x] = new GameObject(x, y, false);
}
setCellColor(x, y, Color.GREEN);
}
}
countFlags = countMinesOnField;
countMineNeighbors();
isGameStopped = false;
}
private GameObject getCell(int x, int y){
return gameField[y][x];
}
private GameObject[] getNeighbors(GameObject gameObject)
{
ArrayList<GameObject> neighbors = new ArrayList<>();
final int[] INDEXES = {-1, 0, 1};
for (int index : INDEXES)
{
int x = gameObject.x + index;
for (int i : INDEXES)
{
int y = gameObject.y + i;
if (is_valid(x, y)&&(!gameField[y][x].isOpen))
{
neighbors.add(gameField[y][x]);
}
}
}
return neighbors.toArray(new GameObject[0]);
}
private boolean is_valid(int x, int y)
{
final int BOARDER = 9;
return (x >= 0 && x < BOARDER && y >= 0 && y < BOARDER);
}
private void countMineNeighbors()
{
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
GameObject cell = gameField[i][j];
if (!cell.isMine)
{
GameObject[] neighbors = getNeighbors(cell);
for (GameObject neighbor : neighbors)
{
if (neighbor.isMine)
{
cell.countMineNeighbors++;
}
}
}
}
}
}
private void openTile(int x, int y){
GameObject cell = getCell(x,y);
if (!cell.isOpen)
{
if (!cell.isFlag)
{
if (!isGameStopped)
{
cell.isOpen = true;
setCellColor(x, y, Color.BLUE);
if (cell.isMine)
{
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
}
else
{
if (cell.countMineNeighbors != 0)
{
setCellNumber(x, y, cell.countMineNeighbors);
}
else
{
setCellValue(x, y, "");
GameObject[] neighbors = getNeighbors(cell);
if (!(neighbors.length == 0))
{
for (GameObject neighbor : neighbors)
{
openTile(neighbor.x, neighbor.y);
}
}
}
}
}
}
}
}
@Override
public void onMouseLeftClick(int x, int y)
{
if (is_valid(x, y)){
openTile(x,y);
}
}
private void markTile(int x, int y){
GameObject cell = getCell(x, y);
if (!cell.isOpen && (countFlags != 0 || (cell.isFlag))&&!isGameStopped)
{
if (cell.isFlag){
countFlags++;
setCellColor(x,y,Color.GREEN);
setCellValue(x,y,"");
cell.isFlag = false;
}
else {
countFlags--;
setCellColor(x,y,Color.YELLOW);
setCellValue(x,y,FLAG);
cell.isFlag = true;
}
}
}
@Override
public void onMouseRightClick(int x, int y)
{
if (is_valid(x,y)){
markTile(x,y);
}
}
private void gameOver(){
isGameStopped = true;
showMessageDialog(Color.WHITE,"YOU LOSE!",Color.BLACK,24);
}
}