Find a winner in a match with a tick

I know that people have asked this before, but I did not understand the answers.

Suppose you have char board[3][3]as a parameter in a function, and that you need to return 1 if you won X, -1 if O won or 0 if no one else won OR if it's a tie.

int checkforwin(char board[3][3]);

what an announcement for a function.

Any idea for non-primitive testing to win one of the opponents?

+4
source share
4 answers

This stores a count for each row / column / diagonal.

If the counter reaches 3, it indicates three Xin a row.
Similarly, -3 indicates three Oper line.
Any other value does not indicate a winner.

4 :

  • .

DRY .
.

int CheckTicTacToe(char board[3][3])
{
    int count = 0;
    int row, col;

    // Check each of 3 rows:
    for(row = 0; row < 3; ++row)
    {
        count = 0;
        for(col=0; col < 3; ++col)
        {
            count += (board[row][col] == 'X')?  1 :
                     (board[row][col] == 'O')? -1 : 0;
        }
        if (count == 3 || count == -3)
        {
            return count / abs(count); // Return either 1 or -1
        }
    }

    // Check each of 3 columns.
    for(col = 0; col < 3; ++col)
    {
        count = 0;
        for(row=0; row < 3; ++row)
        {
            count += (board[row][col] == 'X')?  1 :
                     (board[row][col] == 'O')? -1 : 0;
        }
        if (count == 3 || count == -3)
        {
            return count / abs(count); // Return either 1 or -1
        }
    }

    // Check Left-to-Right downward Diagonal:
    count = 0;
    for(col = 0; col < 3; ++col)
    {
        count += (board[col][col] == 'X')?  1 :
                 (board[col][col] == 'O')? -1 : 0;
    }
    if (count == 3 || count == -3)
    {
        return count / abs(count); // Return either 1 or -1
    }

    // Check Left-to-Right upward Diagonal
    count = 0;
    for(col = 0; col < 3; ++col)
    {
        count += (board[col][2-col] == 'X')?  1 :
                 (board[col][2-col] == 'O')? -1 : 0;
    }
    if (count == 3 || count == -3)
    {
        return count / abs(count); // Return either 1 or -1
    }

    return 0;
}
+2

, , - . if for, .

// board[3][3]:
//    [0][0] | [1][0] | [2][0]
//    -------+--------+-------
//    [0][1] | [1][1] | [2][1]
//    -------+--------+-------
//    [0][2] | [1][2] | [2][2]
//
int checkforwin(char board[3][3])
{
    int x;

    for(x = 0; x < 3; x++)
    {
      // check vertical lines
      if ((board[x][0] != '\0') && 
          (board[x][0] == board[x][1]) && 
          (board[x][0] == board[x][2]))
         return(board[x][0] == 'O' ? -1 : 1);

      // check horizontal lines
      if ((board[0][x] != '\0') &&
          (board[0][x] == board[1][x]) && 
          (board[0][x] == board[2][x]))
         return(board[0][x] == 'O' ? -1 : 1);
    };

    // check top left to bottom right diagonal line
    if ((board[0][0] != '\0') && 
       (board[0][0] == board[1][1]) && 
       (board[0][0] == board[2][2]))
      return(board[0][0] == 'O' ? -1 : 1);

    // check bottom left to top right diagonal line
    if ((board[2][0] != '\0') && 
       (board[2][0] == board[1][1]) &&
       (board[0][0] == board[0][2]))
      return(board[2][0] == 'O' ? -1 : 1);

    // no winner
    return 0;
}
+2

tic-tac-toe. , , .

if. 8 -, 8 if. , , , , , . - , tic-tac-toe . , Connect Four, , tic-tac-toe if.

. , , , , if if for. , - , .

, , . .

typedef struct
{
    int valid;
    int rowA, colA;
    int rowB, colB;
    int rowC, colC;
}
    stPath;

static stPath paths[] =
{
    { TRUE,   0, 0,   0, 1,   0, 2 },   // top row
    { TRUE,   1, 0,   1, 1,   1, 2 },   // middle row
    { TRUE,   2, 0,   2, 1,   2, 2 },   // bottom row

    { TRUE,   0, 0,   1, 0,   2, 0 },   // left column
    { TRUE,   0, 1,   1, 1,   2, 1 },   // middle column
    { TRUE,   0, 2,   1, 2,   2, 2 },   // right column

    { TRUE,   0, 0,   1, 1,   2, 2 },   // TL to BR diagonal
    { TRUE,   0, 2,   1, 1,   2, 0 },   // TR to BL diagonal

    { FALSE,  0, 0,   0, 0,   0, 0 }    // end of list
};

int checkforwin( char board[3][3] )
{
    int a, b, c;
    stPath *pptr;

    // assumes that board array uses 'X' 'O' and <sp> to mark each position

    for ( pptr = paths; pptr->valid; pptr++ )
    {
        a = board[pptr->rowA][pptr->colA];
        b = board[pptr->rowB][pptr->colB];
        c = board[pptr->rowC][pptr->colC];

        if ( a == b && b == c && a != ' ' )
            return( (a == 'X') ? 1 : -1 );
    }

    return( 0 );    // no winner yet
}

, , , , , , .

+2

If-statements - . , ('X' 'O') . - If.

, . , - "011011001", .

0

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


All Articles