How to properly checkDiagonalWin in connect 4

后端 未结 1 1352
余生分开走
余生分开走 2021-01-23 00:37

My check vertical win and check horizontal win work perfectly fine, however i dont know what to do with my check diagonal code to make it actually check diagonal. Some guidance

相关标签:
1条回答
  • 2021-01-23 01:20

    So basically, you need a start point, you then need to determine in which direction to move

    With that idea in hand, you could use something like...

        boolean win = true;
        for (int count = 0; count < 4; count++) {
            if (row < myNumRows && row >= 0 && col < myNumColumns && col >= 0) {
                int test = myBoard[row][col];
                if (test != check) {
                    win = false;
                    break;
                }
            } else {
                break;
            }
            row += rowDelta;
            col += colDelta;
        }
    

    As the basic algorithm. All this does is checks each cell from a start point, to a total of 4 cells, the algorithm moves by the specified delta/direction and keeps checking while each cell matches the check value.

    Now, I'd wrap this in a simple method

    public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
    
        boolean win = true;
        for (int count = 0; count < 4; count++) {
            if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
                int test = grid[row][col];
                if (test != check) {
                    win = false;
                    break;
                } else {
                    break;
                }
            }
            row += rowDelta;
            col += colDelta;
        }
        return win;
    
    }
    

    which makes it simpler to call, know given any point, you can do something like...

    int startRow = ...;
    int startCol = ...;
    int player = ...;
    
    if (didWin(myBoard, player, startRow, startCol, 1, 0) || // Vertical, down
                    didWin(myBoard, 1, startRow, startCol, 0, 1) || // Right
                    didWin(myBoard, 1, startRow, startCol, 0, -1) || // Left
                    didWin(myBoard, 1, startRow, startCol, 1, 1) || // Right/down
                    didWin(myBoard, 1, startRow, startCol, -1, -1) || // Left/Up
                    didWin(myBoard, 1, startRow, startCol, 1, -1) || // Down/Left
                    didWin(myBoard, 1, startRow, startCol, -1, 1) // Up/Right
                    ) {
        // You be the winner
    
    }
    

    nb: I've left out check a vertical up direction, because it's unlikely that you could actually win this way

    ps: I'd be even more lazy and would just have a didWin method, which did the above checks and returned true or false, but I'm lazy

    So, the startRow and startCol would represent the anchor point around which you want to check and would, in this example, represent the last drop.

    This example uses a int to represent the player/token, but you could use anything, all this does is compares the token you supply with the values in the array

    0 讨论(0)
提交回复
热议问题