When I run this, it does everything it is meant to. I get a bomb when isMine is true, the number of bombs matches the field count and the number of bombs surround the cell is correct when isMine is false.
Problem is I cannot get these two things to pass validation on openTile(). I cannot see what I am missing. Help???
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
import java.util.ArrayList;
public class MinesweeperGame extends Game{
private static final int SIDE=9;
private static final String MINE = "\uD83D\uDCA3";
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private int countMinesOnField=0;
public void initialize(){
setScreenSize(SIDE,SIDE);
createGame();
}
private void createGame(){
for (int x=0;x<SIDE;x++){
for(int y=0;y<SIDE;y++){
boolean isMine = false;
if (getRandomNumber(10)==2) {
isMine = true;
countMinesOnField++;
}
gameField[x][y] = new GameObject(y,x, isMine);
setCellColor(y,x,Color.RED);
}
}
countMineNeighbors();
}
@Override
public void onMouseLeftClick(int x, int y){
openTile(x,y);
}
private ArrayList<GameObject> getNeighbors(GameObject cell){
ArrayList<GameObject> neighbourList = new ArrayList<GameObject>();
//row above
if (cell.y-1>=0){
if(cell.x-1>=0)
neighbourList.add(gameField[cell.y-1][cell.x-1]);
neighbourList.add(gameField[cell.y-1][cell.x]);
if (cell.x+1<SIDE)
neighbourList.add(gameField[cell.y-1][cell.x+1]);
}
//same row
if (cell.x-1>=0)
neighbourList.add(gameField[cell.y][cell.x-1]);
if (cell.x+1<SIDE)
neighbourList.add(gameField[cell.y][cell.x+1]);
//row below
if (cell.y+1<SIDE){
if(cell.x-1>=0)
neighbourList.add(gameField[cell.y+1][cell.x-1]);
neighbourList.add(gameField[cell.y+1][cell.x]);
if (cell.x+1<SIDE)
neighbourList.add(gameField[cell.y+1][cell.x+1]);
}
return neighbourList;
}
private void countMineNeighbors(){
for(int i=0;i<SIDE;i++){
for(int j=0;j<SIDE;j++){
if(gameField[i][j].isMine == false){
for(GameObject z:getNeighbors(gameField[i][j])){
if (z.isMine == true)
gameField[i][j].countMineNeighbors++;
}
}
}
}
}
private void openTile(int x, int y){
if(gameField[x][y].isMine){
setCellValue(x,y, MINE);
} else {
setCellNumber(x,y,gameField[x][y].countMineNeighbors);
}
gameField[x][y].isOpen = true;
setCellColor(x,y,Color.GREEN);
}
}