#java #minesweeper
#java #сапер
Вопрос:
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)) == '.' amp;amp; (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 amp;amp; x i < minesweeper.length amp;amp; y j >= 0 amp;amp; 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-массива. Ко всему этому методу do добавляются параллельные точки (x, y) из 2d-массива, где находятся мины List<Point> points = new ArrayList<>();
points.add(new Point(x, y));
public void randomX()
. После распечатайте таблицу public void printMinesweeper()
. Этот метод выводит таблицу 2d-массива со скрытыми минами else if (minesweeper[i][j] == 'X') {System.out.print('.');}
и видимой цифрой, сколько мин вокруг, вычисляется и печатается с помощью метода else { System.out.print(getCharAt(i, j));}
. После того, как программа добралась до метода public void game()
в цикле while и спросила пользователя 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
указал and value '.';
, что координаты, введенные пользователем, проверяются, if-else statement
если (fields.get(new Point(x, y)) == '.' amp;amp; (getCharAt(x, y) == "0" || getCharAt(x, y) == "X"))
программа помещает на fields
карту символ «*» в этих координатах. Параллельно эти координаты добавляются в список guess
. И программы делают это, пока игра не завершена. В моем коде, если игра завершена, если ckecked с помощью метода private static boolean checkIfFinished() {return points.equals(guess);}
, если это true
этот ответ, перейдите к логическим переменным finished
и пока видите, что завершено, true и show System.out.println("Congratulations! You found all mines!");
Моя проблема в том, что когда я ввожу количество мин, превышающее 3, даже если я установил все мины, и я знаю, что points.equals(guess)
игра возвращается false
и игра продолжается. Как это решить.
Ответ №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;
}