Hey, I was trying to count the neighbours around the mine, but I cannot get it to work. Could someone help me?
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
import java.util.*;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
public void initialize(){
setScreenSize(SIDE, SIDE);
createGame();
}
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField = 0;
private void createGame(){
for(int i = 0; i < SIDE; i++)
for(int j = 0; j < SIDE; j++){
boolean mine = false;
if (i == getRandomNumber(10) || j == getRandomNumber(10)){
mine = true;
countMinesOnField++;
}
else mine = false;
gameField[i][j] = new GameObject(j, i, mine);
setCellColor(i, j, Color.ORANGE);
}
countMineNeighbors();
}
private void countMineNeighbors(){
for (int x = 0; x < SIDE; x++)
for(int y = 0; y < SIDE; y++)
if(!gameField[y][x].isMine)
for(GameObject g:getNeighbors(gameField[y][x])){
if(g.isMine)
gameField[y][x].countMineNeighbors++;
}
}
private ArrayList<GameObject> getNeighbors(GameObject gameObject) {
ArrayList<GameObject> neighbors = new ArrayList<GameObject>();
int m = gameObject.y;
int n = gameObject.x;
for(int x = m - 1; x < m + 1; x++)
for(int y = n - 1; y < n + 1; y++){
if(x < 1 || x > SIDE)
break;
if(y < 1 || y > SIDE)
break;
//if(gameField[y][x] == gameObject)
// continue;
neighbors.add(gameField[y][x]);
}
return neighbors;
}
}