The createGame() method must clear all items (flags, mines, numbers) from the playing field. Use the setCellValue(int, int, String) method. Pass an empty string as the last argument.
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 int countMinesOnField;
private static final String MINE="\uD83D\uDCA3";
private int countFlags;
private static final String FLAG ="\uD83D\uDEA9";
private boolean isGameStopped ;
private int countClosedTiles =SIDE*SIDE;
private int score;
@Override
public void initialize() {
setScreenSize(SIDE, SIDE);
createGame();
}
private void createGame() {
// isGameStopped=false;
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);
}
}
countMineNeighbors();
countFlags=countMinesOnField;
setCellValue(countFlags,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()
{
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
if(!gameField[i][j].isMine)
{
for(GameObject z:getNeighbors(gameField[i][j]))
{
if (z.isMine == true)
gameField[i][j].countMineNeighbors++;
}
}
}
}
}
private void openTile(int x,int y)
{
if(gameField[y][x].isFlag )
{
return;
}
if(gameField[y][x].isOpen )
{
return;
}
if(isGameStopped==true )
{
return;
}
gameField[y][x].isOpen = true;
setCellColor(x,y,Color.GREEN);
countClosedTiles--;
if(gameField[y][x].isMine)
{
setCellValue(x,y,MINE);
}
else
if(!gameField[y][x].isMine &&gameField[y][x].countMineNeighbors==0)
{
for(GameObject p:getNeighbors(gameField[y][x]))
{
if(!p.isOpen)
openTile(p.x,p.y);//recursively on each neighbor that hasn't been revealed.
}
}
else
{
setCellNumber(x,y,gameField[y][x].countMineNeighbors);
// setCellValue(gameField[y][x].x, gameField[y][x].y, "");
}
if(!gameField[y][x].isMine &&gameField[y][x].countMineNeighbors==0)
{
setCellValue(gameField[y][x].x, gameField[y][x].y, "");
}
if(gameField[y][x].isMine)
{
setCellValueEx(x,y,Color.RED,MINE) ;
gameOver();
}
if(countClosedTiles==countMinesOnField&&!gameField[y][x].isMine)
{
win();
}
if(gameField[y][x].isOpen&&!gameField[y][x].isMine)
{
score=score+5;
}
setScore(score);
}//opentile closes
private void markTile(int x,int y)
{
if(isGameStopped==true)
return;
if((gameField[y][x].isOpen==true)||(!gameField[y][x].isFlag&&countFlags==0))
return;
if(!gameField[y][x].isFlag)
{
countFlags--;
setCellValue(x,y,FLAG);
gameField[y][x].isFlag=true;
setCellColor(x,y,Color.YELLOW);
}else
if(gameField[y][x].isFlag)
{
countFlags++;
setCellValue(x,y,"");
gameField[y][x].isFlag=false;
setCellColor(x,y,Color.ORANGE);
}
}
@Override
public void onMouseRightClick(int x, int y)
{
markTile(x,y);
}
private void gameOver()
{
isGameStopped=true;
showMessageDialog(Color.GREEN, "GAME OVER", Color.RED, 40) ;
}
private void win()
{
isGameStopped=true;
showMessageDialog(Color.GREEN, "Congrats you won", Color.GREEN, 40) ;
}
private void restart()
{
isGameStopped=false;
countClosedTiles=SIDE*SIDE;
score=0;
countMinesOnField=0;
createGame();
setScore(score);
}
@Override
public void onMouseLeftClick(int x, int y)
{
// openTile(x,y);
if(isGameStopped)
{
restart();
return;
}
this.openTile(x,y);
}
}