This code results in failed tests telling me that the countMineNeighbors() method is setting neighbor the countMineNeighbors for objects that are flagged as mines, but I don't see why this would be happening. On line 37 I have an if statement that should only do that if the object is not flagged as a mine. Can anyone see what the problem is with this code?
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
};
private void createGame() {
for (int i =0; i<gameField.length; i++){
for(int j = 0; j < gameField[i].length; j++){
boolean hasMine = false;
if (getRandomNumber(10) < 1){
hasMine = true;
countMinesOnField++;
}
gameField[i][j] = new GameObject(j, i, hasMine);
}
countMineNeighbors();
}
for (GameObject[] a : gameField) {
for(GameObject b : a) {
setCellColor(b.x, b.y, Color.BLUE);
}
}
}
private void countMineNeighbors() {
for(GameObject[] x : gameField) {
for (GameObject y : x){
if (!y.isMine) {
y.countMineNeighbors = getNeighbors(y);
}
}
}
}
private int getNeighbors(GameObject g) {
int count = 0;
int[][] neighbors = new int[3][3];
int xStart = g.x -1;
int yStart = g.y -1;
for (int[] row : neighbors){
for (int cell : row) {
if (yStart >= 0 && xStart >=0) {
if (gameField[xStart][yStart].isMine) {
count++;
}
}
yStart++;
}
xStart++;
}
return count;
}
}