gpt4 book ai didi

Java 扫雷游戏 我想在开始游戏时隐藏宽屏?

转载 作者:行者123 更新时间:2023-12-01 11:06:13 25 4
gpt4 key购买 nike

我是这个网站的新手,但是,当我开始游戏时,我在隐藏我的宽屏时遇到了问题,我只是想让用户看不到它。但我不知道我的代码在哪里错过了。请帮我解决这个问题。

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):

我想要的是这样的

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

[] [] [] [] [] [] [] []

最佳答案

我觉得这是一种奇怪的做法,但我假设您是 Java 新手,这是某种帮助您自己学习的作业或个人项目。

据我所知,您的 if 语句似乎对 if 和 else 执行了相同的操作。考虑更改 else 语句以打印“[ ]”

                            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;
}
}

也许可以尝试一下?

关于Java 扫雷游戏 我想在开始游戏时隐藏宽屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com