【问题标题】:Which is the mistake in the code for game Minesweeper这是游戏扫雷的代码中的错误
【发布时间】:2020-09-30 14:11:03
【问题描述】:
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();
    }
}

早上好。简而言之,代码的作用。游戏开始时,会询问用户System.out.print("How many mines do you want on the field?: ");。用户输入一个数字并随机在二维数组表上生成地雷。来自 2d 数组的平行点 (x, y) 添加到 List&lt;Point&gt; points = new ArrayList&lt;&gt;(); points.add(new Point(x, y)); 所有这些都执行 public void randomX() 方法。打印表格后public void printMinesweeper()。此方法打印带有隐藏地雷else if (minesweeper[i][j] == 'X') {System.out.print('.');} 和可见数字的二维数组表,周围有多少地雷,通过方法else { System.out.print(getCharAt(i, j));} 计算和打印。在程序到达while循环中的方法public void game()并询问用户System.out.print("Set/delete mines marks (y and x coordinates): ");后,用户输入2个坐标。上面我声明了static List&lt;Point&gt; guess = new ArrayList&lt;&gt;();static Map&lt;Point, Character&gt; fields = init() // is a map with 81 cells with keys Point and value '.'; 用户输入的坐标在if-else statement 中验证,如果(fields.get(new Point(x, y)) == '.' &amp;&amp; (getCharAt(x, y) == "0" || getCharAt(x, y) == "X")) 程序放入fields 映射字符'*' 在这个坐标。同时,这个坐标被添加到列表guess 中。并且程序在游戏未完成时执行此操作。在我的代码中,如果游戏结束,如果通过方法private static boolean checkIfFinished() {return points.equals(guess);} 进行检查,如果这是true,则此答案转到布尔变量finished,同时看到已完成并显示System.out.println("Congratulations! You found all mines!");

我的问题是:当我引入大于 3 的地雷时,即使我设置了所有地雷并且我知道 points.equals(guess) 游戏返回 false 并且游戏继续。如何解决这个问题。

【问题讨论】:

    标签: java minesweeper


    【解决方案1】:

    您需要修复checkIfFinished()。在您当前的情况下,您还要检查您的积分顺序是否相同,否则您的游戏将无法完成。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 2021-02-28
      相关资源
      最近更新 更多