我是这个网站的新手,但是,当我开始游戏时,我在隐藏广泛问题时遇到了问题,我只是想让用户看不到它。但我不知道我在哪里错过了我的代码。请帮帮我。
import java.util.Scanner;
public class MineSweeperGame {
int row, col;
boolean succes = false;
public static void main(String[] args) {
MineSweeperGame ms = new MineSweeperGame();
ms.run();
}
//
// Recursive reveal method
//
public void reveal(int x, int y, char[][] mineField, boolean[][] isVisible) {
int minx, miny, maxx, maxy;
int result = 0;
//
// if cell(x, y) is not mine, make sure visible that cell.
//
if (mineField[x][y] != '*') {
isVisible[x][y] = true;
//
// if cell(x, y) is blank, check all surrounding cells
//
if (mineField[x][y] == '.') {
//
// Don't try to check beyond the edges of the board...
//
minx = (x <= 0 ? 0 : x - 1);
miny = (y <= 0 ? 0 : y - 1);
maxx = (x >= row - 1 ? row - 1 : x + 1);
maxy = (y >= col - 1 ? col - 1 : y + 1);
//
// Loop over all surrounding cells, call recursive reveal(i, j) method
//
for (int i = minx; i <= maxx; i++) {
for (int j = miny; j <= maxy; j++) {
if (isVisible[i][j] == false) {
reveal(i, j, mineField, isVisible);
}
}
}
}
} //
// if cell(x, y) is mine, do nothing.
//
else {
}
}
void printMineMap(char[][] mineField, boolean[][] isVisible) {
System.out.println();
succes = true;
//
// Loop over all cells, print cells
//
for (int x = 0; x < row; x++) {
for (int y = 0; y < col; y++) {
//
// Loop over all cells, print cells
//
if (isVisible[x][y]) {
System.out.print(" " + mineField[x][y] + " ");
} else {
System.out.print("[" + mineField[x][y] + "] ");
if (mineField[x][y] != '*') {
succes = false;
}
}
}
System.out.println();
}
if (succes) {
System.out.println("********** Congratulations~!!! You win. **********");
}
}
private void run() {
//
// Initialize MineField
//
char[][] mineField = MineField.getMineField();
row = mineField.length;
col = mineField[0].length;
boolean[][] isVisible = new boolean[row][col];
// print mine map
printMineMap(mineField, isVisible);
while (true) {
System.out.print("Enter your guess (x y): ");
Scanner in = new Scanner(System.in);
//
// input x, y
//
int x = in.nextInt() - 1;
if (x < 0) {// if negative value, exit program.
System.out.println("Canceled by user.");
break;
}
int y = in.nextInt() - 1;
if (x >= row || y >= col) // ignore invalid values
{
continue;
}
//
// Check cell(x,y) is mine, if yes, quit program
//
if (mineField[x][y] == '*') {
isVisible[x][y] = true;
printMineMap(mineField, isVisible);
System.out.println("Game Over ~!!!");
break;
}
//
// call recursive reveal method to reveal cell(x, y)
//
reveal(x, y, mineField, isVisible);
printMineMap(mineField, isVisible);
}
}
}
游戏开始时的位置和示例(我尝试将其作为游戏剂量发布,但每次尝试都会错过订单)。
[.] [.] [1] [1] [2] [*] [1] [.]
[.] [.] [1] [*] [2] [1] [1] [.]
[.] [.] [1] [1] [1] [1] [2] [2]
[.] [1] [2] [2] [1] [1] [*] [*]
[.] [1] [*] [*] [1] [1] [2] [2]
[1] [2] [3] [4] [3] [1] [.] [.]
[*] [1] [1] [*] [*] [1] [.] [.]
[1] [1] [1] [2] [2] [1] [.] [.]
输入您的猜测(x y):
我想要的是这样的东西
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
[] [] [] [] [] [] [] []
答案 0 :(得分:1)
我觉得这是一种奇怪的方式,但我假设你是java的新手,这是一种帮助你自己学习的任务或个人项目。
据我所知,看起来你的if语句对if和else都做同样的事情。考虑更改else语句以打印&#34; []&#34;
if (isVisible[x][y]) {
System.out.print(" " + mineField[x][y] + " ");
} else {
System.out.print("[" + mineField[x][y] + "] ");
if (mineField[x][y] != '*') {
succes = false;
}
}
换句话说,上面应该是:
if (isVisible[x][y]) {
System.out.print(" " + mineField[x][y] + " ");
} else {
System.out.print("[ ] ");
if (mineField[x][y] != '*') {
succes = false;
}
}
也许给那个镜头?