我编写了一个完整的程序来播放Connect Four,但用于检查谁赢了(每次转弯后)的算法不起作用,我不知道为什么。编译时我不断得到一个奇怪的消息(异常ArrayIndexOutOfBoundsException)。任何帮助表示赞赏。
以下是代码:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class connectFourDemo extends Applet
implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
int[][] myGrid = new int[7][6];
// If piece is 0, white. If 1, red. If 2, black.
int xCoord, yCoord; // X and Y co-ordinates for mouse navigation.
int width, height;
int playerTurn = 1; // Player's turn. Default is 1 since red goes first.
int mx, my; // The mouse coordinates.
boolean isButtonPressed = false;
public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.yellow);
mx = width / 2;
my = height / 2;
addMouseListener(this);
addMouseMotionListener(this);
}
private int getXValue(int xValue) {
if (xValue < width / 7) return 0;
else if (xValue < width / 7 * 2) return 1;
else if (xValue < width / 7 * 3) return 2;
else if (xValue < width / 7 * 4) return 3;
else if (xValue < width / 7 * 5) return 4;
else if (xValue < width / 7 * 6) return 5;
else return 6;
}
private int getYValue(int yValue) {
if (yValue < width / 6) return 0;
else if (yValue < width / 6 * 2) return 1;
else if (yValue < width / 6 * 3) return 2;
else if (yValue < width / 6 * 4) return 3;
else if (yValue < width / 6 * 5) return 4;
else return 5;
}
public void verticalCheck(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 5; i >= 0; i--) {
if (myGrid[xCoord][i] == 1) {
System.out.println("Counter one in vertical check is " + counter + ".");
if (myGrid[xCoord][i - 1] == 1 && (i - 1 >= 0)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four vertically!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 5; i >= 0; i--) {
if (myGrid[xCoord][i] == 2) {
System.out.println("Counter two in vertical check is " + counter + ".");
if (myGrid[xCoord][i - 1] == 2 && (i - 1 >= 0)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four vertically!");
}
}
}
public void horizontalCheck(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
if (myGrid[i][y] == 1) {
System.out.println("Counter one in horizontal check is " + counter + ".");
if (myGrid[i + 1][y] == 1 && (i + 1 <= 6)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four horizontally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
if (myGrid[i][y] == 2) {
System.out.println("Counter two in horizontal check is " + counter + ".");
if (myGrid[i + 1][y] == 2 && (i + 1 <= 6)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four horizontally!");
}
}
}
public void diagonalCheckRight(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
for (int j = 5; j >= 0; j--) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four diagonally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 2) {
System.out.println("Counter two in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four diagonally!");
}
}
}
public void diagonalCheckLeft(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check left is " + counter + ".");
if (myGrid[i - 1][j - 1] == 1 && (i + 1 <= 6) && (j - 1 >= 0)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four diagonally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 2) {
System.out.println("Counter one in diagonal check left is " + counter + ".");
if (myGrid[i - 1][j - 1] == 2 && (i <= 6) && (j >= 0)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four diagonally!");
}
}
}
public void mouseEntered( MouseEvent e ) {
// Called when the pointer enters the applet's rectangular area.
}
public void mouseExited( MouseEvent e ) {
// Called when the pointer leaves the applet's rectangular area.
}
public void mouseClicked( MouseEvent e ) {
// Called after a press and release of a mouse button with no motion in between.
mx = e.getX();
my = e.getY();
xCoord = getXValue(mx);
yCoord = getYValue(my);
if (myGrid[xCoord][yCoord] == 0 && playerTurn == 1) { // Drop from top, fall to bottom and vice versa.
for (int y = 5; y >= yCoord; y--) {
if (myGrid[xCoord][y] == 0) {
myGrid[xCoord][y] = 1;
y = yCoord - 1;
}
}
verticalCheck(xCoord, yCoord);
horizontalCheck(xCoord, yCoord);
diagonalCheckRight(xCoord, yCoord);
diagonalCheckLeft(xCoord, yCoord);
playerTurn = 2;
}
else if (myGrid[xCoord][yCoord] == 0 && playerTurn == 2) {
for (int y = 5; y >= yCoord; y--) {
if (myGrid[xCoord][y] == 0) {
myGrid[xCoord][y] = 2;
y = yCoord - 1;
}
}
verticalCheck(xCoord, yCoord);
horizontalCheck(xCoord, yCoord);
diagonalCheckRight(xCoord, yCoord);
diagonalCheckLeft(xCoord, yCoord);
playerTurn = 1;
}
}
public void mousePressed(MouseEvent e) { // Called after a button is pressed down.
repaint();
// "Consume" the event so it won't be processed in the
// default manner by the source which generated it.
e.consume();
}
public void mouseReleased(MouseEvent e) { // Called after a button is released.
repaint();
e.consume();
}
public void mouseMoved(MouseEvent e) { // Called during motion when no buttons are down.
mx = e.getX();
my = e.getY();
mx = mx / 50; // Divides applet width by the width of each oval (50).
my = my / 50; // Divides applet height by the height of each oval (50).
showStatus("Mouse in column " + (mx + 1) + ", row " + (my + 1) + ".");
}
public void mouseDragged(MouseEvent e) { // Called during motion with buttons down.
}
public void paint(Graphics g) {
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 7; x++) {
if (myGrid[x][y] == 0) {
g.setColor(Color.white);
}
if (myGrid[x][y] == 1) {
g.setColor(Color.red);
}
if (myGrid[x][y] == 2) {
g.setColor(Color.black);
}
g.fillOval((width / 7) * x + 2, (height / 6) * y + 1, (width / 7) - 4, (height / 6) - 4);
}
}
}
}
答案 0 :(得分:5)
抛出代码,你的问题出现在“diagonalCheckRight”中,即本节:
for (int i = 0; i <= 6; i++) {
for (int j = 5; j >= 0; j--) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
您的j
索引从5开始,如果您执行myGrid[i+1][j+1]
,则表示您正在访问myGrid[1][6]
的第一次迭代,但是您已将myGrid
定义为大小为[7][6]
,因此有效指数为:[0..6] [0..5]。
下次再看一下错误信息,我的控制台显示: java.lang.ArrayIndexOutOfBoundsException:6 在Main.diagonalCheckRight(Main.java:134)
我将该类重命名为Main,而134正是我发现错误的行号。