Как решить требование валидатора очищать поле от мин, чисел и флагов?
Раскомментирование команды clearField(); в методе restart() тут же вызывает ошибки валидатора помимо этой еще и в нескольких других местах
package com.javarush.games.minesweeper;
import com.javarush.engine.cell.*;
import java.util.*;
public class MinesweeperGame extends Game{
private static final int SIDE = 9;
private int countClosedTiles = SIDE*SIDE;
private int score;
private final static String MINE = "\uD83D\uDCA3";
private final static String FLAG = "\uD83D\uDEA9";
private int countFlags;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField;
private boolean isGameStopped;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
private void gameOver() {
isGameStopped = true;
showMessageDialog(Color.ORANGE, "Конец", Color.RED, 20);
}
private void restart() {
isGameStopped = false;
countClosedTiles = SIDE*SIDE;
score = 0;
setScore(score);
countMinesOnField = 0;
//clearField();
createGame();
}
private void clearField() {
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
setCellValue(x, y, "");
}
}
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.ORANGE, "Выигрыш", Color.RED, 20);
}
private void openTile(int x, int y) {
if (isGameStopped) return;
GameObject gameObj = gameField[y][x];
if (gameObj.isOpen) return;
if (gameObj.isFlag) return;
if (gameObj.isMine) {
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
} else {
gameObj.isOpen = true;
countClosedTiles--;
score = score + 5;
setScore(score);
setCellColor(x, y, Color.GREEN);
if (countMinesOnField == countClosedTiles) {
win();
}
if (gameObj.countMineNeighbors > 0)
setCellNumber(x, y, gameObj.countMineNeighbors);
else
setCellValue(x, y, "");
if (gameObj.countMineNeighbors == 0) {
ArrayList<GameObject> list = getNeighbors(gameObj);
for (GameObject currGameObj : list) {
if (!currGameObj.isMine) {
openTile(currGameObj.x, currGameObj.y);
}
}
}
}
}
private void markTile(int x, int y) {
if (isGameStopped) return;
GameObject gameObj = gameField[y][x];
if (gameObj.isOpen) return;
if (!gameObj.isFlag && countFlags <= 0) return;
gameObj.isFlag = !gameObj.isFlag;
if (gameObj.isFlag) {
//Поставили флаг
countFlags--;
setCellColor(x, y, Color.YELLOW);
setCellValue(x, y, FLAG);
} else {
//Сняли флаг
countFlags++;
setCellColor(x, y, (gameObj.isMine ? Color.RED : Color.ORANGE));
setCellValue(x, y, "");
}
}
@Override
public void onMouseLeftClick(int x, int y){
if (isGameStopped) {
restart();
} else {
openTile(x, y);
}
}
@Override
public void onMouseRightClick(int x, int y){
markTile(x, y);
}
private void createGame(){
countMinesOnField = 0;
for (int x = 0; x < SIDE; x++){
for (int y = 0; y < SIDE; y++){
boolean isMine = getRandomNumber(10) == 5;
GameObject gameObj = new GameObject(x, y, isMine);
gameField[y][x] = gameObj;
setCellColor(x, y, Color.ORANGE);
if (gameObj.isMine) countMinesOnField = countMinesOnField + 1;
//if (gameObj.isMine) setCellColor(x, y, Color.RED);
}
}
countFlags = countMinesOnField;
countMineNeighbors();
LightMines();
}
private void LightMines(){
for (int x = 0; x < SIDE; x++){
for (int y = 0; y < SIDE; y++){
GameObject gameObj = gameField[y][x];
if (gameObj.isMine) setCellColor(x, y, Color.RED);
}
}
}
private void countMineNeighbors(){
getNeighbors(gameField[1][1]);
//Перебор ячеек вокруг
for (int x = 0; x < SIDE; x++){
for (int y = 0; y < SIDE; y++){
GameObject gameObj = gameField[y][x];
if (!gameObj.isMine) {
//setCellNumber(x, y, gameObj.countMineNeighbors);
gameObj.countMineNeighbors = GetNumOfMines(x, y);
//setCellNumber(x, y, gameObj.countMineNeighbors);
}
}
}
}
private int GetNumOfMines(int x, int y){
int result = 0;
for (int i = Math.max(0, x - 1); i <= Math.min(SIDE - 1, x + 1); i++)
for (int j = Math.max(0, y - 1); j <= Math.min(SIDE - 1, y + 1); j++) {
if (gameField[j][i].isMine)
result = result + 1;
}
return result;
}
private ArrayList<GameObject> getListOfNeighbors(GameObject gameObj) {
int x = gameObj.x;
int y = gameObj.y;
ArrayList<GameObject> list = new ArrayList<GameObject>();
for (int i = Math.max(0, x - 1); i <= Math.min(SIDE - 1, x + 1); i++)
for (int j = Math.max(0, y - 1); j <= Math.min(SIDE - 1, y + 1); j++) {
list.add(gameField[j][i]);
}
return list;
}
private ArrayList<GameObject> getNeighbors(GameObject gameObj){
int x = gameObj.x;
int y = gameObj.y;
ArrayList<GameObject> list = new ArrayList<GameObject>();
for (int i = Math.max(0, x - 1); i <= Math.min(SIDE - 1, x + 1); i++)
for (int j = Math.max(0, y - 1); j <= Math.min(SIDE - 1, y + 1); j++) {
list.add(gameField[j][i]);
}
return list;
// return 1;
}
}