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)
{
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