Уже окончательно запутался, у остальных работает, а у меня ifelse ....
Пишет что я считаю мины тае же и для каждой ячейки "мины" из матрицы, а не нада. Не пойму.
На экран выводит, все в порядке. Но валидатор видимо смущает лишняя операция подсчета ((
Смущают меня варианты с:
if (!gameField[y][x].isMine)package com.javarush.games.minesweeper;
import com.javarush.engine.cell.*;
import java.util.ArrayList;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private int countMinesOnField;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame() {
for (int x = 0; x < gameField.length; x++) {
for (int y = 0; y < gameField[x].length; y++) {
boolean isMine;
//mining some fields
if (getRandomNumber(10) == 9) {
isMine = true;
countMinesOnField++;
} else {
isMine = false;
}
gameField[y][x] = new GameObject(x, y, isMine);
setCellColor(y, x, Color.ORANGE);
}
}
countMineNeighbors();
}
//FIXME
private void countMineNeighbors() {
for (int y = 0; y < gameField.length; y++) {
for (int x = 0; x < gameField[y].length; x++) {
// if gameField cell IS mined
if (gameField[y][x].isMine) {
setCellColor(x, y, Color.RED);
continue;
// if gameField cell IS NOT mined
} else {
ArrayList<GameObject> neighborCells =
getNeighbors(gameField[y][x]);
// considers mines in neighbor cells
for (GameObject cell : neighborCells) {
if (cell.isMine) {
gameField[y][x].countMineNeighbors++;
}
}
// text cell, color cell
if (gameField[y][x].countMineNeighbors != 0) {
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
}
}
}
}
//getNeighbors();
}
private ArrayList<GameObject> getNeighbors(GameObject cell) {
ArrayList<GameObject> cellList = new ArrayList<>();
GameObject thisCell = cell;
for (int j = thisCell.y - 1; j <= thisCell.y + 1; j++) {
if (gameField[j] == null) {
continue;
} else {
for (int i = thisCell.x - 1; i <= thisCell.x + 1; i++) {
if (gameField[j][i] == null
|| gameField[j][i].equals(thisCell)) {
continue; // gonext if cell isnt exist/current posission
} else {
cellList.add(gameField[j][i]);
}
}
}
}
return cellList;
}
}