connect4对角win算法

j0pj023g  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(108)

我知道已经有很多关于连接4对角线检查的问题,我也试着遵循它们,但它们似乎对我的代码不起作用。我尝试在7x62d数组上使用从右上到左下的检查。我已经完成了水平和垂直检查,我只是不知道如何让我的对角线检查工作。

public int winnerCheck (int[] [] board, int lastRow, int lastColumn)
{
    int check = 0;
    int player = board[lastRow][lastColumn];

    //Horizontal check
    for (int i = 0; i<board[lastRow].length; i++) {
        if (board[lastRow][i] == player) {
            check++;
        }
        else
            check = 0;

        if (check == 4) {
            break;
        }
    }

    if (check == 4) {
        return player;
    }
    else
        check = 0;

    //Vertical check
    for (int i = 0; i<board.length; i++) {
        if (board[i][lastColumn] == player) {
            check++;
        }
        else
            check = 0;

        if (check == 4) {
            break;
        }
    }

    if (check == 4) {
        return player;
    }
    else
        check = 0;

//Diagonal check

    for (int i = 3; i >=0; i--) {
        for (int j = 4; j>=0; j-- ) {
            if (board[i][j] == player && board[i-1][j-1]==player && board[i-2][j-2]==player && board[i-3][j-3]==player)
                check++;
            else
                check = 0;
        }
        if (check==1)
            break;
    }

    if (check==1) {
        return player;
    }

    return 0;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题