java - 连接四胜利检查不工作 Java

标签 java algorithm

<分区>

我编写了一个完整的程序来玩 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);
        }
    }
}
}

最佳答案

运行代码,你的问题在“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 正是我发现错误的行号。

关于java - 连接四胜利检查不工作 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12518202/

相关文章:

algorithm - Bogo排序平均运行时间解释

algorithm - 从邻接表中找到两个节点的最低公共(public)祖先

algorithm - 递归树算法的运行时复杂度是多少,它在不同子树大小的数量上是二次的?

Java 象棋棋盘

java - 拥有一个线程池比多个线程池更好的设计

java - 2,3维LIST<String, String, String>如何实现? java

java - 如何通过按钮将 Activity 链接到 Fragment

java - 黑莓 map 字段为空?

java - 是否可以通过 JNI 将静态库链接到 Java?

c - 如何将二维数组从最大到最小排序