我正在用java编写一个扫雷游戏,并且我继续得到“线程中的异常”主要“java.lang.NullPointerException”错误。我知道这意味着我正在尝试使用null变量,但我不确定为什么变量尚未初始化。
这是我的代码:
扫雷类
aws configure
细胞分类
import java.util.*;
public class MineSweeper {
private int numrows = 4;
private int numcols = 4;
private int numcells = numrows*numcols;
private int nummines = 4;
private Cell[][] cells;
// set up scanner for input
Scanner in = new Scanner(System.in);
public MineSweeper() {
cells = new Cell[numrows][numcols];
int minesPlaced = 0;
Random random = new Random();
// checking if any more mines need to be placed
while (minesPlaced < nummines) {
// picking random row and column
int x = random.nextInt(numrows);
int y = random.nextInt(numcols);
Cell c = cells[x][y];
// placing mines
if (c.getMined() == false) {
c.setMined(true);
minesPlaced++;
} // end if
} // end while
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell b = cells[i][j];
int num = getAdjMineCount(i,j);
b.setAdjCount(num);
} // end for
} // end for
} // end constructor MineSweeper
public int getAdjMineCount(int i, int j) {
Cell a = cells[i-1][j-1];
Cell b = cells[i-1][j];
Cell c = cells[i-1][j+1];
Cell d = cells[i][j-1];
Cell e = cells[i][j+1];
Cell f = cells[i+1][j-1];
Cell g = cells[i+1][j];
Cell h = cells[i+1][j+1];
int mines = 0;
/* if (getAdjCell(i,j,0) = isMined) mines++;
if (getAdjCell(i,j,1) = isMined) mines++;
if (getAdjCell(i,j,2) = isMined) mines++;
if (getAdjCell(i,j,3) = isMined) mines++;
if (getAdjCell(i,j,4) = isMined) mines++;
if (getAdjCell(i,j,5) = isMined) mines++;
if (getAdjCell(i,j,6) = isMined) mines++;
if (getAdjCell(i,j,7) = isMined) mines++; */
if (a.getMined() == true) mines++;
if (b.getMined() == true) mines++;
if (c.getMined() == true) mines++;
if (d.getMined() == true) mines++;
if (e.getMined() == true) mines++;
if (f.getMined() == true) mines++;
if (g.getMined() == true) mines++;
if (h.getMined() == true) mines++;
return mines;
}
public int getMarkCount() {
int marks = 0;
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell c = cells[i][j];
if (c.getMarked() == true) {
marks++;
} // end if
} // end for
} // end for
return marks;
} // end getMarkCount
public void show() {
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell c = cells[i][j];
c.show();
} // end for
} //end for
}
public void uncoverAll() {
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell c = cells[i][j];
c.setUncovered();
} // end for
} // end for
} // end uncoverAll
public Cell getAdjCell(int r, int c, int direction) {
// checking if cells are off the board
if (r == 0 && (direction == 1 || direction == 2 || direction == 3))
return null;
if (r == 3 && (direction == 5 || direction == 6 || direction == 7))
return null;
if (c == 0 && (direction == 3 || direction == 4 || direction == 5))
return null;
if (c == 3 && (direction == 1 || direction == 0 || direction == 7))
return null;
// checking each direction
if (direction == 0) return cells[r][c+1];
else if (direction == 1) return cells[r-1][c+1];
else if (direction == 2) return cells[r-1][c];
else if (direction == 3) return cells[r-1][c-1];
else if (direction == 4) return cells[r][c-1];
else if (direction == 5) return cells[r+1][c-1];
else if (direction == 6) return cells[r+1][c];
else if (direction == 7) return cells[r+1][c+1];
else return null;
}
public boolean allNonMinesUncovered() {
int count = 0;
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell c = cells[i][j];
if (c.getMined() == false && c.getCovered() == false) {
count ++;
} // end if
} // end for
} // end for
if (count == (numcells - nummines)) {
return true;
}
else
return false;
} // end allNonMinesUncovered()
public boolean allMinesMarked() {
int count = 0;
for (int i=0; i<numrows; i++) {
for (int j=0; j<numcols; j++) {
Cell c = cells[i][j];
if (c.getMarked() == true && c.getMined() == true) {
count++;
} // end if
} // end for
} // end for
if (count == nummines) {
return true;
}
else
return false;
} // end allMinesMarked()
public void game() {
// print prompt and take input
System.out.print(">");
String string1 = in.next();
while (string1.length() != 0 ){
// splitting newArray into command and arguments
String[] newArray = string1.split("\\s");
String command = newArray[0];
int arg1 = Integer.parseInt(newArray[1]);
int arg2 = Integer.parseInt(newArray[2]);
Cell c = cells[arg1][arg2];
if (command == "show") {
show(); // prints the board
}
else if (command == "u") {
if (c.getMarked() == true) {
break; }
else if (c.getMined() == true) {
uncoverAll();
System.out.println("You lose!"); }
else {
c.setUncovered();
if (allMinesMarked() == true && allNonMinesUncovered() == true) {
System.out.println("You win!");
break;
} // end if
} // end else
} // end else if for "u"
else if (command == "m") {
if (c.getCovered() == false) {
break; }
else if (c.getMarked() == true) {
c.setMarked(true); }
else {
c.setMarked(true);
if (allMinesMarked() == true && allNonMinesUncovered() == true) {
System.out.println("You win!");
break;
} // end if
} // end else
} // end else if for "m"
else if (command == "q") {
break; // exits while loop
}
else {
System.out.println("Bad command.");
}
System.out.print(">");
string1 = in.next();
} // end while
} // end game()
public static void play() {
MineSweeper mine = new MineSweeper();
mine.game();
} // end play()
} //end class MineSweeper
驱动程序类
import java.util.*;
public class Cell {
private int row, col;
private boolean covered;
private boolean marked, mined;
private int adjcount;
public Cell(int r, int c) {
row = r;
col = c;
covered = true;
marked = false;
mined = false;
adjcount = 0;
}
public void show() {
if (covered==true && marked==false)
System.out.print("?");
if (covered==true && marked==true)
System.out.print("X");
if (covered==false && mined==true)
System.out.print("*");
if (covered==false && mined==false && adjcount==0)
System.out.print("_");
if (covered==false && mined==false && adjcount > 0)
System.out.print(adjcount);
}
public boolean getMined() {
return mined;
}
public void setMined(boolean m) {
mined = m;
}
public boolean getCovered() {
return covered;
}
public void setUncovered() {
covered = false;
}
public boolean getMarked() {
return marked;
}
public void setMarked(boolean m) {
marked = m;
}
public int getAdjCount() {
return adjcount;
}
public void setAdjCount(int c) {
adjcount = c;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
我在Minesweeper级别的第31行得到错误
if(c.getMined()== false){
getMined()返回变量'mined',但显然'mined'尚未初始化。我很困惑,因为当通过Cell构造函数创建'cells'对象时,我认为'mined'被初始化为false。我的代码几乎与我的教师最终代码相同,所以我不确定我的错误在哪里。有什么想法吗?
堆栈跟踪:
public class Driver {
public static void main(String[] args) {
MineSweeper.play();
} //end of main
} //end of class Driver