my code caught by infinity loop :(
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;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
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;
}
@Override
public void onMouseLeftClick(int x, int y){//Override the onMouseLeftClick method
openTile(x,y);//call the openTile method
}
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(){//create a neighbors mine
for(int i=0; i<SIDE; i++){
for(int j=0; j<SIDE; j++){
if(!gameField[i][j].isMine){// if there are no mines
for(GameObject g : getNeighbors(gameField[i][j]))//call the getNeighbors method
if(g.isMine){//if there is a mine
gameField[i][j].countMineNeighbors++;//instanceofcrement the count of mine in the gameField
}
}
}
}
}
private void openTile(int x, int y){//create a openTile method of int variable x,y
gameField[x][y].isOpen = true;//if its open
setCellColor(x,y,Color.ORANGE);//set the color
if(gameField[y][x].isMine){//check there is a mine in the GameObject
setCellValue(x,y,MINE);//if yes means set the mine
}
else
if(!gameField[y][x].isMine && gameField[y][x].countMineNeighbors >= 1)
{
setCellNumber(x, y, gameField[y][x].countMineNeighbors );
}
else
if(!gameField[y][x].isMine && gameField[y][x].countMineNeighbors == 0)
{
setCellValue(x,y, "");//if no means display the neighbors Numbers
for(GameObject p:getNeighbors(gameField[x][y]))
{
if(p.isOpen == false)
openTile(p.x,p.y);
}
}
}
}