Java 连接4检查win算法

Java 连接4检查win算法,java,arrays,algorithm,netbeans,Java,Arrays,Algorithm,Netbeans,我知道有很多关于连接4检查是否成功的问题。问题是,大多数其他算法使我的程序出现运行时错误,因为它们试图访问数组之外的索引。 我的算法是这样的: private int checkWin(int[][] gridTable,int rowNum,int colNum, int maxRow, int maxCol) { // For checking whether any win or lose condition is reached. Returns 1 if win or lose i

我知道有很多关于连接4检查是否成功的问题。问题是,大多数其他算法使我的程序出现运行时错误,因为它们试图访问数组之外的索引。 我的算法是这样的:

private int checkWin(int[][] gridTable,int rowNum,int colNum, int maxRow, int maxCol) 
{
//  For checking whether any win or lose condition is reached. Returns 1 if win or lose is reached. else returns 0
//  gridTable[][] is the game matrix(can be any number of rows and columns between 4 and 40)
//  colNum is the column number where the last token was placed
//  rowNum is the row number where the last token was placed
//  maxRow is the number of rows in my grid
//  maxCol is the number of columns in my grid

int player = gridTable[rowNum][colNum]; //player ID
int count=0;

// Horizontal check
for (int i=0;i<maxCol;i++)
{
    if (gridTable[rowNum][i]==player)
        count++;
    else
        count=0;

    if (count>=4)
        return 1;
}
//Vertical check
for (int i=0;i<maxRow;i++)
{
    if (gridTable[i][colNum]==player)
        count++;
    else
        count=0;

    if (count>=4)
        return 1;
} 
count=0;
// 4 in a row diagonally
for(int i=colNum+1,j=rowNum+1;i<maxRow && j<maxCol;i++,j++) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}
// 4 in a row diagonally
for(int i=colNum-1,j=rowNum-1;i>=0 && j>=0;i--,j--) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}
// 4 in a row diagonally
for(int i=colNum+1,j=rowNum-1;i<maxRow && j>=0;i++,j--) 
{ 
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}

for(int i=colNum-1,j=rowNum+1;i>=0 && j<maxCol;i--,j++) 
{ // 4 in a row diagonally
    if(gridTable[j][i]!=player)
    {
        count=1;
        break;        
    }
    count++;
}

if(count>=4)
    return 1;

return 0;
}
private int checkWin(int[][]gridTable、int rowNum、int colNum、int maxRow、int maxCol)
{
//用于检查是否达到任何赢或输条件。如果达到赢或输,则返回1。否则返回0
//gridTable[]是游戏矩阵(可以是4到40之间的任意数量的行和列)
//colNum是最后一个标记所在的列号
//rowNum是放置最后一个标记的行号
//maxRow是我的网格中的行数
//maxCol是我的网格中的列数
int player=gridTable[rowNum][colNum];//播放器ID
整数计数=0;
//水平检查
对于(int i=0;i=4)
返回1;
}
//垂直检查
对于(int i=0;i=4)
返回1;
} 
计数=0;
//4成对角线排列
对于(int i=colNum+1,j=rowNum+1;i=0;i--,j--)
{ 
if(gridTable[j][i]!=玩家)
{
计数=1;
打破
}
计数++;
}
//4成对角线排列
对于(int i=colNum+1,j=rowNum-1;i=0;i++,j--)
{ 
if(gridTable[j][i]!=玩家)
{
计数=1;
打破
}
计数++;
}
对于(int i=colNum-1,j=rowNum+1;i>=0&&j=4)
返回1;
返回0;
}
count是一个变量,如果count等于或大于4,则检查是否获胜,这意味着它们应该是同一玩家的4个或更多连续令牌


问题:有时该方法在没有按顺序排列4个令牌的情况下检查赢,而在其他情况下,当4个令牌按顺序排列时,该方法不检查赢。

因此,在深入研究您的代码后,似乎对角线检查只能在单个方向上赢(如果我将令牌添加到最低的行和最低的列,会发生什么情况?)

相反,无论您在哪个方向签入,基本检查算法始终是相同的过程

您需要一个起点(x/y)和x/y增量(移动方向)。您可以将其归纳为一种方法

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;
            }
        }
        row += rowDelta;
        col += colDelta;
    }
    return win;

}
哪个输出

Vertical
Win
Win
Lose

Horizontal
Win
Win
Lose

Diag
Win
Win
Lose
现在,你可以把它总结成

public boolean didWin(int[][] grid, int check, int row, int col) {
    return didWin(grid, check, row, col, 1, 0) ||
                    didWin(grid, check, row, col, -1, 0) ||
                    didWin(grid, check, row, col, 0, 1) ||
                    didWin(grid, check, row, col, 0, -1) ||
                    didWin(grid, check, row, col, 1, 1) ||
                    didWin(grid, check, row, col, -1, -1) ||
                    didWin(grid, check, row, col, -1, 1) ||
                    didWin(grid, check, row, col, 1, -1);
}
所以,使用类似于

int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 4, 3, 1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 1, 3, -1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 0, 3, 1, 0) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, 0, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 0, 0, 1) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1, 1, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, -1, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 1, 2, 1, 1) ? "Win" : "Lose");
int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 1, 3) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 4, 3) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");
Vertical
Win
Win

Horizontal
Win
Win

Diag
Win
Win
它打印出类似于

int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 4, 3, 1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 1, 3, -1, 0) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 0, 3, 1, 0) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, 0, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 0, 0, 1) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1, 1, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4, -1, -1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 1, 2, 1, 1) ? "Win" : "Lose");
int[][] gridTable = new int[ROWS][COLUMNS];

gridTable[ROWS - 1][3] = 1;
gridTable[ROWS - 2][3] = 1;
gridTable[ROWS - 3][3] = 1;
gridTable[ROWS - 4][3] = 1;

System.out.println("Vertical");

System.out.println(didWin(gridTable, 1, ROWS - 1, 3) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, ROWS - 4, 3) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[3][1] = 1;
gridTable[3][2] = 1;
gridTable[3][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Horizontal");
System.out.println(didWin(gridTable, 1, 3, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");

gridTable = new int[ROWS][COLUMNS];
gridTable[0][1] = 1;
gridTable[1][2] = 1;
gridTable[2][3] = 1;
gridTable[3][4] = 1;

System.out.println("");
System.out.println("Diag");
System.out.println(didWin(gridTable, 1, 0, 1) ? "Win" : "Lose");
System.out.println(didWin(gridTable, 1, 3, 4) ? "Win" : "Lose");
Vertical
Win
Win

Horizontal
Win
Win

Diag
Win
Win

我想补充一点,这种方法只有在连续提供4个芯片的正确开始时才有效。例如,didWin(gridTable,1,3,3)将为水平检查提供false而不是true,因为循环只能检查一个方向

其目的不是提供一个“全面的、开箱即用的”解决方案,而是一个可以从中开发更广泛解决方案的概念(我的意思是,我不希望人们真的不得不思考;)。我还基于这样的想法设计了解决方案:OP会知道最后一块放在哪里,即起点;)

通过稍微修改
didWin
方法,可以通过
n
从任意点检查
n
网格

public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
    boolean match = false;
    int matches = 0;
    while (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
        int test = grid[row][col];
        if (test != check && match) {
            break;
        } else if (test == check) {
            match = true;
            matches++;
        }
        row += rowDelta;
        col += colDelta;
    }
    return matches == 4;
}
并且能够让它工作。有时候,答案并不是一个完整的解决方案,而是一个想法的种子,它能把一个人带到一个新的地方;)


我进一步的增强包括提供预期连体件的数量,但我非常确定这是一个我真的不需要演示的增强;)

看起来您的代码对于水平和垂直情况都是正确的。棘手的部分是对角线的情况

让我们尝试一张图片:

对于绿线,起始行位置为0。。。马克斯罗-4。该列将为0。。。斯塔丁罗-

伪代码:

// top-left to bottom-right - green diagonals
for( rowStart = 0; rowStart < rowMax - 4; rowStart++){
    count = 0;
    int row, col;
    for( row = rowStart, col = 0; row < rowMax && col < colMax; row++, col++ ){
        if(gridTable[row][col] == player){
            count++;
            if(count >= 4) return 1;
        }
        else {
            count = 0;
        }
    }
}

// top-left to bottom-right - red diagonals
for( colStart = 1; colStart < colMax - 4; colStart++){
    count = 0;
    int row, col;
    for( row = 0, col = colStart; row < rowMax && col < colMax; row++, col++ ){
        if(gridTable[row][col] == player){
            count++;
            if(count >= 4) return 1;
        }
        else {
            count = 0;
        }
    }
}
//从左上到右下-绿色对角线
对于(rowStart=0;rowStart=4)返回1;
}
否则{
计数=0;
}
}
}
//从左上到右下-红色对角线
用于(colStart=1;colStart=4)返回1;
}
否则{
计数=0;
}
}
}

你可以对对角线做类似的事情(从左下到右上)。

由于某种原因,我不太喜欢计数器,所以我这样做(它适用于不同尺寸的电路板)

public boolean-areFourConnected(int-player){
//水平检查

对于(int j=0;j这对我来说是有效的,它也不像看起来那样花了那么长时间:
这些方法对x和o具有行、列、对角线和反对角线 )

public static void checkVertO(){
    if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' && board[3][0] == 'O' || board[1][0] == 'O' && board[2][0] == 'O' && board[3][0] == 'O' && board[4][0] == 'O' ||
    board[2][0] == 'O' && board[3][0] == 'O' && board[4][0] == 'O' && board[5][0] == 'O' || board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' || 
    board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O' && board[4][1] == 'O' || board[2][1] == 'O' && board[3][1] == 'O' && board[4][1] == 'O' && board[5][1] == 'O' || 
    board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' || board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O' && board[4][2] == 'O' ||
    board[2][2] == 'O' && board[3][2] == 'O' && board[4][2] == 'O' && board[5][2] == 'O' || board[0][3] == 'O' && board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' || 
    board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O' && board[4][3] == 'O' || board[2][3] == 'O' && board[3][3] == 'O' && board[4][3] == 'O' && board[5][3] == 'O' ||
    board[0][4] == 'O' && board[1][4] == 'O' && board[2][4] == 'O' && board[3][4] == 'O' || board[1][4] == 'O' && board[2][4] == 'O' && board[3][4] == 'O' && board[4][4] == 'O' ||
    board[2][4] == 'O' && board[3][4] == 'O' && board[4][4] == 'O' && board[5][4] == 'O' || board[0][5] == 'O' && board[1][5] == 'O' && board[2][5] == 'O' && board[3][5] == 'O' || 
    board[1][5] == 'O' && board[2][5] == 'O' && board[3][5] == 'O' && board[4][5] == 'O' || board[2][5] == 'O' && board[3][5] == 'O' && board[4][5] == 'O' && board[5][5] == 'O' ||
    board[0][6] == 'O' && board[1][6] == 'O' && board[2][6] == 'O' && board[3][6] == 'O' || board[1][6] == 'O' && board[2][6] == 'O' && board[3][6] == 'O' && board[4][6] == 'O'||
    board[2][6] == 'O' && board[3][6] == 'O' && board[4][6] == 'O' && board[5][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkHorzO(){
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' && board[0][3] == 'O' || board[0][1] == 'O' && board[0][2] == 'O' && board[0][3] == 'O' && board[0][4] == 'O' ||
    board[0][2] == 'O' && board[0][3] == 'O' && board[0][4] == 'O' && board[0][5] == 'O' || board[0][3] == 'O' && board[0][4] == 'O' && board[0][5] == 'O' && board[0][6] == 'O' ||
    board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O' || board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O' && board[1][4] == 'O' ||
    board[1][2] == 'O' && board[1][3] == 'O' && board[1][4] == 'O' && board[1][5] == 'O' || board[1][3] == 'O' && board[1][4] == 'O' && board[1][5] == 'O' && board[1][6] == 'O' ||
    board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' || board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O' && board[2][4] == 'O' ||
    board[2][2] == 'O' && board[2][3] == 'O' && board[2][4] == 'O' && board[2][5] == 'O' || board[2][3] == 'O' && board[2][4] == 'O' && board[2][5] == 'O' && board[2][6] == 'O' ||
    board[3][0] == 'O' && board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' || board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O' && board[3][4] == 'O' ||
    board[3][2] == 'O' && board[3][3] == 'O' && board[3][4] == 'O' && board[3][5] == 'O' || board[3][3] == 'O' && board[3][4] == 'O' && board[3][5] == 'O' && board[3][6] == 'O' ||
    board[4][0] == 'O' && board[4][1] == 'O' && board[4][2] == 'O' && board[4][3] == 'O' || board[4][1] == 'O' && board[4][2] == 'O' && board[4][3] == 'O' && board[4][4] == 'O' ||
    board[4][2] == 'O' && board[4][3] == 'O' && board[4][4] == 'O' && board[4][5] == 'O' || board[4][3] == 'O' && board[4][4] == 'O' && board[4][5] == 'O' && board[4][6] == 'O' ||
    board[5][0] == 'O' && board[5][1] == 'O' && board[5][2] == 'O' && board[5][3] == 'O' || board[5][1] == 'O' && board[5][2] == 'O' && board[5][3] == 'O' && board[5][4] == 'O' ||
    board[5][2] == 'O' && board[5][3] == 'O' && board[5][4] == 'O' && board[5][5] == 'O' || board[5][3] == 'O' && board[5][4] == 'O' && board[5][5] == 'O' && board[5][6] == 'O' ){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkHorzX(){
    if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' && board[0][3] == 'X' || board[0][1] == 'X' && board[0][2] == 'X' && board[0][3] == 'X' && board[0][4] == 'X' ||
    board[0][2] == 'X' && board[0][3] == 'X' && board[0][4] == 'X' && board[0][5] == 'X' || board[0][3] == 'X' && board[0][4] == 'X' && board[0][5] == 'X' && board[0][6] == 'X' ||
    board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X' || board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X' && board[1][4] == 'X' ||
    board[1][2] == 'X' && board[1][3] == 'X' && board[1][4] == 'X' && board[1][5] == 'X' || board[1][3] == 'X' && board[1][4] == 'X' && board[1][5] == 'X' && board[1][6] == 'X' ||
    board[2][0] == 'X' && board[2][3] == 'X' && board[2][4] == 'X' && board[2][5] == 'X' || board[2][3] == 'X' && board[2][4] == 'X' && board[2][5] == 'X' && board[2][6] == 'X' ||
    board[3][0] == 'X' && board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' || board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X' && board[3][4] == 'X' ||
    board[3][2] == 'X' && board[3][3] == 'X' && board[3][4] == 'X' && board[3][5] == 'X' || board[3][3] == 'X' && board[3][4] == 'X' && board[3][5] == 'X' && board[3][6] == 'X' ||
    board[4][0] == 'X' && board[4][3] == 'X' && board[4][4] == 'X' && board[4][5] == 'X' || board[4][3] == 'X' && board[4][4] == 'X' && board[4][5] == 'X' && board[4][6] == 'X' ||
    board[5][0] == 'X' && board[5][1] == 'X' && board[5][2] == 'X' && board[5][3] == 'X' || board[5][1] == 'X' && board[5][2] == 'X' && board[5][3] == 'X' && board[5][4] == 'X' ||
    board[5][2] == 'X' && board[5][3] == 'X' && board[5][4] == 'X' && board[5][5] == 'X' || board[5][3] == 'X' && board[5][4] == 'X' && board[5][5] == 'X' && board[5][6] == 'X' ){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkDiagX(){
    if (board[2][0] == 'X' && board[3][1] == 'X' && board[4][2] == 'X' && board[5][3] == 'X'|| board[1][0] == 'X' && board[2][1] == 'X' && board[3][2] == 'X' && board[4][3] == 'X'||
    board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X'|| board[0][1] == 'X' && board[1][2] == 'X' && board[2][3] == 'X' && board[3][4] == 'X'||
    board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X' && board[4][4] == 'X'|| board[2][1] == 'X' && board[3][2] == 'X' && board[4][3] == 'X' && board[5][4] == 'X'||
    board[0][2] == 'X' && board[1][3] == 'X' && board[2][4] == 'X' && board[3][5] == 'X'|| board[1][2] == 'X' && board[2][3] == 'X' && board[3][4] == 'X' && board[4][5] == 'X'||
    board[2][2] == 'X' && board[3][3] == 'X' && board[4][4] == 'X' && board[5][5] == 'X'|| board[0][3] == 'X' && board[1][4] == 'X' && board[2][5] == 'X' && board[3][6] == 'X'||
    board[1][3] == 'X' && board[2][4] == 'X' && board[3][5] == 'X' && board[4][6] == 'X'|| board[2][3] == 'X' && board[3][4] == 'X' && board[4][5] == 'X' && board[5][6] == 'X'){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkDiagO(){
    if (board[2][0] == 'O' && board[3][1] == 'O' && board[4][2] == 'O' && board[5][3] == 'O'|| board[1][0] == 'O' && board[2][1] == 'O' && board[3][2] == 'O' && board[4][3] == 'O'||
    board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O'|| board[0][1] == 'O' && board[1][2] == 'O' && board[2][3] == 'O' && board[3][4] == 'O'||
    board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O' && board[4][4] == 'O'|| board[2][1] == 'O' && board[3][2] == 'O' && board[4][3] == 'O' && board[5][4] == 'O'||
    board[0][2] == 'O' && board[1][3] == 'O' && board[2][4] == 'O' && board[3][5] == 'O'|| board[1][2] == 'O' && board[2][3] == 'O' && board[3][4] == 'O' && board[4][5] == 'O'||
    board[2][2] == 'O' && board[3][3] == 'O' && board[4][4] == 'O' && board[5][5] == 'O'|| board[0][3] == 'O' && board[1][4] == 'O' && board[2][5] == 'O' && board[3][6] == 'O'||
    board[1][3] == 'O' && board[2][4] == 'O' && board[3][5] == 'O' && board[4][6] == 'O'|| board[2][3] == 'O' && board[3][4] == 'O' && board[4][5] == 'O' && board[5][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

public static void checkAntiDiagX(){
    if (board[3][0] == 'X' && board[2][1] == 'X' && board[1][2] == 'X' && board[0][3] == 'X'|| board[4][0] == 'X' && board[3][1] == 'X' && board[2][2] == 'X' && board[1][3] == 'X'||
    board[3][1] == 'X' && board[2][2] == 'X' && board[1][3] == 'X' && board[0][4] == 'X'|| board[5][0] == 'X' && board[4][1] == 'X' && board[3][2] == 'X' && board[2][3] == 'X'||        
    board[4][1] == 'X' && board[3][2] == 'X' && board[2][3] == 'X' && board[1][4] == 'X'|| board[3][2] == 'X' && board[2][2] == 'X' && board[1][4] == 'X' && board[0][5] == 'X'||
    board[5][1] == 'X' && board[4][2] == 'X' && board[3][3] == 'X' && board[2][4] == 'X'|| board[4][2] == 'X' && board[3][3] == 'X' && board[2][4] == 'X' && board[1][5] == 'X'||
    board[3][3] == 'X' && board[2][4] == 'X' && board[1][5] == 'X' && board[0][6] == 'X'|| board[5][2] == 'X' && board[4][3] == 'X' && board[3][4] == 'X' && board[2][5] == 'X'||
    board[4][3] == 'X' && board[3][4] == 'X' && board[2][5] == 'X' && board[1][6] == 'X'|| board[5][3] == 'X' && board[4][4] == 'X' && board[3][5] == 'X' && board[2][6] == 'X'){
        System.out.println("Game over, X won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

 public static void checkAntiDiagO(){
    if (board[3][0] == 'O' && board[2][1] == 'O' && board[1][2] == 'O' && board[0][3] == 'O'|| board[4][0] == 'O' && board[3][1] == 'O' && board[2][2] == 'O' && board[1][3] == 'O'||
    board[3][1] == 'O' && board[2][2] == 'O' && board[1][3] == 'O' && board[0][4] == 'O'|| board[5][0] == 'O' && board[4][1] == 'O' && board[3][2] == 'O' && board[2][3] == 'O'||        
    board[4][1] == 'O' && board[3][2] == 'O' && board[2][3] == 'O' && board[1][4] == 'O'|| board[3][2] == 'O' && board[2][2] == 'O' && board[1][4] == 'O' && board[0][5] == 'O'||
    board[5][1] == 'O' && board[4][2] == 'O' && board[3][3] == 'O' && board[2][4] == 'O'|| board[4][2] == 'O' && board[3][3] == 'O' && board[2][4] == 'O' && board[1][5] == 'O'||
    board[3][3] == 'O' && board[2][4] == 'O' && board[1][5] == 'O' && board[0][6] == 'O'|| board[5][2] == 'O' && board[4][3] == 'O' && board[3][4] == 'O' && board[2][5] == 'O'||
    board[4][3] == 'O' && board[3][4] == 'O' && board[2][5] == 'O' && board[1][6] == 'O'|| board[5][3] == 'O' && board[4][4] == 'O' && board[3][5] == 'O' && board[2][6] == 'O'){
        System.out.println("Game over, O won.");
        printBoard();
        doIt();
    }else {
        return;
    }
}

如果有人仍然需要这个解决方案,我会用c#编写一个函数,然后放入GitHub repo

/// <summary>
    /// WinnerCalc check if blue or red win the game.
    /// </summary>
    /// <returns>Return 1 if 1 win and 2 if 2 win and -1 if no one win.</returns>
    /// <param name="matrix">2d array</param>
    /// <param name="lastRow">The row number.</param>
    /// <param name="lastColumn">The column number.</param>
    public static int WinnerCalc(int[,] matrix, int lastRow, int lastColumn)
    {
        int lastValue = matrix[lastRow, lastColumn];
        Console.WriteLine("drop in row: " + (lastRow) + " and column: " + (lastColumn) + " , the value is: " + lastValue);
        int rows = matrix.GetLength(0); //6
        int columns = matrix.GetLength(1); //7
        Console.WriteLine("number of rows is " + rows + ", and number of colums is " + columns);

        int numToWin = 4;
        int winner = -1;//is now one win tha game
        int match;

        match = 0;
        //check Horizontal
        for (int c = 0; c < columns; c++)
        {
            int currentValue = matrix[lastRow, c];
            if (currentValue == lastValue)
                match++;
            else match = 0;
            if(match == numToWin)
            {
                winner = lastValue;
                break;
            }
        }
        if (winner != -1)
        {
            Console.WriteLine("win Horizontal !");
            return winner;
        }

        match = 0;
        //check Vertical
        for (int r = 0; r < rows; r++)
        {
            int currentValue = matrix[r, lastColumn];
            if (currentValue == lastValue)
                match++;
            else match = 0;
            if (match == numToWin)
            {
                winner = lastValue;
                break;
            }
        }
        if (winner != -1)
        {
            Console.WriteLine("win Vertical !");
            return winner;
        }

        //check diagonal top-left to bottom-right - include middle
        match = 0;
        for (int r = 0; r <= rows - 4; r++)
        {
            int rowPosition = r;
            for (int column = 0; column < columns && rowPosition < rows; column++)
            {
                int currentValue = matrix[rowPosition, column];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                rowPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Top left! - include middle");
            return winner;
        }

        //check diagonal top-left to bottom-right - after middle
        match = 0;
        for (int c = 1; c <= columns - 4; c++)
        {
            int columnPosition = c;
            for (int row = 0; row < rows && columnPosition < columns; row++)
            {
                int currentValue = matrix[row, columnPosition];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                columnPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Top left! - after middle");
            return winner;
        }


        //check diagonal bottom-left to top-right - include middle
        match = 0;
        for (int r = rows - 1; r >= rows - 4; r--)
        {
            int rowPosition = r;
            for (int column = 0; column < columns && rowPosition < rows && rowPosition >= 0; column++)
            {
                int currentValue = matrix[rowPosition, column];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                rowPosition--;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Bottom left! - include middle");
            return winner;
        }


        //check diagonal bottom-left to top-right - after middle
        match = 0;
        for (int c = 1; c < columns; c++)
        {
            int columnPosition = c;
            for (int row = rows - 1; row < rows && columnPosition < columns && columnPosition >= 1; row--)
            {
                int currentValue = matrix[row, columnPosition];
                if (currentValue == lastValue)
                    match++;
                else match = 0;
                if (match == numToWin)
                {
                    winner = lastValue;
                    break;
                }
                columnPosition++;
            }
            if (winner != -1) break;
        }
        if (winner != -1)
        {
            Console.WriteLine("win Diagonal Bottom left! - after middle");
            return winner;
        }



        return winner; // no winner return -1
    }

}
//
///WinnerCalc检查蓝色或红色是否赢得比赛。
/// 
///如果1赢,则返回1;如果2赢,则返回2;如果没有人赢,则返回-1。
///二维阵列
///行号。
///列号。
公共静态int WinnerCalc(int[,]矩阵,int lastRow,int lastColumn)
{
int lastValue=矩阵[lastRow,lastColumn];
WriteLine(“插入行:”+(lastRow)+“和列:”+(lastColumn)+”,值为:“+lastValue”);
int rows=matrix.GetLength(0);//6
int columns=matrix.GetLength(1);//7
Console.WriteLine(“行数为“+行+”,列数为“+列”);
int numToWin=4;
int winner=-1;//现在比游戏赢一场
整数匹配;
匹配=0;
//水平检查
for(int c=0;c