can someone explain the logic of private List<GameObject> getNeighbors(GameObject gameObject) method
what do these 2 lines do
for (int y = gameObject.y - 1; y <= gameObject.y + 1; y++) {
for (int x = gameObject.x - 1; x <= gameObject.x + 1; x++) {
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
import java.util.ArrayList;
import java.util.List;
public class MinesweeperGame extends Game{
private GameObject[][] gameField = new GameObject[9][9];
private static final int SIDE=9;
private int countMinesOnField;
public int minecount=0;
public void initialize()
{
setScreenSize(SIDE,SIDE);
createGame();
countMinesOnField=minecount;
}
private void createGame()
{
for(int x=0; x<SIDE ;x++)
{
for(int y=0;y<SIDE;y++)
{
int n = getRandomNumber(10);
if(n==9)
{
gameField[y][x] = new GameObject(x, y,true);
setCellColor(x, y, Color.DARKSEAGREEN) ;
minecount++;
}
else
{
gameField[y][x] = new GameObject(x, y,false);
setCellColor(x, y, Color.DARKSEAGREEN) ;
}
}
}
this.countMinesOnField = minecount;
}
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()
{
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
if(!gameField[i][j].isMine)
{
for(GameObject z:getNeighbors(gameField[i][j]))
{
if (z.isMine == true)
gameField[i][j].countMineNeighbors++;
}
}
}
}
}
}