This is how I implemented getNeighbor() method, but apparently, there's a problem with it. Can anyone please tell me what's wrong?
I'm getting an error with the third condition:
• The countMineNeighbors() method should, for each non-mined cell in the gameField matrix, count the number of adjacent mined cells and assign this value to the countMineNeighbors field.
Note: Only non-mine cells are calling this method
***************************************************************
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private int countMinesOnField = 0;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
public void initialize(){
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame(){
for (int i = 0; i < SIDE; i++){
for (int j = 0; j < SIDE; j++){
int posib = getRandomNumber(10);
boolean mine = (posib<1)?true:false;
if (mine){
countMinesOnField++;
}
gameField[i][j] = new GameObject(i, j, mine);
setCellColor(i, j, Color.ORANGE);
}
}
countMineNeighbors();
}
private void countMineNeighbors(){
for (int i = 0; i < SIDE; i++){
for (int j = 0; j < SIDE; j++){
if (!gameField[i][j].isMine){
getNeighbors(gameField[i][j]);
}
}
}
}
public void getNeighbors(GameObject room){
int count = 0;
for (int i = room.x - 1; i <= room.x + 1; i++){
for (int j = room.y - 1; j <= room.y + 1; j++){
try{
if (gameField[i][j].isMine){
count++;
}
}
catch (Exception e){
}
}
}
room.countMineNeighbors = count;
}
}