1) Убедись, что в методе openTile() значение поля score увеличивается на 5, если элемент матрицы gameField отмечается флагом isOpen и этот элемент — не мина.
2)Убедись, что метод setScore() вызывается после изменения параметра score.
package com.javarush.games.minesweeper;
import com.javarush.engine.cell.*;
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 static final String FLAG = "\uD83D\uDEA9";
private int countFlags;
private boolean isGameStopped;
private int countClosedTiles = SIDE*SIDE;
private int score;
@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 x=0;x<gameField.length;x++){
for(int y=0;y<gameField[x].length;y++){
boolean isMaine=false;
if(getRandomNumber(10)<1){
isMaine=true;
countMinesOnField++;
}
gameField[y][x]=new GameObject(x,y,isMaine);
setCellColor(x,y,Color.ORANGE);
}
}
countFlags = countMinesOnField;
countMineNeighbors();
isGameStopped=false;
}
private List<GameObject> getNeighbors(GameObject cell){
List<GameObject> listNeighbors = new ArrayList<>();
for(int i=cell.x-1;i<=cell.x+1;i++){
for(int j=cell.y-1;j<=cell.y+1;j++){
if((i!=cell.x || j!=cell.y) && ((i>=0) && (j>=0) &&(i<SIDE) &&(j<SIDE) )){
listNeighbors.add(gameField[j][i]);
}
}
}
return listNeighbors;
}
private void countMineNeighbors(){
List<GameObject> list = new ArrayList<>();
for (int x = 0; x < SIDE; x++) {
for (int y = 0; y < SIDE; y++) {
if (!gameField[y][x].isMine) {
list = getNeighbors(gameField[y][x]);
for( int i = 0; i < list.size(); i++){
if (list.get(i).isMine){
gameField[y][x].countMineNeighbors++;
}
}
}
}
}
}
private void openTile(int x, int y){
if(gameField[y][x].isOpen!=true && gameField[y][x].isFlag!=true && isGameStopped!=true){
gameField[y][x].isOpen=true;
setCellColor(x,y,Color.GREEN);
countClosedTiles--;
if(gameField[y][x].isMine){
setCellValueEx(x,y,Color.RED,MINE);
gameOver();
if(isGameStopped==true){
return;
}
} else{
List<GameObject> list = new ArrayList<>();
list = getNeighbors(gameField[y][x]);
score+=5;
if(countClosedTiles==countMinesOnField){
win();
}
if(gameField[y][x].countMineNeighbors==0){
for ( GameObject object : list) {
if(!object.isOpen){
openTile(object.x,object.y);
}
}
setCellValue(x,y,"");
}else{
setCellNumber(x,y,gameField[y][x].countMineNeighbors);
}
setScore(score);
}
}
}
private void markTile(int x, int y){
if(gameField[y][x].isOpen==true){
return;
}
if(countFlags==0 && gameField[y][x].isFlag==false){
return;
}
if(gameField[y][x].isFlag==false){
gameField[y][x].isFlag = true;
countFlags--;
setCellValue(x, y, FLAG);
setCellColor(x, y, Color.YELLOW);
} else{
gameField[y][x].isFlag = false;
countFlags++;
setCellValue(x, y, "");
setCellColor(x, y, Color.ORANGE);
}
}
private void gameOver(){
isGameStopped=true;
showMessageDialog(Color.WHITE,"YOU LOSE, HA-HA!!!",Color.RED,50);
}
private void win(){
isGameStopped=true;
showMessageDialog(Color.WHITE,"YOU WIN!",Color.GREEN,50);
}
}