1

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);
        }
    }
}
}
4

1 に答える 1

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]そうすると、最初の反復でアクセスすることを意味しますが、有効なインデックスが [0..6] であるため、サイズmyGrid[1][6]が定義されているため、範囲外になります。 [0..5]。myGrid[7][6]

また、次回エラー メッセージを確認すると、コンソールに次のように表示されていました: java.lang.ArrayIndexOutOfBoundsException: 6 at Main.diagonalCheckRight(Main.java:134)

クラスの名前を Main に変更しました。その 134 は、エラーが見つかった行番号とまったく同じです。

于 2012-09-20T18:12:22.730 に答える