I'm learning C, and my teacher asked my class to make a Tic Tac Toe game. I managed to make most of it, but I'm kind of stuck. I've made a function with the purpose of checking the lines, one by one, and if it finds out all the line numbers are equal to 1 or 2, the game loop receives the victory number and stops. But there seems to be something wrong with the condition I've put on the if
inside the function, and I can't figure out what it is. It does not return a number to the game loop even when a line is complete with a number. I've tried searching for if
conditions but I couldn't find my error.
Here's the function:
int checkLines (int mat [3][3])
{
int i;
int victory;
for (i=0; i<3; i++)
{
if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
{
victory = 1;
} else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
{
victory = 2;
}else
{
victory = 0;
}
}
return victory;
}
if( mat[i][0] == mat[i][1] == mat[i][2] ) { victory = mat[i][0] }
. Then theelse if
block is redundant (and could be used instead for the column and diagonal tests perhaps).