All works as expected, i tired passing both x,y and y,x and both work perfectly (as the game should) but i cannot pass the two openTile conditions. Any help is appreciated. I also tried leaving everything x,y except gameField[y][x].isMine and still no luck.
private void openTile(int y, int x) {
// display a mine in the cell if it's a mine
if (gameField[y][x].isMine)
{
this.setCellValue(y, x, MINE);
this.setCellColor(y, x, Color.RED);
}
// display the # of mined neighbors if not a mine in cell
else
{
this.setCellNumber(y, x, gameField[y][x].countMineNeighbors);
this.setCellColor(y, x, Color.GREEN);
}
// set the status of the cell to open
gameField[y][x].isOpen = true;
}
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
public class MinesweeperGame extends Game {
private static final int SIDE = 9;
private int countMinesOnField;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private static final String MINE = "\uD83D\uDCA3";
public void initialize() {
this.setScreenSize (SIDE, SIDE);
createGame();
}
private void createGame() {
int numMines = 0;
for (int x=0; x<SIDE; x++) {
for (int y=0; y<SIDE; y++) {
this.setCellColor(x, y, Color.ORANGE);
if (this.getRandomNumber(10) == 1) {
gameField[y][x] = new GameObject(x, y, true);
numMines++;
}
else
gameField[y][x] = new GameObject(x, y, false);
}
}
countMinesOnField = numMines;
countMineNeighbors();
}
private void countMineNeighbors() {
for (int x=0; x<SIDE; x++)
for (int y=0; y<SIDE; y++)
if (gameField[y][x].isMine == false) getNeighbors(gameField[y][x]);
}
private void getNeighbors(GameObject GameObject) {
GameObject.countMineNeighbors = 0;
// cell to left checked if viable
if (GameObject.x-1 >= 0)
if (gameField[GameObject.y][GameObject.x-1].isMine) GameObject.countMineNeighbors++;
// cell to right checked if viable
if (GameObject.x+1 < SIDE)
if (gameField[GameObject.y][GameObject.x+1].isMine) GameObject.countMineNeighbors++;
// above row cells checked if viable
if (GameObject.y-1 >= 0) {
if (gameField[GameObject.y-1][GameObject.x].isMine) GameObject.countMineNeighbors++;
if (GameObject.x-1 >= 0)
if (gameField[GameObject.y-1][GameObject.x-1].isMine) GameObject.countMineNeighbors++;
if (GameObject.x+1 < SIDE)
if (gameField[GameObject.y-1][GameObject.x+1].isMine) GameObject.countMineNeighbors++;
}
// below row cells checked if viable
if (GameObject.y+1 < SIDE) {
if (gameField[GameObject.y+1][GameObject.x].isMine) GameObject.countMineNeighbors++;
if (GameObject.x-1 >= 0)
if (gameField[GameObject.y+1][GameObject.x-1].isMine) GameObject.countMineNeighbors++;
if (GameObject.x+1 < SIDE)
if (gameField[GameObject.y+1][GameObject.x+1].isMine) GameObject.countMineNeighbors++;
}
}
private void openTile(int y, int x) {
// display a mine in the cell if it's a mine
if (gameField[y][x].isMine)
{
this.setCellValue(y, x, MINE);
this.setCellColor(y, x, Color.RED);
}
// display the # of mined neighbors if not a mine in cell
else
{
this.setCellNumber(y, x, gameField[y][x].countMineNeighbors);
this.setCellColor(y, x, Color.GREEN);
}
// set the status of the cell to open
gameField[y][x].isOpen = true;
}
@Override
public void onMouseLeftClick(int y, int x) {
openTile(y, x);
}
}