java - 连接 4 检查获胜算法

标签 java arrays algorithm netbeans

我知道有很多关于 connect 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;
}

count 是检查是否获胜的变量,如果 count 等于或大于 4 意味着它们应该是同一玩家的 4 个或更多连续标记。

问题:有时该方法会在没有按顺序排列 4 个 token 的情况下检查是否获胜,而其他时候在按顺序排列 4 个 token 时不检查是否获胜。

最佳答案

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

public boolean areFourConnected(int player){

    // horizontalCheck 
    for (int j = 0; j<getHeight()-3 ; j++ ){
        for (int i = 0; i<getWidth(); i++){
            if (this.board[i][j] == player && this.board[i][j+1] == player && this.board[i][j+2] == player && this.board[i][j+3] == player){
                return true;
            }           
        }
    }
    // verticalCheck
    for (int i = 0; i<getWidth()-3 ; i++ ){
        for (int j = 0; j<this.getHeight(); j++){
            if (this.board[i][j] == player && this.board[i+1][j] == player && this.board[i+2][j] == player && this.board[i+3][j] == player){
                return true;
            }           
        }
    }
    // ascendingDiagonalCheck 
    for (int i=3; i<getWidth(); i++){
        for (int j=0; j<getHeight()-3; j++){
            if (this.board[i][j] == player && this.board[i-1][j+1] == player && this.board[i-2][j+2] == player && this.board[i-3][j+3] == player)
                return true;
        }
    }
    // descendingDiagonalCheck
    for (int i=3; i<getWidth(); i++){
        for (int j=3; j<getHeight(); j++){
            if (this.board[i][j] == player && this.board[i-1][j-1] == player && this.board[i-2][j-2] == player && this.board[i-3][j-3] == player)
                return true;
        }
    }
    return false;
}

关于java - 连接 4 检查获胜算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770321/

相关文章:

python - 选择适当的算法来创建和计算元素共享对的排列

java - ViewPager 中的 Recyclerviews

java - Android:数据库操作耗电吗?

java - 一种有效的分位数算法/数据结构,允许样本随着时间的推移而增加?

c++ - 总不会打印为双

javascript - Eloquent Javascript 正则表达式示例 : Why Is Exec Returning A Two-Item Array?

python - 无法理解为什么返回此值

php - 如何重新排列数组项,将依赖项移动到顶部?

java - 通过 maven a.k.a 发布时禁用 lombok 日志记录作为 maven 驱动的功能翻转

javascript - 在 Angular 4/TypeScript 中创建新的 Javascript 对象发送 json 接收 json