Trying to run game to check wether the board will display, but i'm getting nothing. The program compiles ok, but then gets stuck before the board appears. So it's waiting for an input that never appears. Any ideas?
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import javax.swing.*;
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 int countFlags;
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
@Override
public void onMouseLeftClick(int x, int y) {
openTile(x, y);
}
@Override
public void onMouseRightClick(int x, int y) {markTile(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);
}
}
countFlags = countMinesOnField;
countMineNeighbors();
}
private void countMineNeighbors() {
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
GameObject check = gameField[y][x];
if (!check.isMine) {
for (GameObject g : getNeighbors(gameField[y][x])) {
if (g.isMine) {
gameField[y][x].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 openTile(int x, int y) {
GameObject tile = gameField[y][x];
tile.isOpen = true;
if (tile.isMine) {
setCellValue(x, y, MINE);
setCellColor(x, y, Color.RED);
} else if (tile.countMineNeighbors == 0) {
String s = "";
setCellValue(x, y, s);
setCellColor(x, y, Color.GREEN);
List<GameObject> neighbours = new ArrayList<>(getNeighbors(tile));
for (int i = 0; i < neighbours.size(); i++) {
GameObject neighbour = neighbours.get(i);
if (!neighbour.isOpen)
openTile(neighbour.x, neighbour.y);
}
} else {
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
setCellColor(x, y, Color.GREEN);
}
}
private void markTile(int x, int y){
GameObject tile = gameField[y][x];
if(!tile.isOpen && countFlags > 0){
tile.isFlag = true;
countFlags -= 1;
setCellValue(x, y, FLAG);
setCellColor(x, y, Color.YELLOW);
}
}
public static void main(String[] args) {
MinesweeperGame game = new MinesweeperGame();
game.initialize();
}
}
/*
1. The MinesweeperGame class must have a private void markTile(int, int) method.
2. The markTile(int x, int y) method must not do anything if the element is already revealed (isOpen == true).
3. The markTile(int, int) method must not do anything if the number of unused flags (countFlags) is zero and the current element is not a flag (isFlag = false).
4. The markTile(int, int) method must set the isFlag field to true, reduce the number of unused flags by one,
draw the FLAG symbol on the field if the current element is not a flag (use the setCellValue(int, int, String) method),
and change the cell background on the field (use the setCellColor(int, int, Color) method). For example, to Color.YELLOW.
*/