Win conditions for a game like connect-4

I have a 5x10 array that is filled with random values ​​1-5. I want to check when 3 numbers match, both horizontally and vertically. I can't figure out how to do this without writing a ton of if statements.

Here is the code for a 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 &lt&lt "============\n";

    while(row &lt rowincrement)
    {

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

+3
source share
3 answers

Suppose you have a specific starting point (x, y), and you are curious if there are three equal numbers in a line at that point. Consider only the case when you look in the horizontal direction. Then one way to do this (ignoring border checking) would be as follows:

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

Similarly, you can write a function to check 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;
}

, ; ! , . : , , " ", , . (+1, +0), it (+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;
}

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
}

, , .

, !

EDIT: .: -)

+9

:

,

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

, :

for(int i = 0 ; i < 8 ; i++)
{
    for(int j = 0 ; j < 3 ; j++)
    {

        if( ((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2])))
            std::cout << "Found a horizontal match on " << i << " " << j << std::endl;

        if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j]))
            std::cout << "Found a vertical match on " << i << " " << j << std::endl;
    }
}
0

, ; - , .

for( int row = 0; row<8 ; ++row )
{
    bool outerLoopBreakFlag = false ;
    for( int col=0 ; col<3; ++col )
    {
         // check for the winning conditions
         // i.e., board[row][col] == board[row][col+1] == board[row][col+2]
         //       board[row][col] == board[row+1][col] == board[row+2][col]
         //       if any one is satisfied, set the outerLoopBreakFlag to true
         else
             break ;
    }
    if( outerLoopBreakFlag == true )
        break ;
}              
0
source

Source: https://habr.com/ru/post/1784306/


All Articles