gpt4 book ai didi

java - 井字游戏 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 03:14:38 25 4
gpt4 key购买 nike

好吧,我正在玩井字棋,在我放入 checkWin() 方法之前,它开始得很好。一旦我把它放进去,每次我点击一个通常放置 X 或 O 的方 block 时,它都会给我一个错误 NullPointerException。我相信代码中所说的,如果 boolean 二维数组“板”为假,那么胜利就会发生,但当我没有感叹号品脱时也会发生这种情况。有人可以帮忙吗?谢谢。

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

每个条目都是空的,您需要在使用之前确保其已填充...

关于java - 井字游戏 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32926375/

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