shaadi.com

Searching...
Friday, 31 May 2013

TICTAC GAME PROGRAM

#define TRUE 1
void main()
{
char tic[9]={'\0'},symbol_1,symbol_2,cur_symbol;
int turn=1,position,i;
void display();
while(TRUE)
{
display();
printf("Enter symbol for First Player <X/O> : ");
scanf("%c",&symbol_1);
if(symbol_1=='O' || symbol_1=='o' || symbol_1=='X'
  || symbol_1=='x') break;
}
if(symbol_1=='O' || symbol_1=='o') symbol_2='X'; else symbol_2='O';
while(turn<=9)
{
display();
if(turn>1)
for(i=0;i<9;i=i+3)
   printf("\n %c | %c | %c",tic[i],tic[i+1],tic[i+2]);
if(turn%2!=0)
   {printf("\n\nPlayer 1 : "); cur_symbol=symbol_1;}
else
   {printf("\n\nPlayer 2 : "); cur_symbol=symbol_2;}
scanf("%d",&position);
if( (position<1 || position>9) || tic[position-1]!='\0')
{
   printf("INVALID position... Enter again");
   getch(); continue;
}
tic[position-1]=cur_symbol;
if((tic[0]==tic[1] && tic[0]==tic[2] && tic[0]!='\0') ||
  (tic[3]==tic[4] && tic[3]==tic[5] && tic[3]!='\0') ||
  (tic[6]==tic[7] && tic[6]==tic[8] && tic[6]!='\0') ||
  (tic[0]==tic[4] && tic[0]==tic[8] && tic[0]!='\0') ||
  (tic[2]==tic[4] && tic[2]==tic[6] && tic[2]!='\0') ||
  (tic[0]==tic[3] && tic[0]==tic[6] && tic[0]!='\0') ||
  (tic[1]==tic[4] && tic[1]==tic[7] && tic[1]!='\0') ||
  (tic[2]==tic[5] && tic[2]==tic[8] && tic[2]!='\0'))
 {
   for(i=0;i<9;i=i+3)
printf("\n %c | %c | %c",tic[i],tic[i+1],tic[i+2]);
   if(turn%2!=0)
   {
 printf("\n\n Congrats... Player 1 has won the game");
 getch(); exit(1);
   }
   else
   {
 printf("\n\n Congrats... Player 2 has won the game");
 getch(); exit(1);
   }
 }
 turn++;
 getch();
}
printf("Better luck next time... ");
getch();
return;
}


void display()
{
clrscr();
printf(" 1 | 2 | 3\n");
printf(" 4 | 5 | 6\n");
printf(" 7 | 8 | 9\n");
printf("\n\n");
}



0 comments:

Post a Comment