Cейчас запустил, пощёлкал - увидел, что цифры в ячейках не зависят от расположения мин, т.е. рядом с нулём может быть мина, а рядом с единицей - не быть.
Помогите, пожалуйста.
package com.javarush.games.minesweeper;
import com.javarush.engine.cell.Color;
import com.javarush.engine.cell.Game;
import java.util.ArrayList;
import java.util.List;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField;
private static final String MINE = "\uD83D\uDCA3";
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
@Override
public void onMouseLeftClick(int x, int y) {
openTile(x, y);
}
private void createGame() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
boolean isMine = getRandomNumber(10) < 1;
if (isMine) {
countMinesOnField++;
}
gameField[y][x] = new GameObject(x, y, isMine);
setCellColor(x, y, Color.ORANGE);
}
}
countMineNeighbors();
}
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 y = 0; y < SIDE; y++) {
for(int x = 0; x < SIDE; x++) {
if(!gameField[y][x].isMine) {
ArrayList<GameObject> array = new ArrayList<>(getNeighbors(gameField[y][x]));
for(int i = 0; i < array.size(); i++) {
if(array.get(i).isMine)
gameField[y][x].countMineNeighbors++;
}
}
}
}
}
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[y][x].isOpen = true;
setCellColor(x, y, Color.GREEN);
}
}