连接4个检查一个双赢的算法 [英] Connect 4 check for a win algorithm

查看:189
本文介绍了连接4个检查一个双赢的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于连接4个检查一个双赢的问题的。问题是,大多数其他算法使我的程序都运行时错误,因为他们试图进入我的阵列外面的索引。 我的算法是这样的:

I know there is a lot of of questions regarding connect 4 check for a win. The issue is that most of other algorithms make my program have runtime errors, because they try to access an index outside of my array. My algorithm is like this:

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;
}

计数是用于检查一赢如果计数等于或大于4意味着它们应该是4个或更多个连续的令牌相同玩家的变量

count is the variable that checks for a win if count is equal or more than 4 means they should be 4 or more consecutive tokens of the same player.

问题:有时方法检查一胜而不4标记,以便和其他时候不检查一个胜利时,4令牌是为了

THE PROBLEM: sometimes the method checks for a win without being 4 tokens in order and other times does not check for a win when 4 tokens are in order.

推荐答案

看起来你的code是正确的水平和垂直的情况下。最棘手的部分是对角线情况。

Looks like your code is correct for the horizontal and vertical cases. The tricky part is the diagonal case.

让我们尝试一个画面:

输入图像的描述在这里

有关绿线,你的起始行位置为0 ...的MaxRow - 4列是0 ... startingRow -

For the green lines, your starting row position is 0 ... maxRow - 4. The column would be 0 ... startingRow -

伪code:

// 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; rowStart++){
    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;
        }
    }
}

您可以做类似的对角线走另一条路东西(从左下到右上)。

You could do something similar for diagonals going the other way (from bottom-left to top-right).

这篇关于连接4个检查一个双赢的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆