赢得连接4类似游戏的条件

Fro*_*and 3 c++ arrays

我有一个5x10数组,其中填充了随机值1-5.我希望能够检查3个数字(水平或垂直)是否匹配.如果不写大量的if语句,我无法想出办法.

这是随机填充的数组的代码


int i;
int rowincrement = 10;
int row = 0;
int col = 5;
int board[10][5];
int randomnum = 5;


int main(int argc, char * argv[])
{
    srand(time(NULL));

    cout << "============\n";

    while(row < rowincrement)
    {

        for(i = 0; i < 5; i++)
        {
            board[row][col] = rand()%5 + 1; 
            cout << board[row][col] << " ";
        }
        cout << endl;
        cout << "============\n";
        row++;
    }
    cout << endl;
    return 0;
}

tem*_*def 9

假设你有一个特定的起点(x,y),你很好奇,如果从这一点开始的行中有三个相等的数字.让我们考虑一下你在水平方向上看的情况.然后一种方法(忽略边界检查)将是这样的:

bool IsHorizontalMatch(int x, int y) {
    /* Get the value of the start position. */
    const int startValue = board[x][y];

    /* Confirm the two values after it match. */
    for (int i = 1; i < 3; ++i)
        if (board[x + i][y] != startValue)
            return false;

    /* If we got here, then they all match! */
    return true;
}
Run Code Online (Sandbox Code Playgroud)

您可以类似地编写这样的函数来进行垂直检查:

bool IsVerticalMatch(int x, int y) {
    /* Get the value of the start position. */
    const int startValue = board[x][y];

    /* Confirm the two values after it match. */
    for (int i = 1; i < 3; ++i)
        if (board[x][y + i] != startValue)
            return false;

    /* If we got here, then they all match! */
    return true;
}
Run Code Online (Sandbox Code Playgroud)

最后,一个用于对角线:

bool IsDiagonalDownMatch(int x, int y) {
    /* Get the value of the start position. */
    const int startValue = board[x][y];

    /* Confirm the two values after it match. */
    for (int i = 1; i < 3; ++i)
        if (board[x + i][y + i] != startValue)
            return false;

    /* If we got here, then they all match! */
    return true;
}

bool IsDiagonalUpMatch(int x, int y) {
    /* Get the value of the start position. */
    const int startValue = board[x][y];

    /* Confirm the two values after it match. */
    for (int i = 1; i < 3; ++i)
        if (board[x + i][y - i] != startValue)
            return false;

    /* If we got here, then they all match! */
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这有效,但它不是很优雅; 这三个功能看起来非常相似!幸运的是,您可以根据单个统一函数重写所有这些内容.这个想法是这样的 - 如果您注意到,所有三个功能都通过定义一些"步长"来指示您移动的方向.在水平情况下,步长为(+ 1,+ 0),在垂直情况下为(+ 0,+ 1),在对角线中为(+ 1,+ 1)或(+ 1,-1).鉴于此,您可以编写一个函数来检查一行中是否有三个值匹配:

bool IsLinearMatch(int x, int y, int stepX, int stepY) {
    /* Get the value of the start position. */
    const int startValue = board[x][y];

    /* Confirm the two values after it match. */
    for (int i = 1; i < 3; ++i)
        if (board[x + i * stepX][y + i * stepY] != startValue)
            return false;

    /* If we got here, then they all match! */
    return true;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以写

bool IsLineStartingAt(int x, int y) {
    return (IsLinearMatch(x, y, 1,  0) ||  // Horizontal
           IsLinearMatch(x, y, 0,  1)  ||  // Vertical
           IsLinearMatch(x, y, 1,  1)  ||  // Diagonal Down
           IsLinearMatch(x, y, 1, -1));    // Diagonal Up
}
Run Code Online (Sandbox Code Playgroud)

给定这个原语,您可以通过迭代所有可能的起点来检查所有可能的匹配.

希望这可以帮助!

编辑:感谢评论者帮助解决我的愚蠢错误.:-)