我正在使用Processing(Java)构建一个connect 4游戏,并且在尝试寻找获胜者时已经开始了。我发布的代码可能无论如何告诉我为什么这不起作用?或者我将如何补救它。
非常感谢任何帮助。
由于
if (whooseWon() == true) {
text = loadFont("Tahoma-Bold-50.vlw");
textFont(text, 50);
fill(255, 0, 0);
game = false;
if (whoWon == 1) {
text("Red Wins!", width/4, height/4);
text("Click to play again", 110, height/2);
} else if (whoWon == 2) {
fill(255, 255, 0);
text("yellow Wins!", width/4, height/4);
text("Click to play again", 110, height/2);
} else if (whoWon == 3) {
fill(255, 255, 0);
text("It's a Tie", width/4, height/4);
text("Click to play again", 110, height/2);
}
}
}
// click to play again functionality
void mousePressed() {
if (game == false) {
game = true;
setup();
}
}
// 21 vertical possibilities, 24 horizontal and 12 diagonally that's a total of 69 possibilities
// cols = j
// rows = i
boolean whooseWon() {
// horizontal
for (int i = 0; i < rows-3; i++) {
for (int j = 0; j < cols; j++) {
//red player
if (piece[i+1][j].getColour() == color(counter1) && piece[i+1]. [j].getColour() == color(counter1) &&
piece[i+2][j].getColour() == color(counter1) && piece[i+3][j].getColour() == color(counter1))
{
whoWon = 1;
return true;
}
if (piece[i][j].getColour() == color(counter2) && piece[i+1][j].getColour() == color(counter2) &&
piece[i+2][j].getColour() == color(counter2) && piece[i+3][j].getColour() == color(counter2))
{
whoWon = 2;
return true;
}
}
}
// vertical
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols-3; j++) {
//red player
if (piece[i][j].getColour() == color(counter1) && piece[i][j+1].getColour() == color(counter1) &&
piece[i][j+2].getColour() == color(counter1) && piece[i][j+3].getColour() == color(counter1))
{
whoWon = 1;
return true;
}
if (piece[i][j].getColour() == color(counter2) && piece[i][j+1].getColour() == color(counter2) &&
piece[i][j+2].getColour() == color(counter2) && piece[i][j+3].getColour() == color(counter2))
{
whoWon = 2;
return true;
}
}
}
//diagonal
for (int i = 0; i < rows-3; i++) {
for (int j = 0; j < cols-3; j++) {
//red player
if (piece[i][j].getColour() == color(counter1) && piece[i+1][j+1].getColour() == color(counter1) &&
piece[i+2][j+2].getColour() == color(counter1) && piece[i+3][j+3].getColour() == color(counter1))
{
whoWon = 1;
return true;
}
if (piece[i][j].getColour() == color(counter2) && piece[i+1][j+1].getColour() == color(counter2) &&
piece[i+2][j+2].getColour() == color(counter2) && piece[i+3][j+3].getColour() == color(counter2))
{
whoWon = 2;
return true;
}
}
}
//diagonal
for (int i = 0; i < rows-3; i++) {
for (int j = 0; j < cols; j++) {
//red player
if (piece[i][j].getColour() == color(counter2) && piece[i+1]
[j-1].getColour() == color(counter2) &&
piece[i+2][j-2].getColour() == color(counter2) && piece[i+3]
[j-3].getColour() == color(counter2))
{
whoWon = 1;
return true;
}
if (piece[i][j].getColour() == color(counter2) && piece[i+1]. [j+1].getColour() == color(counter2) &&
piece[i+2][j-2].getColour() == color(counter2) && piece[i+3]
[j-3].getColour() == color(counter2))
{
whoWon = 2;
return true;
}
}
}
return false;
}
`
答案 0 :(得分:1)
你让人们很难帮助你。不要在巨幅草图中发布断开连接的片段,而是尝试创建MCVE。这通常涉及从空白草图开始并且仅添加足够的代码来重复该问题,而没有任何与其没有直接关系的额外内容。我们应该能够将代码复制并粘贴到我们自己的编辑器中,点击运行,并看到与您相同的东西。否则我们都只是在猜测。您通常会在创建MCVE的过程中解决您的问题!
话虽如此,我可以帮助您尝试调试您的问题。你说你得到了ArrayIndexOutOfBounds
例外。错误在哪一行?我不是那么问你所以告诉我。我问这是因为这是你应该问自己的第一个问题。
当你将它缩小到一个特定的行时,你需要回溯以确切知道发生了什么。正在访问哪些索引?数组有多少个索引?你们两者之间存在不匹配,导致你的问题。
使用println()
函数打印出可能属于此值的每个值。这至少是i
和j
个变量,以及row
和columns
,piece.length
和piece[yourIndexHere].length
。
然后逐行浏览您的代码,直到您完全了解它正在做什么为止。再一次,如果你从MCVE工作而不是试图解决一大堆问题,你会有更好的运气。然后,如果您遇到困难,您可以使用该MCVE发布一个新问题,我们将更容易为您提供帮助。祝你好运。