# Thread: checking for win in tic tac toe

1. ## checking for win in tic tac toe

in my last tic tac toe game, checking for the win was very primative. my board array is set up like this:

[1,1] [1,2] [1,3]
[2,1] [2,2] [2,3]
[3,1] [3,2] [3,3]

this is how my old checkforwin looked:
Code:
```void checkforwin()
{
int w=1;
char d='X';
win=0;
for(int p=1;p<=2;p++)
{
if(p==2)
{
d='O';
w=2;
}
for(int g=1;g<4;g++)
{
if(board[1][g]==d && board[2][g]==d && board[3][g]==d)
{
win=w;
}
}
for(int t=1;t<4;t++)
{
if(board[t][1]==d && board[t][2]==d && board[t][3]==d)
{
win=w;
}
}
if(board[1][1]==d && board[2][2]==d && board[3][3]==d)
{
win=w;
}
if(board[3][1]==d && board[2][2]==d && board[1][3]==d)
{
win=w;
}
}
}```
im thinking of this: perhaps i could search the array for x (or 0), and then see if it is in a line or something. just anything better than what is above. also, sorry for so much posting.

2. Break it down into its component steps
Code:
```#define BOARD   3

// checks all rows
int checkboardrow ( int board[BOARD][BOARD], int player ) {
int win = 1;
int row, col;
for ( row = 0 ; row < BOARD && win ; row++ ) {
for ( col = 0 ; col < BOARD && win ; col++ ) {
if ( board[row][col] != player ) win = 0;
}
}
return win;
}

// checks all rows, cols and diagonals
int checkboard ( int board[BOARD][BOARD], int player ) {
return checkboardrow(board,player) ||   // checks all rows
checkboardcol(board,player) ||   // checks all cols
checkboardleft(board,player)||   // checks left diagonal
checkboardright(board,player);   // checks right diagonal
}```