Hey,
In line 53 I think it should met expectations.
Anyone can assist?
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
import java.util.*;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int mineCount = 0;
private int countMinesOnField;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private int countFlags;
public void initialize(){
setScreenSize(SIDE,SIDE);
createGame();
}
private void openTile(int x, int y){
if(gameField[y][x].isMine){
setCellValue(x,y, MINE);
} else {
setCellNumber(x,y,gameField[x][y].countMineNeighbors);
}
gameField[x][y].isOpen = true;
setCellColor(x,y,Color.GREEN);
}
@Override
public void onMouseLeftClick(int x, int y){
openTile(x,y);
}
private void createGame(){
countFlags = countMinesOnField;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
int n = getRandomNumber(9);
setCellColor(j,i,Color.ORANGE);
if (n == 1){
gameField[j][i] = new GameObject(j,i,true);
this.countMinesOnField++;
//setCellValueEx(j,i,Color.RED,MINE,Color.WHITE,50);
setCellValueEx(j,i,Color.CORNFLOWERBLUE,FLAG,Color.WHITE,50);
}
gameField[j][i] = new GameObject(j,i,false);
}
}
countMineNeighbors();
System.out.println(countMinesOnField);
countFlags = countMinesOnField;
}
private void countMineNeighbors() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
GameObject currentCell = gameField[x][y];
if (!gameField[x][y].isMine)
{
GameObject gameObject = gameField[x][y];
getNeighbors(gameObject);
//System.out.print(gameObject.countMineNeighbors + " ");
}
}
}
}
private void getNeighbors(GameObject gameObject)
{
int count = 0;
int x = gameObject.y;
int y = gameObject.x;
if(!gameField[x][y].isMine)
{
if(x == 0 && y == 0 )
{
if(gameField[1][0].isMine) count++;
if(gameField[1][1].isMine) count++;
if(gameField[0][1].isMine) count++;
}
else if(x == 0 && y == (SIDE-1))
{
if(gameField[0][SIDE-2].isMine) count++;
if(gameField[1][SIDE-2].isMine) count++;
if(gameField[1][SIDE-1].isMine) count++;
}
else if(y == 0 && x == (SIDE-1))
{
if(gameField[SIDE-2][0].isMine) count++;
if(gameField[SIDE-2][1].isMine) count++;
if(gameField[SIDE-1][1].isMine) count++;
}
else if(x == (SIDE-1) && y == (SIDE-1))
{
if(gameField[SIDE-2][SIDE-1].isMine) count++;
if(gameField[SIDE-2][SIDE-2].isMine) count++;
if(gameField[SIDE-1][SIDE-2].isMine) count++;
}
else if(x == 0)
{
if(gameField[x][y-1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
else if (x == (SIDE-1))
{
if(gameField[x][y-1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x-1][y].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
}
else if(y == 0)
{
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
else if(y == (SIDE-1))
{
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x][y-1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
}
else
{
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x][y-1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
}
gameObject.countMineNeighbors = count;
}
}