JavaFX Connect 4 游戏获胜方法

1 java javafx

我正在 JavaFX 中创建 Connect 4 游戏,这是我的checkWin方法。我的游戏的工作方式是,每个单元格中board都有一个空白圆圈,只有当用户选择一列时,它才会有setFill(Color.RED)(或蓝色,取决于玩家)。除此方法外,一切正常。

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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sch*_*tod 5

首先,一般来说,尝试将您的数据与您的表示分开。因此,请保留一些数组或其他数据结构,仅在有石头和什么的情况下才存储。不要存储圈子

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

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