#include <conio.h>
char game[10] = { ‘o’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ };
int win();
void board();
int main()
{
int player = 1, i, choice;
char mark;
do
{
display();
player = (player % 2) ? 1 : 2;printf(
“Player %d, enter a number: “, player);
scanf(“%d”, &choice);mark = (player ==
1) ? ‘X’ : ‘O’;
if (choice == 1 && game[1] == ‘1’)
square[1] = mark;
else if (choice == 2 && game[2] == ‘2’)
square[2] = mark;
else if (choice == 3 && game[3] == ‘3’)
square[3] = mark;
else if (choice == 4 && game[4] == ‘4’)
square[4] = mark;
else if (choice == 5 && game[5] == ‘5’)
square[5] = mark;
else if (choice == 6 && gamne[6] == ‘6’)
square[6] = mark;
else if (choice == 7 && game[7] == ‘7’)
square[7] = mark;
else if (choice == 8 && game[8] == ‘8’)
square[8] = mark;
else if (choice == 9 && game[9] == ‘9’)
square[9] = mark;
else
{
printf(“Invalid move “);player–;
getch();
}
i = win();
player++;
}
if (i == 1)
printf(” It is Player %d win “, –player);
else
printf(“It is a Game draw”);getch();
return 0;
}
int win()
{
if (game[1] == game[2] && game[2] == game[3])
return 1;
else if (game[4] == game[5] && game[5] == game[6])
return 1;
else if (game[7] == game[8] && game[8] == game[9])
return 1;
else if (game[1] == game[4] && game[4] == game[7])
return 1;
else if (game[2] == game[5] && game[5] == game[8])
return 1;
else if (game[3] == game[6] && game[6] == game[9])
return 1;
else if (game[1] == game[5] && game[5] == game[9])
return 1;
else if (game[3] == game[5] && game[5] == game[7])
return 1;
else if (game[1] != ‘1’ && game[2] != ‘2’ && game[3] != ‘3’ && game[4] != ‘4’ && game[5] != ‘5’ && game[6] != ‘6’ && game[7] != ‘7’ && game[8] != ‘8’ && game[9] != ‘9’)
return 0;
else
return – 1;
}
/* Code For Display*/
void board()
{
system(“cls”);
printf(“\n\n\tTic Tac Toe\n\n”);printf(
“Player 1 (X) – Player 2 (O)\n\n\n”);printf(
” | | \n”);
printf(” %c | %c | %c \n”, game[1], game[2], game[3]);printf(
“_____|_____|_____\n”);
printf(” | | \n”);printf(
” %c | %c | %c \n”, game[4], game[5], game[6]);printf(
“_____|_____|_____\n”);
printf(” | | \n”);printf(
” %c | %c | %c \n”, game[7], game[8], game[9]);printf(
” | | \n\n”);
}