所以基本上我一直用Java制作扫雷克隆。我把它完美地工作,直到我添加了游戏胜利和失去部分和大开口印刷品。现在,当我运行它时,一些接近零的点不会被揭示,即使它们应该。我无法弄清楚bug在哪里。这是我的代码:
/**
* A program to play Scat Scout...
*/
import static java.lang.System.*; // so you can write out.println() instead of System.out.println()
import java.util.*;
class ScatScout {
static final int boardSize = 10;
static final Random rand = new Random();
static final boolean SCAT = true;
static final boolean CLEAR = false;
static int clearCounter = 100;
static boolean gameWon = false;
static boolean gameLost = false;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean[][] board = new boolean[boardSize+2][boardSize+2]; // scat or no scat
boolean[][] exposed = new boolean[boardSize+2][boardSize+2]; // showing or hidden
int[][] counts = new int[boardSize+2][boardSize+2]; // number of neighbors with scat
if (args.length > 0) {
rand.setSeed(Integer.parseInt(args[0]));
}
System.out.println("comment explaining game");
createScat(board); //initialize the board
compareScat(board, counts); //find how many scat are next to each spot
while(!gameWon&&!gameLost){ //keep going until game win or loss
printBoard(board, counts, exposed);
System.out.println("Enter two integers (row and column):");
expose(input.nextInt()+1, input.nextInt()+1, board, exposed, counts);
if (gameLost){
for (int i = 1; i<= board.length-2; i++){
for (int j = 1; j<= board.length-2; j++){
if (!board[i][j]){
exposed[i][j] = true;
}
}
}
printBoard(board, counts, exposed);
System.out.print("You stepped in it! Yucky!"); //game lose
break;
}
gameWon = true; //game is one if all blanks but the bombs are exposed
for(int i = 1; i<= board.length-2; i++){
for(int j = 1; j<= board[0].length-2; j++){
if(!exposed[i][j]&&!board[i][j]){
gameWon=false; //otherwise continue loop
}
}
}
if (gameWon){
System.out.print("You cleared the scat! Congrats!"); //game win message
break;
}
}
input.close();
}
public static void printBoard(boolean[][] board, int[][] counts, boolean[][] exposed){ //initialize board
int numRows = counts.length;
int numCols = counts[0].length;
System.out.println(" 0123456789"); //print the border
for(int i=1; i<=numRows-2; i++){
System.out.print(i-1 + " "); //border
for(int j=1; j<=numCols-2; j++){
if (exposed[i][j] == true){
System.out.print(counts[i][j]); //print the scat near a spot if it's exposed
}
else{
System.out.print("*"); //print unknown spot
}
}
System.out.print(" ");
System.out.print(i-1); //border
System.out.println(" ");
}
System.out.println(" 0123456789"); //border
}
public static void createScat(boolean[][] board){ //randomly seed scat into field
for(int i=1; i<= board.length-2; i++){
int x = rand.nextInt(board.length-3)+1;
int y = rand.nextInt(board.length-3)+1;
if(x!=0&&x!=11&&y!=0&&y!=11&&board[x][y]==CLEAR){
board[x][y]=SCAT; //scat in this random spot
}
}
}
public static void compareScat(boolean[][] board, int[][] counts){ //checks #scat in surrounding spots
int numRows = counts.length;
int numCols = counts[0].length;
for(int i=1; i<=numRows-2; i++){
for(int j=1; j<=numCols-2; j++){
for(int ii=i-1; ii<=i+1; ii++){
for(int jj=j-1; jj<=j+1; jj++){
if(board[ii][jj]==SCAT){ //this looks at all scat in all directions around the original spot
counts[i][j]=counts[i][j]+1; //adds to the counter if scat found around it
}
}
}
}
}
}
static void expose(int c, int r, boolean[][] board, boolean[][] exposed, int[][] counts) { //exposes chosen spot
if (exposed[r][c]) return; // nothing to do
if (board[r][c]== SCAT){
gameLost=true; //lose game if choose a spot with scat
}
exposed[r][c] = true; // expose any neighbors that have zero counts
if (counts[r][c] > 0) return;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int x = r+i;
int y = c+j;
if (!(i==1 && j==1) && x >= 1 && x < board.length-1 && y >= 1 && y < board[x].length-1) {
if (counts[x][y] == 0) {
expose(x, y, board, exposed, counts);
}
else {
exposed[x][y] = true;
}
}
}
}
}
}