# tick- tack- toe

This is a discussion on tick- tack- toe within the C Programming forums, part of the General Programming Boards category; do you know the game? "x or o" is another name for it. You need to form a continuity of ...

1. ## tick- tack- toe

do you know the game? "x or o" is another name for it. You need to form a continuity of the sign you choose- x or o. I need to write a function that will get a bi dimensional array as a parameter, & check if in this array x won or o.
this is what I wrote:

Code:
```int TickTackToe(char A[N][N],int n)
{
int i,j;
char x='x',o='o',flag;

for (i=j=0;i<n;i++,j++)
if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
break;

for (i=n-1,j=n-1-i;i>=0;i--,j++)
if (A[i][j]==A[i-1][j+1]==A[i-2][j+2])
break;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if (A[i][j]==A[i+1][j]==A[i+2][j])
break;

for(j=0;j<n;j++)
for(i=0;i<n;i++)
if (A[i][j]==A[i][j+1]==A[i][j+2])

flag=A[i][j];

if (flag==x)
return (1);
else
if (flag==o)
return (o);

else
return (-1);

}```
problems:
1. It allways give me 1 as return value.
2. can you think of way to make it look nicer?

TIA,

Ronen

2. return (o) or return(0);???

You can't do this:
A[i][j]==A[i+1][j+1]==A[i+2][j+2]
What's happenign is:
A[i][j]==A[i+1][j+1] return 1 if true or 0 if false then:
1:0==A[i+2][j+2]
which I think is always false
A[i][j]==A[i+1][j+1] && A[i+1][j+1]==A[i+2][j+2]

More
Code:
```for (i=j=0;i<n;i++,j++)
if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
break;```
... why do you do this?? Should you place return 1 instead break? Like it is now it simply runs a cicle and breaks. The i and j variables are then re-defined, and nothing is done regarding the previous cicle.

3. I do the "break" cause I need the flag veriable activated.

4. But if you have one cicle after another the values of i and j are redefined... if the break statment is reached, no more for()s should be parsed. Instead of break write return A[i][j]==x.
Basicly it'll be like this:
Code:
```int TickTackToe(char A[N][N],int n)
{
int i,j;

for (i=j=0;i<n;i++,j++)//check diagonal up,left->right,down
if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
return A[i][j]=='x';

for (i=n-1,j=0;i>=0;i--,j++)//check diagonal right,up->down,left
if (A[i][j]==A[i-1][j+1]==A[i-2][j+2])
return A[i][j]=='x';

for(i=0;i<n;i++)//check vertical lines
for(j=0;j<n;j++)
if (A[i][j]==A[i+1][j]==A[i+2][j])
return A[i][j]=='x';

for(j=0;j<n;j++)//check horizontal lines
for(i=0;i<n;i++)
if (A[i][j]==A[i][j+1]==A[i][j+2])
return A[i][j]=='x';
//return statment should have been reached... if not no line was detected
return -1;
}```
Hope this one helps.
if (flag==x)
return (1);
else if (flag==o)
return (0);

return A[i][j]=='x'; does the same stuff: if that char is 'x' return 1, else 0. If no line is detected the function at the end return -1.

5. This is also works...
Thank you, xErath.

Code:
```int TickTackToe(char A[N][N],int n)
{
int i,j;
char x='x',o='o',flag;

for (i=j=0;i<n;i++,j++)
if (A[i][j]==A[i+1][j+1]&&A[i+1][j+1]==A[i+2][j+2])
break;

for (i=n-1,j=n-1-i;i>=0;i--,j++)
if (A[i][j]==A[i-1][j+1]&&A[i-1][j+1]==A[i-2][j+2])
break;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if (A[i][j]==A[i+1][j]&&A[i+1][j]==A[i+2][j])		       break;

for(j=0;j<n;j++)
for(i=0;i<n;i++)
if (A[i][j]==A[i][j+1]&&A[i][j+1]==A[i][j+2])

flag=A[i][j];
if (flag==x)
return (1);
else
if (flag==o)
return (0);
else
return (-1);
}```

Ronen