I would be happy if anyone could help me in finding a solution. My game works fine (both in the codegym IDE and local in VS Code). I don't think my solution is that far away from decent code (correct me please if iam wrong) but I can't get Codegym to accept my solution. I have tried to different approaches, with no success. Honestly at this point, it's not about solving problems or finding a solution, but only to find code which Codegym accepts. Which sucks on so many levels. I would like to continue but iam stuck here. I would appreciate any help.
Thanks a lot!
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.Color;
import com.codegym.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 List<GameObject> MinesOnField = new ArrayList<>();
private int countMinesOnField = 0;
private int unusedFlags = 0;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private static final Color colorCell = Color.ORANGE;
private static final Color colorOpen = Color.GREEN;
private static final Color colorMine = Color.RED;
private static final Color colorFlag = Color.YELLOW;
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame() {
boolean isMine = false;
// Schleife für Zeilen
for (int y = 0; y < SIDE; y++) {
// Schleife für Spalten
for (int x = 0; x < SIDE; x++) {
isMine = getRandomNumber(10) < 1;
gameField[y][x] = new GameObject(x, y, isMine);
setCellColor(x, y, colorCell);
if (isMine) {
countMinesOnField++;
MinesOnField.add(gameField[y][x]);
}
}
}
countMineNeighbors();
unusedFlags = countMinesOnField;
}
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() {
List<GameObject> result = new ArrayList<>();
for (int y = 0; y < SIDE; y++) {
// Schleife für Spalten
for (int x = 0; x < SIDE; x++) {
if (gameField[y][x].isMine) {
continue;
}
result = getNeighbors(gameField[y][x]);
for (GameObject gameObject : result) {
if (gameObject.isMine) {
gameField[y][x].countMineNeighbors++;
}
}
}
}
}
private void openTile(int x_, int y_) {
if (gameField[y_][x_].isOpen == true) {
return;
}
if (gameField[y_][x_].isMine) {
// Game Over
setCellValue(x_, y_, MINE);
gameField[y_][x_].isOpen = true;
setCellColor(x_, y_, colorMine);
}
else {
gameField[y_][x_].isOpen = true;
setCellColor(x_, y_, colorOpen);
if (gameField[y_][x_].countMineNeighbors == 0) {
setCellValue(x_, y_, "");
for (GameObject neighbors : getNeighbors(gameField[y_][x_])) {
openTile(neighbors.x, neighbors.y);
}
}
else {
setCellNumber(x_, y_, gameField[y_][x_].countMineNeighbors);
}
}
}
@Override
public void onMouseLeftClick(int x_, int y_) {
openTile(x_, y_);
}
@Override
public void onMouseRightClick(int x_, int y_) {
markTile(x_, y_);
}
private void markTile(int x_, int y_) {
if (!gameField[y_][x_].isOpen) {
if (gameField[y_][x_].isFlag) {
gameField[y_][x_].isFlag = false;
unusedFlags++;
setCellValue(x_, y_, "");
setCellColor(x_, y_, colorCell);
}
else {
if (unusedFlags != 0) {
gameField[y_][x_].isFlag = true;
unusedFlags--;
setCellValue(x_, y_, FLAG);
setCellColor(x_, y_, colorFlag);
}
}
}
}
}