# Tic Tac Toe

```#include <stdio.h>

int main (void)
{
int player = 0;
int winner = 0;
int choice = 0;
int row = 0;
int column = 0;
int line = 0;

char board  = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};

//Look at the for loop.  It checks that there are <9 moves and no winner
for (int i = 0; i<9 && winner==0; i++)
{
//Print the board
printf("\n\n");
printf(" %c | %c | %c\n", board, board, board);
printf("---|---|---\n");
printf(" %c | %c | %c\n", board, board, board);
printf("---|---|---\n");
printf(" %c | %c | %c\n", board, board, board);

//Player = i div 2 (The remainder of i divided by 2) + 1)
player = i%2 + 1;

do
{
//tell the player to input a move
printf("\nPlayer %d, please enter the number of the square "
"where you want to place your %c: ",
player,(player==1)?'X':'O');
//      ^^^^^^^^^^ if player is player 1 then x else o
scanf("%d", &choice);

row = --choice/3;
//row == (choice - 1) / 3
column = choice%3;
//Column == The remainder of choice divided by 3
}while(choice<0 || choice>9 || board [row][column]>'9');
//^^^^^^Keep asking the question while it is an invalid number
board[row][column] = (player == 1) ? 'X' : 'O';
//^^^if valid choice set the position to X if player 1 or O if player 2

//Next conditional checks for a winner
if((board==board && board==board) ||
(board==board && board==board))
winner = player;
else
for(line = 0; line <=2; line++)
if((board[line]==board[line] && board[line]==board[line])||
(board[line]==board[line] && board[line]==board[line]))
winner = player;

}

//Print the final board
printf("\n\n");
printf(" %c | %c | %c\n", board, board, board);
printf("---|---|---\n");
printf(" %c | %c | %c\n", board, board, board);
printf("---|---|---\n");
printf(" %c | %c | %c\n", board, board, board);

//Was there a winner or a draw

if(winner==0)
printf("The game is a draw\n");
else
printf("Player %d has won\n", winner);

return 0;
}
```