扫雷游戏代码中的错误是什么
Which is the mistake in the code for game Minesweeper
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?: ");
。用户输入一个数字并随机生成二维数组 table 上的地雷。来自二维数组的平行点(x,y)被添加到 List<Point> points = new ArrayList<>();
points.add(new Point(x, y));
所有这些做 public void randomX()
方法。打印 table public void printMinesweeper()
后。此方法打印一个二维数组table,隐藏地雷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<Point> guess = new ArrayList<>();
和 static Map<Point, Character> fields = init() // is a map with 81 cells with keys
Point and value '.';
如果 (fields.get(new Point(x, y)) == '.' && (getCharAt(x, y) == "0" || getCharAt(x, y) == "X"))
programm put in the fields
map 则用户输入的坐标在 if-else statement
中验证此坐标处的字符“*”。同时,此坐标被添加到列表 guess
中。程序在游戏未完成时执行此操作。在我的代码中,如果游戏完成,如果通过方法 private static boolean checkIfFinished() {return points.equals(guess);}
进行检查,如果这是 true
,则此答案转到布尔变量 finished
,同时看到完成为真并显示 System.out.println("Congratulations! You found all mines!");
我的问题是:当我引入数量大于 3 的地雷时,即使我设置了所有地雷并且我知道 points.equals(guess)
游戏 return false
并且游戏继续。如何解决这个问题。
您需要修复 checkIfFinished()
。在你目前的情况下,你还要检查你的分数顺序是否相同,否则你的游戏将无法完成。
private static boolean checkIfFinished() {
if (points.size() != guess.size()) {
return false;
}
for (Point point : guess) {
if (!points.contains(point)) {
return false;
}
}
return true;
}
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?: ");
。用户输入一个数字并随机生成二维数组 table 上的地雷。来自二维数组的平行点(x,y)被添加到 List<Point> points = new ArrayList<>();
points.add(new Point(x, y));
所有这些做 public void randomX()
方法。打印 table public void printMinesweeper()
后。此方法打印一个二维数组table,隐藏地雷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<Point> guess = new ArrayList<>();
和 static Map<Point, Character> fields = init() // is a map with 81 cells with keys
Point and value '.';
如果 (fields.get(new Point(x, y)) == '.' && (getCharAt(x, y) == "0" || getCharAt(x, y) == "X"))
programm put in the fields
map 则用户输入的坐标在 if-else statement
中验证此坐标处的字符“*”。同时,此坐标被添加到列表 guess
中。程序在游戏未完成时执行此操作。在我的代码中,如果游戏完成,如果通过方法 private static boolean checkIfFinished() {return points.equals(guess);}
进行检查,如果这是 true
,则此答案转到布尔变量 finished
,同时看到完成为真并显示 System.out.println("Congratulations! You found all mines!");
我的问题是:当我引入数量大于 3 的地雷时,即使我设置了所有地雷并且我知道 points.equals(guess)
游戏 return false
并且游戏继续。如何解决这个问题。
您需要修复 checkIfFinished()
。在你目前的情况下,你还要检查你的分数顺序是否相同,否则你的游戏将无法完成。
private static boolean checkIfFinished() {
if (points.size() != guess.size()) {
return false;
}
for (Point point : guess) {
if (!points.contains(point)) {
return false;
}
}
return true;
}