Hey together,
since I finished the first 5 parts of the game a long time ago, and when I continued the code was gone, I copied a lot from other users, cause I couldn't go back to part 1-4 and couldn't remember what they were about.
Result is, I finished all tasked and could verify them, but now the game isn't working correctly.
I can't really get into it and find the problems - I would be extremely thankful if someone could solve the following problems:
1) If you hit a field without a number, there are actually a lot of field opened but also 1 mine mostly :/ so its a instant lose.
2) The numbers in the field are not always correct
I can't publish my solution since code gym judge it as "finished solution", no matter its not at all !
Here the copy:
package com.codegym.games.minesweeper;
import com.codegym.engine.cell.*;
public class MinesweeperGame extends Game{
//Parameter
private static final int SIDE = 9;
private int countMinesOnField = 0;
private GameObject[][] gameField = new GameObject[SIDE][SIDE];
private static final String MINE = "\uD83D\uDCA3";
private static final String FLAG = "\uD83D\uDEA9";
private int countFlags;
private boolean isGameStopped;
private int countClosedTiles = SIDE * SIDE;
private int score;
//Methoden
@Override
public void initialize(){
setScreenSize(SIDE,SIDE);
showGrid(true);
createGame();
System.out.println(countMinesOnField);
}
private void createGame(){
for(int i = 0; i < SIDE; i++)
for(int j = 0; j < SIDE; j++){
setCellValue(i, j, "");
int rn = getRandomNumber(10);
if(rn == 9 && countMinesOnField < 10){
gameField[i][j] = new GameObject(i, j,true );
countMinesOnField ++;
}
else{
gameField[i][j] = new GameObject(i, j, false);
}
setCellColor(i, j, Color.LIGHTPINK);
}
countMineNeighbors();
countFlags = countMinesOnField;
}
@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 countMineNeighbors(){
for (int j = 0; j < SIDE; j++){
for (int i = 0; i < SIDE; i++){
if (!gameField[i][j].isMine){
GameObject gameObject = gameField[i][j];
getNeighbors(gameObject);
}
}
}
}
private void getNeighbors(GameObject gameObject){
int count = 0;
int x = gameObject.y;
int y = gameObject.x;
if(!gameField[x][y].isMine){
if(x == 0 && y == 0 ){
if(gameField[1][0].isMine) count++;
if(gameField[1][1].isMine) count++;
if(gameField[0][1].isMine) count++;
}
else if(x == 0 && y == (SIDE-1)){
if(gameField[0][SIDE-2].isMine) count++;
if(gameField[1][SIDE-2].isMine) count++;
if(gameField[1][SIDE-1].isMine) count++;
}
else if(y == 0 && x == (SIDE-1)){
if(gameField[SIDE-2][0].isMine) count++;
if(gameField[SIDE-2][1].isMine) count++;
if(gameField[SIDE-1][1].isMine) count++;
}
else if(x == (SIDE-1) && y == (SIDE-1)){
if(gameField[SIDE-2][SIDE-1].isMine) count++;
if(gameField[SIDE-2][SIDE-2].isMine) count++;
if(gameField[SIDE-1][SIDE-2].isMine) count++;
}
else if(x == 0){
if(gameField[x][y-1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
else if (x == (SIDE-1)){
if(gameField[x][y-1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x-1][y].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
}
else if(y == 0){
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
else if(y == (SIDE-1)){
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x][y-1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
}
else{
if(gameField[x-1][y].isMine) count++;
if(gameField[x+1][y].isMine) count++;
if(gameField[x-1][y-1].isMine) count++;
if(gameField[x][y-1].isMine) count++;
if(gameField[x+1][y-1].isMine) count++;
if(gameField[x-1][y+1].isMine) count++;
if(gameField[x][y+1].isMine) count++;
if(gameField[x+1][y+1].isMine) count++;
}
}
gameObject.countMineNeighbors = count;
}
private void openTile(int x, int y){
if (gameField[y][x].isOpen || gameField[y][x].isFlag || isGameStopped){
}
else {
gameField[y][x].isOpen = true;
countClosedTiles--;
setCellColor(x, y, Color.GREEN);
if (!gameField[y][x].isMine && gameField[y][x].countMineNeighbors >= 1){
setCellNumber(x, y, gameField[y][x].countMineNeighbors);
score+=5;
}
else if (gameField[y][x].isMine){
setCellValueEx(x, y, Color.RED, MINE);
gameOver();
}
else if (!gameField[y][x].isMine && gameField[y][x].countMineNeighbors == 0) {
setCellValue(x, y, "");
getNeighbors(gameField[y][x]);
score+=5;
int m = y, n = x;
if (m - 1 >= 0 && m - 1 < SIDE){
if (!gameField[m - 1][n].isOpen)
openTile(n, m - 1);
if (n - 1 >= 0 && !gameField[m - 1][n - 1].isOpen)
openTile(n - 1, m - 1);
if (n + 1 < SIDE && !gameField[m - 1][n + 1].isOpen)
openTile(n + 1, m - 1);
}
if (m >= 0 && m < SIDE){
if (n - 1 >= 0 && !gameField[m][n - 1].isOpen)
openTile(n - 1, m);
if (n + 1 < SIDE && !gameField[m][n + 1].isOpen)
openTile(n + 1, m);
}
if (m + 1 >= 0 && m + 1 < SIDE){
if (!gameField[m + 1][n].isOpen)
openTile(n, m + 1);
if (n - 1 >= 0 && !gameField[m + 1][n - 1].isOpen)
openTile(n - 1, m + 1);
if (n + 1 < SIDE && !gameField[m + 1][n + 1].isOpen)
openTile(n + 1, m + 1);
}
}
}
setScore(score);
if (countMinesOnField == countClosedTiles && !gameField[y][x].isMine){
win();
}
}
private void markTile(int x, int y) {
GameObject gameObject = gameField[y][x];
if(isGameStopped){
}
else{
if (!gameObject.isOpen) {
if (!gameObject.isFlag && countFlags > 0) {
gameObject.isFlag = true;
countFlags--;
setCellValue(x, y, FLAG);
setCellColor(x, y, Color.YELLOW);
}
else if (gameObject.isFlag) {
gameObject.isFlag = false;
countFlags++;
setCellValue(x, y, "");
setCellColor(x, y, Color.LIGHTPINK);
}
}
}
}
private void gameOver(){
isGameStopped = true;
showMessageDialog(Color.BLACK, "GAME OVER", Color.WHITE, 72);
}
private void win(){
isGameStopped = true;
showMessageDialog(Color.BLACK, "YOU WIN", Color.WHITE, 72);
}
private void restart(){
isGameStopped = false;
countClosedTiles = SIDE * SIDE;
score = 0;
setScore(score);
countMinesOnField = 0;
createGame();
}
}
Greetings Noah