【发布时间】:2017-01-05 15:29:11
【问题描述】:
我正在使用 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;
}
`
【问题讨论】:
-
代码原样的结果是什么?我的头脑并没有做出一个很好的 java 解释器。
-
@DanFarrell 它是一个更大的代码块的一部分,但目前我不断收到错误消息“arrayindexoutofbounds 6”我认为这回答了你的问题?谢谢
-
一个错误!伟大的!我们肯定需要查看完整的错误消息来帮助您。
-
拥有一个返回某些东西(
void除外)并且有副作用的函数是不好的做法(在你的情况下设置whoWon)同时。如果还没有人赢,为什么不直接返回whoWon和-1?除此之外,我现在尝试解析您的代码:) -
@HarrySayers 是的;但如果您的问题确实是
ArrayIndexOutOfBoundsException,那与您的问题无关。关于我提到的规则,见this SE
标签: java object for-loop processing