好的,所以我正在打井字游戏,在我加入checkWin()方法之前,它开始就很棒。一旦放入,每次单击通常放置X或O的正方形时,都会给我一个错误NullPointerException。我相信代码中说的是如果布尔2D数组“ board”为假,那么胜利就发生了,但是当我没有感叹号时也会发生。有人可以帮忙吗?谢谢。
public Boolean[][] board= new Boolean[3][3];
public boolean xTurn = true;
public void setup() {
size(900, 900);
background(255);
strokeWeight(2);
line(300, 0, 300, 900);
line(600, 0, 600, 900);
line(0, 300, 900, 300);
line(0, 600, 900, 600);
}
public void draw() {
}
public void mouseClicked() {
if (xTurn) {
xTurn();
}
else if (!xTurn) {
oTurn();
}
}
public void xTurn() {
if (mouseX > 0 && mouseX < 300 && mouseY > 0 && mouseY < 300 && board[0][0] == null) {
stroke(255, 0, 0);
line(50, 250, 250, 50);
line(250, 250, 50, 50);
board[0][0] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 0 && mouseY < 300 && board[0][1] == null) {
stroke(255, 0, 0);
line(350, 250, 550, 50);
line(550, 250, 350, 50);
board[0][1] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 0 && mouseY < 300 && board[0][2] == null) {
stroke(255, 0, 0);
line(650, 50, 850, 250);
line(650, 250, 850, 50);
board[0][2] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 0 && mouseX < 300 && mouseY > 300 && mouseY < 600 && board[1][0] == null) {
stroke(255, 0, 0);
line(50, 550, 250, 350);
line(250, 550, 50, 350);
board[1][0] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 300 && mouseY < 600 && board[1][1] == null) {
stroke(255, 0, 0);
line(350, 550, 550, 350);
line(550, 550, 350, 350);
board[1][1] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 300 && mouseY < 600 && board[1][2] == null) {
stroke(255, 0, 0);
line(650, 350, 850, 550);
line(650, 550, 850, 350);
board[1][2] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 0 && mouseX < 300 && mouseY > 600 && mouseY < 900 && board[2][0] == null) {
stroke(255, 0, 0);
line(50, 850, 250, 650);
line(250, 850, 50, 650);
board[2][0] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 600 && mouseY < 900 && board[2][1] == null) {
stroke(255, 0, 0);
line(350, 850, 550, 650);
line(550, 850, 350, 650);
board[2][1] = true;
xTurn = false;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 600 && mouseY < 900 && board[2][2] == null) {
stroke(255, 0, 0);
line(650, 650, 850, 850);
line(650, 850, 850, 650);
board[2][2] = true;
xTurn = false;
checkWin();
}
}
public void oTurn() {
if (mouseX > 0 && mouseX < 300 && mouseY > 0 && mouseY < 300 && board[0][0] == null) {
stroke(0, 0, 255);
ellipse(150, 150, 200, 200);
board[0][0] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 0 && mouseY < 300 && board[0][1] == null) {
stroke(0, 0, 255);
ellipse(450, 150, 200, 200);
board[0][1] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 0 && mouseY < 300 && board[0][2] == null) {
stroke(0, 0, 255);
ellipse(750, 150, 200, 200);
board[0][2] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 0 && mouseX < 300 && mouseY > 300 && mouseY < 600 && board[1][0] == null) {
stroke(0, 0, 255);
ellipse(150, 450, 200, 200);
board[1][0] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 300 && mouseY < 600 && board[1][1] == null) {
stroke(0, 0, 255);
ellipse(450, 450, 200, 200);
board[1][1] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 300 && mouseY < 600 && board[1][2] == null) {
stroke(0, 0, 255);
ellipse(750, 450, 200, 200);
board[1][2] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 0 && mouseX < 300 && mouseY > 600 && mouseY < 900 && board[2][0] == null) {
stroke(0, 0, 255);
ellipse(150, 750, 200, 200);
board[2][0] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 300 && mouseX < 600 && mouseY > 600 && mouseY < 900 && board[2][1] == null) {
stroke(0, 0, 255);
ellipse(450, 750, 200, 200);
board[2][1] = false;
xTurn = true;
checkWin();
}
else if (mouseX > 600 && mouseX < 900 && mouseY > 600 && mouseY < 900 && board[2][2] == null) {
stroke(0, 0, 255);
ellipse(750, 750, 200, 200);
board[2][2] = false;
xTurn = true;
checkWin();
}
}
public void checkWin() {
if (!board[0][0] && !board[0][1] && !board[0][2]) {
stroke(0, 255, 0);
line(100, 150, 800, 150);
}
}
最佳答案
public Boolean[][] board= new Boolean[3][3];
每个条目都为空,您需要在使用前确保其已填充...