Java扫雷器控制台递归错误
我不熟悉堆栈溢出 我的扫雷舰基于文本的程序一直有问题,我就是不能让我的递归以我想要的方式工作 有人能帮我递归显示所有空白单元格,并且是“单击”单元格的邻居吗 我的代码的问题是每次我在10乘10板上发现一个点 例如: 当您输入r以显示时,请将其放入第3行和第3列 代码显示了地图上除地雷以外的所有位置, 而不是只显示附近的数字,而不是连接到其他“空白”位置 这些已经过时了 输出应为:Java扫雷器控制台递归错误,java,algorithm,recursion,helper,minesweeper,Java,Algorithm,Recursion,Helper,Minesweeper,我不熟悉堆栈溢出 我的扫雷舰基于文本的程序一直有问题,我就是不能让我的递归以我想要的方式工作 有人能帮我递归显示所有空白单元格,并且是“单击”单元格的邻居吗 我的代码的问题是每次我在10乘10板上发现一个点 例如: 当您输入r以显示时,请将其放入第3行和第3列 代码显示了地图上除地雷以外的所有位置, 而不是只显示附近的数字,而不是连接到其他“空白”位置 这些已经过时了 输出应为: 1| . . . . . . . . . . 2| . . . . . .
1| . . . . . . . . . .
2| . . . . . . . . . .
3| . . 3 . . . . . . .
4| . . . . . . . . . .
5| . . . . . . . . . .
6| . . . . . . . . . .
7| . . . . . . . . . .
8| . . . . . . . . . .
9| . . . . . . . . . .
10| . . . . . . . . . .
1 2 3 4 5 6 7 8 9 10
然而,实际输出是:
1| 1 1 1 1 1 1 0 0 0 0
2| 1 . 1 1 . 1 0 0 0 0
3| 2 3 3 2 2 2 1 0 0 0
4| 1 . . 1 1 . 2 1 1 0
5| 1 3 3 2 2 2 3 . 2 1
6| 0 2 . 2 1 . 3 3 . 1
7| 0 2 . 2 1 1 2 . 2 1
8| 1 2 3 2 1 0 1 1 1 0
9| 2 . 4 . 1 1 1 2 1 1
10| 2 . . 2 1 1 . 2 . 1
1 2 3 4 5 6 7 8 9 10
以下是我的递归准则:
扫雷艇的递推算法
当用户单击单元格时
如果该单元格尚未显示,请显示它
如果细胞的本质是地雷,游戏结束了
否则,如果单元格的真实性质为空,则递归地显示
每一个都有8个邻居,如果他们在边界内并且没有被透露
该算法自然地发现所有相邻的空单元,并在到达边缘上的多个单元时终止
这是我的新代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class P4_Icel_Murad_MinesweeperConsole {
char resp = ' ';
int row = 0;
int col = 0;
final static int MINES = 5;
public boolean won = false;
public boolean fail = false;
public static void main(String[] args) {
P4_Icel_Murad_MinesweeperConsole hello = new P4_Icel_Murad_MinesweeperConsole(10, MINES);
System.out.println();
hello.revealBoard();
}
private static class Cell {
final int FLAGGED = 1;
final int QUESTION = 2;
final int BLANK = 3;
public boolean hidden = true;
public final boolean mine;
public int flagType;
public int neighbors = 0;
public Cell(boolean mine) {
this.mine = mine;
}
public void setFlag(int myFlagType){
if(myFlagType == 1){
this.flagType = myFlagType;
}else if (myFlagType == 2) {
this.flagType = myFlagType;
}else if (myFlagType == 3) {
hidden = true;
this.neighbors = myFlagType;
}
}
public boolean getflagType(int i) {
if(i == 1){
return true;
}else
return false;
}
}
private final int size;
private Cell[][] board;
private void revealBoard(){
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
Cell f = board[i][j];
f.hidden = false;
}
}
showBoard();
}
private void revealCell(int i, int j){
Cell f = board[i][j];
f.hidden = false;
f.setFlag(0);
}
private P4_Icel_Murad_MinesweeperConsole(int size, int numberOfMines) {
this.size = size;
initBord(numberOfMines);
showBoard();
while(!won && !fail){
questionAsk();
showBoard();
}
}
private void questionAsk(){
Scanner s = new Scanner(System.in);
System.out.println("Would you like to flag a cell or reveal a cell?");
System.out.println("Enter 'f' or 'r': >");
resp = s.next().charAt(0);
System.out.println("Enter row: ");
row = s.nextInt() - 1;
System.out.println("Enter col: ");
col = s.nextInt() - 1;
if(row > size || col > size){
System.out.println("This row/col amount is too high.");
showBoard();
questionAsk();
}
userInput(row,col,true);
}
private void initBord(int numberOfMines) {
List<Cell> mines = new ArrayList<Cell>();
for (int i = 0; i < size * size; i++) {
mines.add(new Cell(i < numberOfMines));
}
Collections.shuffle(mines);
board = new Cell[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = (i == 0 || j == 0 || i == size || j == size)
? new Cell(false)
: mines.remove(0);
}
}
calculateNeighbors();
}
// F>HIDDEN IS DONE ONLY FOR ONE POS, MUST FIX
private void userInput(int row, int col, boolean revealMines){
Cell f = board[row][col];
if (resp == 'r') {
if (f.mine && !revealMines) {
return;
}
if (f.hidden) {
revealCell(row,col);
if(f.mine){
failed();
}else if (f.neighbors == 0){
if (isInBounds(row + 1,col)){
Cell neighbor = board[row + 1][col];
if (neighbor.hidden && neighbor.neighbors == 0) {
userInput(row + 1,col,false);
}else if (neighbor.hidden && !neighbor.mine){
revealCell(row + 1,col);
}
}
if (isInBounds(row - 1,col)){
Cell neighbor = board[row - 1][col];
if (neighbor.hidden && neighbor.neighbors == 0) {
userInput(row - 1,col,false);
}else if (neighbor.hidden && !neighbor.mine){
revealCell(row - 1,col);
}
}
if (isInBounds(row,col - 1)){
Cell neighbor = board[row][col - 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row,col - 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row,col-1);
}
}
if (isInBounds(row , col + 1)){
Cell neighbor = board[row][col + 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row,col + 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row,col+1);
}
}
if (isInBounds(row - 1,col - 1)){
Cell neighbor = board[row - 1][col - 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row - 1,col - 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row -1 ,col - 1);
}
}
if (isInBounds(row + 1,col - 1)){
Cell neighbor = board[row + 1][col - 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row + 1,col - 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row + 1 ,col - 1);
}
}
if (isInBounds(row + 1,col + 1)){
Cell neighbor = board[row + 1][col + 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row + 1,col + 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row + 1 ,col + 1);
}
}
if (isInBounds(row - 1,col + 1) && f.hidden){
Cell neighbor = board[row - 1][col + 1];
if (neighbor.hidden && neighbor.neighbors == 0)
userInput(row - 1,col + 1,false);
else if (neighbor.hidden && !neighbor.mine){
revealCell(row - 1 ,col + 1);
}
}
}
}
}
if(resp == 'f'){
if (f.hidden) {
f.setFlag(1);
revealCell(row,col);
}else if(f.flagType == 1){
f.setFlag(2);
}else if(f.flagType == 2){
f.setFlag(3);
}else if(f.flagType == 3){
f.setFlag(1);
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if(f.getflagType(1) == f.mine)
won = true;
}
}
}
private boolean isInBounds(int row, int col){
if((row < size && row >= 0) && (col < size && col >= 0)){
return true;
}
return false;
}
private void failed(){
System.out.println("You Lost! :(");
fail = true;
}
private void calculateNeighbors() {
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
if(isInBounds(i + di,j + dj))
if (board[i + di][j + dj].mine) count++;
}
}
board[i][j].neighbors = count;
count = 0;
}
}
}
private void showBoard() {
for(int i = 0; i < size; i++) {
System.out.printf("%2d| ",i + 1);
for(int j = 0; j < size; j++) {
Cell f = board[i][j];
if(f.hidden || f.flagType == 3) {
System.out.printf("%3s",". ");
} else if (f.mine) {
System.out.printf("%3s","* ");
} else if (f.flagType == 1){
System.out.printf("%3s","F ");
}else if (f.flagType == 2){
System.out.printf("%3s","? ");
}else{
calculateNeighbors();
System.out.printf("%3s", f.neighbors + " ");
}
}
System.out.println();
}
System.out.print(" ");
for(int i = 0; i < size; i++) {
System.out.printf("%3s",i + 1 + " ");
}
System.out.println();
}
}
Thank you in advance.
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
公共级P4_冰封_Murad_扫雷控制台{
char resp='';
int行=0;
int col=0;
最终静态int=5;
公共布尔值=false;
公共布尔值fail=false;
公共静态void main(字符串[]args){
P4_Icel_Murad_扫雷控制台hello=新的P4_Icel_Murad_扫雷控制台(10枚地雷);
System.out.println();
您好,revealBoard();
}
私有静态类单元{
最终整型标记=1;
最后一道问题=2;
最终整型空白=3;
公共布尔隐藏=真;
公共矿山;
公共int标志类型;
公共int邻域=0;
公共单元(布尔型){
这是我的;
}
公共void setFlag(int myFlagType){
如果(myFlagType==1){
this.flagType=myFlagType;
}else if(myFlagType==2){
this.flagType=myFlagType;
}else if(myFlagType==3){
隐藏=真实;
this.neights=myFlagType;
}
}
公共布尔getflagType(int i){
如果(i==1){
返回true;
}否则
返回false;
}
}
私人最终整数大小;
私人电池[]板;
私人空地展示板(){
对于(int i=0;i”;
resp=s.next().charAt(0);
System.out.println(“输入行:”);
row=s.nextInt()-1;
System.out.println(“输入列:”);
col=s.nextInt()-1;
如果(行>大小|列>大小){
System.out.println(“此行/列金额太高”);
展示板();
提问();
}
用户输入(行、列、真);
}
私有void initBord(int numberOfMines){
List mines=new ArrayList();
对于(int i=0;i仅对一个POS执行隐藏,必须修复
私有void用户输入(int行、int列、布尔值){
单元f=板[行][列];
如果(resp='r'){
如果(f.矿山和矿场){
返回;
}
如果(f.hidden){
雷瓦尔塞尔(世界其他地区,哥伦比亚);
如果(我的){
失败();
}else如果(f.neights==0){
if(isInBounds(行+1,列)){
小区邻居=线路板[行+1][列];
if(neighbor.hidden&&neighbor.neighbors==0){
用户输入(行+1,列,假);
}else if(neighbor.hidden&&!neighbor.mine){
revealCell(行+1,列);
}
}
if(isInBounds(第1行,第2列)){
private void userInput(int row, int col, boolean revealMines){
Cell f = board[row][col];
if (resp == 'r') {
// if this cell is a mine and we are not revealing mines, return:
if (f.mine && !revealMines)
return;
if (f.hidden) {
userInput(row,col,true);