代码中的错误在于扫雷游戏中。

huangapple go评论77阅读模式
英文:

Which is the mistake in the code for game Minesweeper

问题

The issue you're facing might be related to the way you're checking if the game is finished using the points.equals(guess) condition. It's possible that there's a subtle difference between the two lists that's causing the comparison to fail, especially when the number of mines is larger.

To resolve this, you can try a more robust approach to check if the game is finished. Instead of relying solely on the points.equals(guess) comparison, you can modify your checkIfFinished() method to directly compare the elements in the points list with the elements in the guess list. This way, you'll ensure that the positions of the points in the lists are identical, regardless of any other factors that might cause the comparison to fail.

Here's how you can modify your checkIfFinished() method:

private static boolean checkIfFinished() {
    if (points.size() != guess.size()) {
        return false;
    }

    for (Point point : points) {
        if (!guess.contains(point)) {
            return false;
        }
    }

    return true;
}

This modified method first checks if the sizes of the points and guess lists are the same. If they're not, it immediately returns false. Then, it iterates through each Point in the points list and checks if it exists in the guess list. If at any point a Point from the points list is not found in the guess list, the method returns false. If all elements in the points list are found in the guess list and the sizes are the same, the method returns true.

This approach should be more reliable in determining whether the game is finished or not, even when the number of mines is larger.

英文:
import java.awt.Point;
import java.util.*;

public class Minesweeper {

    public static void main(String[] args) {
        Minesweeper minesweeper = new Minesweeper();
        minesweeper.randomX();
        minesweeper.game();
    }

    private static final int NR_OF_FIELDS = 81;
    private static final char EMPTY_SPACE = '.';
    char[][] minesweeper = new char[9][9];
    Random randNum = new Random();
    Scanner sc = new Scanner(System.in);
    static List<Point> points = new ArrayList<>();
    static List<Point> guess = new ArrayList<>();
    static Map<Point, Character> fields = init();

    public Minesweeper() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                minesweeper[i][j] = '.';
            }
        }
    }

    boolean finished = false;

    public void game() {
        while (!finished) {
            System.out.print("Set/delete mines marks (y and x coordinates): ");
            int n = sc.nextInt();
            int m = sc.nextInt();
            int x = n - 1;
            int y = m - 1;

            if (n < 0 || n > 9 || m < 0 || m > 9) {
                System.out.println("Coordinates should be from 1 to 9!");
//set character '*'
            } else if (fields.get(new Point(x, y)) == '.' && (getCharAt(x, y) == "0" || getCharAt(x, y) == "X")) {
                fields.put(new Point(x, y), '*');
                guess.add(new Point(x, y));
                finished = checkIfFinished();
                printMinesweeper();
// delete character '*'               
 } else if (fields.get(new Point(x, y)) == '*') {
                    fields.put(new Point(x, y), '.');
                    guess.remove(new Point(x, y));
                    finished = checkIfFinished();
                    printMinesweeper();
            } else {
                System.out.println("There is a number here!");
            }
        }
        System.out.println("Congratulations! You found all mines!");
    }

    private static boolean checkIfFinished() {
        return points.equals(guess);
    }

    private static Map<Point, Character> init() {
        Map<Point, Character> fields = new HashMap<>(81);
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++)
                fields.put(new Point(i, j), '.');
        }
        return fields;
    }

    public void printMinesweeper() {
        System.out.println(" " + "|" + "123456789" + "|");
        System.out.println("-" + "|" + "---------" + "|");
        for (int i = 0; i < 9; i++) {
            System.out.print(i + 1 + "|");
            for (int j = 0; j < 9; j++) {
                if (fields.get(new Point(i, j)) == '*') {
                    System.out.print(minesweeper[i][j] = fields.get(new Point(i, j)));
                    minesweeper[i][j] = 'X';
                } else if (minesweeper[i][j] == 'X') {
                    System.out.print('.');
                } else {
                    System.out.print(getCharAt(i, j));
                }
            }
            System.out.println("|");
        }
        System.out.println("-" + "|" + "---------" + "|");
    }

    private String getCharAt(int i, int j) {
        if (mineAt(i, j)) {
            return "X";
        }

        int minesNear = countMinesNear(i, j);
        if (minesNear == 0) {
            return ".";
        } else {
            return Integer.toString(minesNear);
        }
    }

    private boolean mineAt(int i, int j) {
        return minesweeper[i][j] == 'X';
    }

    private int countMinesNear(int i, int j) {
        int mines = 0;
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                if (x + i >= 0 && x + i < minesweeper.length && y + j >= 0 && y + j < minesweeper.length) {
                    if (minesweeper[x + i][y + j] == 'X') {
                        mines++;
                    }
                }
            }
        }
        return mines;
    }

    public void randomX() {
        System.out.print("How many mines do you want on the field?: ");
        int numberOfMines = sc.nextInt();
        int i = 0;
        while (i < numberOfMines) {
            int x = randNum.nextInt(9);
            int y = randNum.nextInt(9);
            points.add(new Point(x, y));
            if (minesweeper[x][y] == '.') {
                minesweeper[x][y] = 'X';
                i++;
            }
        }
        printMinesweeper();
    }
}

Good morning. In short what the code does. When game begin, the user is asked System.out.print("How many mines do you want on the field?: ");. User input a number and random are generate mines on the 2d array table. Parallel points (x, y) from 2d array where is mines are add to List<Point> points = new ArrayList<>(); points.add(new Point(x, y)); All this do public void randomX() method. After print the table public void printMinesweeper(). This method print a 2d array table with hidden mines else if (minesweeper[i][j] == 'X') {System.out.print('.');} and visible digit, how many mines are around, calculated and prints by the method else { System.out.print(getCharAt(i, j));}. After program got to the method public void game() in while loop and ask user System.out.print("Set/delete mines marks (y and x coordinates): ");.User input 2 coordinates. Above I had declared static List<Point> guess = new ArrayList<>(); and static Map<Point, Character> fields = init() // is a map with 81 cells with keys Point and value '.'; The coordinates input by user are verificated in if-else statement if (fields.get(new Point(x, y)) == '.' && (getCharAt(x, y) == "0" || getCharAt(x, y) == "X")) programm put in the fields map the character '*' at this coordinates. In parallel this coordinates are added in list guess. And programm do this while game is not finished. In my code if game is finished if ckecked by the method private static boolean checkIfFinished() {return points.equals(guess);} if this is true this answer go to boolean variables finished and while see that finished is true and show System.out.println("Congratulations! You found all mines!");

My problem is: when I introduce a number of mines bigger then 3 even if I set all mines and I know that points.equals(guess) the game return false and game continue. How to resolve this.

答案1

得分: 0

你需要修复 checkIfFinished()。在你当前的情况下,你还要检查点的顺序是否相同,否则你的游戏不会结束。

private static boolean checkIfFinished() {
    if (points.size() != guess.size()) {
        return false;
    }
    for (Point point : guess) {
        if (!points.contains(point)) {
            return false;
        }
    }
    return true;
}
英文:

You need to fix checkIfFinished(). In your current case, you check also if the order of your points is the same, otherwise your game won't finish.

private static boolean checkIfFinished() {
    if (points.size() != guess.size()) {
        return false;
    }
    for (Point point : guess) {
        if (!points.contains(point)) {
            return false;
        }
    }
    return true;
}

huangapple
  • 本文由 发表于 2020年9月30日 13:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定