胜利条件为连接-4般的游戏 [英] Win conditions for a connect-4 like game

查看:133
本文介绍了胜利条件为连接-4般的游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充了随机值1-5中的一个5×10阵列。我希望能够检查时,3个数字,无论是水平,或垂直,相匹配。我不能想出一个办法做到这一点,而无需编写一吨的if语句。

下面是code的随机填充阵列



INT I;
INT rowincrement = 10;
INT行= 0;
INT COL = 5;
INT板[10] [5];
INT randomnum = 5;
INT主(INT ARGC,CHAR *的argv [])
{
    函数srand(时间(NULL));    COUT&LT与LT============ \\ N的;    而(行&LT rowincrement)
    {        对于(i = 0; I&LT 5;我++)
        {
            板[行] [COL] =兰特()%5 + 1;
            COUT&LT和LT板[行] [COL&LT与LT;
        }
        COUT&LT和LT ENDL;
        COUT&LT与LT============ \\ N的;
        行++;
    }
    COUT&LT和LT ENDL;
    返回0;
}


解决方案

假设你有一些特殊的出发点(X,Y),你是好奇,如果有在开始在这一点上一排三个相等。让我们考虑流向何方您正在寻找在水平方向上的情况。然后有办法做到这一点(忽略边界检查)会是这样的:

 布尔IsHorizo​​ntalMatch(INT X,int y)对{
    / *获取的起始位置的值。 * /
    const int的在startValue =板[X] [Y]    / *确认两个值是比赛结束后。 * /
    对(INT I = 1;我3; ++ⅰ)
        如果(板[X + I] [Y]!=在startValue)
            返回false;    / *如果我们来到这里,那么它们都匹配! * /
    返回true;
}

您同样可以像这样写一个函数用于垂直检查:

 布尔IsVerticalMatch(INT X,int y)对{
    / *获取的起始位置的值。 * /
    const int的在startValue =板[X] [Y]    / *确认两个值是比赛结束后。 * /
    对(INT I = 1;我3; ++ⅰ)
        如果(板[X] [Y + I]!=在startValue)
            返回false;    / *如果我们来到这里,那么它们都匹配! * /
    返回true;
}

最后,一个用于对角线

 布尔IsDiagonalDownMatch(INT X,int y)对{
    / *获取的起始位置的值。 * /
    const int的在startValue =板[X] [Y]    / *确认两个值是比赛结束后。 * /
    对(INT I = 1;我3; ++ⅰ)
        如果(板[X + I] [Y + I]!=在startValue)
            返回false;    / *如果我们来到这里,那么它们都匹配! * /
    返回true;
}布尔IsDiagonalUpMatch(INT X,int y)对{
    / *获取的起始位置的值。 * /
    const int的在startValue =板[X] [Y]    / *确认两个值是比赛结束后。 * /
    对(INT I = 1;我3; ++ⅰ)
        如果(板[X + I] [Y - I]!=在startValue)
            返回false;    / *如果我们来到这里,那么它们都匹配! * /
    返回true;
}

这工作,但它只是不是很优雅;所有这三个功能看起来非常相似!幸运的是,你可以重写所有的人都在一个统一的功能而言。这个想法是这样的 - 如果你会发现,所有的三个功能用有一些步长中定义表明您移动什么方向努力。在水平的情况下,步骤是(+ 1,+ 0),在垂直情况下,它是(+ 0,+ 1),和在对角线它的(+ 1,+ 1)或(+ 1,-1)。鉴于此,您可以编写一个函数来检查三个值中的一个行匹配:

 布尔IsLinearMatch(INT X,INT Y,INT stepX,诠释stepY){
    / *获取的起始位置的值。 * /
    const int的在startValue =板[X] [Y]    / *确认两个值是比赛结束后。 * /
    对(INT I = 1;我3; ++ⅰ)
        如果(板[X + I * stepX] [Y + I * stepY]!=在startValue)
            返回false;    / *如果我们来到这里,那么它们都匹配! * /
    返回true;
}

您可以接着写

 布尔IsLineStartingAt(INT X,int y)对{
    返回IsLinearMatch(X,Y,1,0)|| //卧式
           IsLinearMatch(X,Y,0,1)|| //立式
           IsLinearMatch(X,Y,1,1)|| //斜下
           IsLinearMatch(X,Y,1,-1)|| //斜上
}

鉴于这种原始的,则只需遍历所有可能的出发点检查所有可能的匹配。

希望这有助于!

编辑:感谢提意见帮助解决我的愚蠢错误。 : - )

I have an 5x10 array that is populated with random values 1-5. I want to be able to check when 3 numbers, either horizontally, or vertically, match. I can't figure out a way to do this without writing a ton of if statements.

Here is the code for the randomly populated array


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

解决方案

Suppose that you have some particular starting point (x, y) and you're curious if there's three equal numbers in a row that start at this point. Let's consider just the case where you're looking in the horizontal direction. Then one way to do this (ignoring bounds-checking) would be like this:

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

You could similarly write a function like this for checking vertically:

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

And finally, one for the diagonals:

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

This works, but it's just not very elegant; all three of these functions look very similar! Fortunately, you can rewrite all of them in terms of a single unifying function. The idea is this - if you'll notice, all three functions work by having some "step size" defined indicating what direction you move. In the horizontal case, the step is (+1, +0), in the vertical case it's (+0, +1), and in the diagonal it's (+1, +1) or (+1, -1). Given this, you can write one function to check if three values match in a line:

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

You can then write

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
}

Given this primitive, you can check for all possible matches by just iterating over all possible starting points.

Hope this helps!

EDIT: Thanks to commenters for helping to fix my silly bugs. :-)

这篇关于胜利条件为连接-4般的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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