Code:
#include <stdio.h>
#include <stdlib.h>
void main(){
char userinput;
printf("\nPlease enter C (play with computer) or U (play with other user)");
scanf(" %c",userinput);
for(userinput=1;userinput<100;userinput++){
switch(userinput){
case 'C': case 'c':
computer();
break;
case 'U': case 'u' :
user();
break;
default:
exit(0);
struct myDataType{
int i;
char ch;
}
inputValue();
void run();
int check(char sym[9], char ch[1],int count);
struct myDataType inputValue(char sym[9],int count);
void Display(char sym[9]);
void user(){
char reStart[1];
again:
run();
printf("\nIf you want to play again, press 1: \nelse any:");
scanf("%s",reStart);
if(*reStart =='1'){
goto again;
}
else
exit(0);
}
void run(){
int count;
struct myDataType info;
char symbol[9]={'1','2','3','4','5','6','7','8','9'};
Display(symbol);
again:
info = inputValue(symbol,count);
symbol[info.i] = info.ch;
Display(symbol);
if(check(symbol,info.ch, count)==1)
;
else{count++;
goto again;
}
}
int check(char sym[9],char ch[1],int count){
int i;
for(i=0;i<3;i++)//for row
if(sym[i]==ch&&sym[i+1]==ch&&sym[i+2]==ch){
printf("the winner is : %c",ch);
return 1;}
for(i=0;i<3;i++) // for row
if(sym[i]==ch&&sym[i+3]==ch&&sym[i+6]==ch){
printf("the winner is : %c",ch);
return 1;}
if(sym[0]==ch&&sym[4]==ch&&sym[8]==ch){
printf("the winner is : %c",ch);
return 1;}
else if(sym[2]==ch&&sym[4]==ch&&sym[6]==ch){
printf("the winner is : %c",ch);
return 1;}
else if(count ==8){
printf("The game is DRAW");
return 1;
} else return 0;
}
struct myDataType inputValue(char sym[9],int count){
char value[1];
int i;
struct myDataType info;
inputAgain:
if(count%2 == 0){
printf("\nEnter Your choice X :");
}else{
printf("\nEnter Your choice O: ");
}
scanf("%s", value);
for(i=0;i<9;i++){
if(*value==sym[i]){
info.i = i;
if(count%2==0)
info.ch = 'X';
else
info.ch = 'O';
break;
} else{
info.i=-1;
info.ch = "";
}
}
if(info.i ==-1){
printf("\nInput is not valid");
goto inputAgain;
} return info;
}
void Display(char sym[9]){
printf("T I C T A C T O E");
printf("\nPlayer 1 symbol: X");
printf("\nPlayer 2 symbol: O");
printf( "\n\t\t\t | | ");
printf("\n\t\t %c | %c | %c ",sym[0],sym[1],sym[2]);
printf("\n\t\t---------|---------|----------");
printf("\n\t\t %c | %c | %c ",sym[3],sym[4],sym[5]);
printf("\n\t\t---------|---------|----------");
printf("\n\t\t %c | %c | %c ",sym[6],sym[7],sym[8]);
printf( "\n\t\t\t | | ");
}
int done = 0;
int moveindex = 0;
char board[3][3];
void computer(){
int i, j, z, player_move = 1;
z = 1;
for ( i = 0; i < 3; i++ )
for ( j = 0; j < 3; j++ )
{
board[i][j] = (char)(z+48);
z++;
}
printf("Tic Tac Toe\n================\n");
do {
if (player_move)
get_player_movement();
else
computer_move();
player_move = !player_move;
if (moveindex == 9)
done = 2;
printf("move %d\n", moveindex);
moveindex++;
} while (done == 0);
if (done == 1)
printf("I win!\n");
if (done == 2)
printf("Draw!\n");
print_template();
return 1;
}
void print_template(void)
{
int i;
for (i = 0; i < 3; i++)
{
printf(" | | \n");
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
printf(" | | \n");
if ( i != 2 )
printf ("--- --- ---\n");
}
}
int get_input(int move)
{
int row, col;
if ( move >= 1 && move <= 3 )
row = 0;
if ( move >= 4 && move <= 6 )
row = 1;
if ( move >= 7 && move <= 9 )
row = 2;
col = move % 3 - 1;
if ( col < 0 )
col = 2;
if ( board[row][col] == 'x' )
return 1;
if ( board[row][col] == 'o' )
return 2;
else
return 0;
}
void get_player_movement(void)
{
int move, valid = 0;
int row, col;
print_template();
do {
printf("Choose a square:\nPlease enter (1-9) only \n");
scanf("%d", &move); // for row
if ( move >= 1 && move <= 3 )
row = 0; //1 row
if ( move >= 4 && move <= 6 )
row = 1; // 2nd row
if ( move >= 7 && move <= 9 )
row = 2; // 3rd row
col = move % 3 - 1; //for column
if ( col < 0 )
col = 2;
if (get_input(move) == 0)
{
board[row][col] = (char)'x';
valid = 1;
}
else
printf("That square is occupied!\n");
} while (!valid);
}
void computer_move (void)
{
int colcount[3] = { 0, 0, 0 };
int rowcount[3] = { 0, 0, 0 };
int i, j;
int can_lose;
int lose_pos[1][2];
if (moveindex <= 1)
if (get_input(5) == 0)
{
board[1][1] = (char)'o';
return;
}
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
if ( board[i][j] == 'o' )
{
colcount[j]++;
rowcount[i]++;
}
if ( board[i][j] == 'x' )
{
colcount[j]--;
rowcount[i]--;
}
}
}
can_lose = 0;
for ( i = 0; i < 3; i++ )
{
if (abs(colcount[i]) == 2)
{
for ( j = 0; j < 3; j++ )
if ( board[j][i] != 'x' && board[j][i] != 'o' )
{
if (colcount[i] > 0)
{
board[j][i] = 'o';
done = 1;
return;
} else
{
can_lose = 1;
lose_pos[0][0] = j;
lose_pos[0][1] = i;
}
}
}
if (abs(rowcount[i]) == 2)
{
for ( j = 0; j < 3; j++ )
if ( board[i][j] != 'x' && board[i][j] != 'o' )
{
if (rowcount[i] > 0)
{
board[i][j] = 'o';
done = 1;
return;
} else
{
can_lose = 1;
lose_pos[0][0] = i;
lose_pos[0][1] = j;
}
}
}
}
if (can_lose)
{
board[lose_pos[0][0]][lose_pos[0][1]] = 'o';
return;
}
if (get_input(5) == 2)
{
if ((get_input(1) == get_input(9) == 1) ||
(get_input(7) == get_input(3) == 1) )
{
if (get_input(6) == 0)
{
board[1][2] = 'o';
return;
}
if (get_input(2) == 0)
{
board[0][1] = 'o';
return;
}
if (get_input(4) == 0)
{
board[1][0] = 'o';
return;
}
if (get_input(8) == 0)
{
board[2][1] = 'o';
return;
}
}
}
if ( get_input(2) * get_input(4) > 0 && get_input(1) == 0 )
{
board[0][0] = 'o';
return;
}
if ( get_input(2) * get_input(6) > 0 && get_input(3) == 0 )
{
board[0][2] = 'o';
return;
}
if ( get_input(4) * get_input(8) > 0 && get_input(7) == 0)
{
board[2][0] = 'o';
return;
}
if ( get_input(6) * get_input(8) > 0 && get_input(9) == 0)
{
board[2][2] = 'o';
return;
}
if ( (get_input(1) == get_input(9) && get_input(1) != 0) ||
(get_input(5) == get_input(9) && get_input(9) != 0) ||
(get_input(5) == get_input(1) && get_input(5) != 0) )
{
if (get_input(1) == 0)
{
board[0][0] = 'o';
if (get_input(5) == 2)
done = 1;
return;
}
if (get_input(5) == 0)
{
if (get_input(1) == 2)
done = 1;
board[1][1] = 'o';
return;
}
if (get_input(9) == 0)
{
board[2][2] = 'o';
if (get_input(5) == 2)
done = 1;
return;
}
}
if ( (get_input(3) == get_input(7) && get_input(3) != 0) ||
(get_input(5) ==get_input(7) && get_input(5) != 0) ||
(get_input(5) == get_input(3) && get_input(3) != 0) )
{
if (get_input(3) == 0)
{
board[0][2] = 'o';
if (get_input(5) == 2)
done = 1;
return;
}
if (get_input(5) == 0)
{
board[1][1] = 'o';
if (get_input(3) == 2)
done = 1;
return;
}
if (get_input(7) == 0)
{
board[2][0] = 'o';
if (get_input(5) == 2)
done = 1;
return;
}
}
if ( get_input(5) == 1 )
{
if (moveindex == 1)
board[0][0] = (char)'o';
if (moveindex == 3)
{
if (get_input(9) == 1)
board[2][0] = (char)'o';
if (get_input(7) == 1)
board[0][2] = (char)'o';
if (get_input(3) == 1)
board[2][0] = 'o';
}
return;
}
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if (board[i][j] != 'x' && board[i][j] != 'o')
{
board[i][j] = 'o';
return;
}
}
}