JavaFXConnect 4游戏获胜方法

JavaFXConnect 4游戏获胜方法,java,javafx,Java,Javafx,我正在用JavaFX创建一个connect4游戏,这里是我的checkWin方法。我的游戏的工作方式是,棋盘上的每个单元格都有一个空白圈,只有当用户选择一列时,它才会根据玩家的不同使用setFillColor.RED或blue。除了这个方法外,一切都很好 public void checkWin(int row, int column, GridPane board) { Circle piece = ((Circle)getNodeByRowColumnIndex(row, c

我正在用JavaFX创建一个connect4游戏,这里是我的checkWin方法。我的游戏的工作方式是,棋盘上的每个单元格都有一个空白圈,只有当用户选择一列时,它才会根据玩家的不同使用setFillColor.RED或blue。除了这个方法外,一切都很好

public void checkWin(int row, int column, GridPane board) {
        Circle piece = ((Circle)getNodeByRowColumnIndex(row, column, board));

        // Horizontal check
        if (column - 3 <= 0) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row, column + i, board)) ; i++) {
                System.out.println("Checking : (" + (row) + " , " + (column + i) + ")");
                if(i == 4) hasWon = true;
            }
        } else if (column + 3 > Columns) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row, column - i, board)) ; i++) {
                System.out.println("Checking : (" + (row) + " , " + (column - i) + ")");
                if(i == 4) hasWon = true;
            }       
        }

        // Vertical check
        if (row - 3 <= 0) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row + i, column, board)) ; i++) {
                System.out.println("Checking : (" + (row + i) + " , " + (column) + ")");
                if(i == 4) hasWon = true;
                System.out.println(i);
            }

        } else if (row + 3 > Rows) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row - i, column, board)) ; i++) {
                System.out.println("Checking : (" + (row - i) + " , " + (column) + ")");
                if(i == 4) hasWon = true;
            }       
        }

        // Ascending diagonal check
        if (row - 3 <= 0 && column - 3 <= 0) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row + i, column + i, board)) ; i++) {
                System.out.println("Checking : (" + (row + i) + " , " + (column + i) + ")");
                if(i == 4) hasWon = true;
            }
        } else if (row + 3 > Rows && column + 3 > Columns) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row - i, column - i, board)) ; i++) {
                System.out.println("Checking : (" + (row - i) + " , " + (column - i) + ")");
                if(i == 4) hasWon = true;       
            }
        }

        // Descending diagonal check
        if (row + 3 > Rows && column - 3 <= 0) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row - i, column + i, board)); i++) {
                System.out.println("Checking : (" + (row - i) + " , " + (column + i) + ")");
                if(i == 4) hasWon = true;
            }

        } else if (row - 3 <= 0 && column + 3 > Columns) {
            for(int i = 1; piece.equals((Circle)getNodeByRowColumnIndex(row + i, column - i, board)); i++) {
                System.out.println("Checking : (" + (row + i) + " , " + (column - i) + ")");
                if(i == 4) hasWon = true;
            }
        }
    }

首先,一般来说,尝试将数据与表示分离。所以,保留一些数组或其他数据结构,仅在有石头的情况下存储。不要储存圆圈

第二。实际问题是密切相关的,并说明了您为什么应该这样做: 您只想比较圆的颜色。不是圆圈本身。 我只是猜测这些是JavaFX圈,因为您没有显示完整的代码。。。 因为每个圆都有半径和圆心。每个中心很可能不同,你的equals方法总是返回false


所以只比较颜色。或者更好:将数据与表示分离

首先,一般来说,尝试将数据与表示分开。所以,保留一些数组或其他数据结构,仅在有石头的情况下存储。不要储存圆圈

第二。实际问题是密切相关的,并说明了您为什么应该这样做: 您只想比较圆的颜色。不是圆圈本身。 我只是猜测这些是JavaFX圈,因为您没有显示完整的代码。。。 因为每个圆都有半径和圆心。每个中心很可能不同,你的equals方法总是返回false


所以只比较颜色。或者更好:将数据与表示分离

也许你可以告诉我们,这段代码有什么不起作用?这个方法不是在行/列/对角线上计算圆,因为它应该是这样的:除了通过实例变量hassWon,你不会从这里返回任何东西。我会把它变成一个布尔函数,然后直接返回true或false。我试过了,还是什么都没有。它似乎没有在for循环中迭代。如果我们能看到类Circle,这会有所帮助。也许你可以告诉我们,这段代码有什么不起作用?这个方法不是按行/列/对角线计算圆,因为它应该是这样的。除了通过实例变量hassWon,你不会从这里返回任何东西。我会把它变成一个布尔函数,然后直接返回true或false。我试过了,还是什么都没有。它似乎没有遍历for循环,如果我们能看到类循环,它会有所帮助。