Checking who won Tic Tac Toe More efficient C ++

I am writing a Tic Tac Toe game, and I would like to know how I can make an effective function to check who won. The two-dimensional matrix X, O or empty space is a panel.

  char CheckWin(const char board[][NUM_COLS], int& sum) // tic tac toe board - IN
{
    char tmp;
    int lcv;
    tmp = ' ';

    if (sum == 9)
    {
        return 'T';
    }
    else if (sum != 9)
    {
        if (((tmp = board[1][1]) != ' ' && board[0][0] == tmp && board[2][2] == tmp) || (board[2][0] == tmp && board[0][2] == tmp))
        {
            return tmp;
        }

        for (lcv = 0; lcv < 3; lcv++)
        {
            if ((tmp = board[lcv][0]) != ' ' && board[lcv][1] == tmp && board[lcv][2] == tmp)
            {
                return tmp;
            }
            else if ((tmp = board[lcv][0]) != ' ' && board[lcv][1] == tmp && board[lcv][2] == tmp)
            {
                return tmp;
            }
        }
    }

    return 'N';
}

Also, something like this again and again, how can I check who won and returned X if X won, O if O has one, T if it is a tie, and N if no one has one all same. Thanks in advance. I'm generally trying to get to know C ++ and programming.

EDIT: I just went with a simple method, but I somehow messed it up, does anyone know how? It seems like it doesn’t return anything, because when I call it mainly after the player selects the row and column (this works fine), it doesn’t output anything

+3
3

, O X, :

x_mask = 0
y_mask = 0
empty_count = 0
mask = 1
for each square
  if x then x_mask |= mask
  if y then y_mask |= mask
  if empty then empty_count++
  mask <<= 1

x_mask y_mask :

for each player
  for each winning combination
    if player_mask & winning_mask == winning_mask then player has won

, :

if neither player won
  if empty_count == 0
    its a tie
  else
    moves still available
+13

""

:

A  B  C
D  E  F
G  H  I

, - , :

A B C
D
G

0, 1 -1 X Y. , :

A: (++x) (++x, ++y) (++y)
B: (++y)
C: (++y) (--x, ++y)
D: (++x)
E: (++x)

++ / x/y - +/-/0 x/y, , .

, x y (), , : , , .


:

int x;
for (row = 0; row < 3; ++row)
    if ((x = board[row][0]) != Empty &&
        board[row][1] == x && board[row][2] == x)
        return x;
// similar loop for columns...
...
// hardcode diagonals...
if ((x = board[1][1]) != Empty &&
    (board[0][0] == x && board[2][2] == x ||
     board[2][0] == x && board[0][2] == x))
    return x
+2

, ( , -), , , -. , , CheckWin . - , - - , , .

+2
source

All Articles