Advertisement
dsdeep

Tic Tac Toe

Sep 3rd, 2020 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int i,x,y,z,count=0,n1,n2,p1=0,p2=0;
  6.     char st[9]={'1','2','3','4','5','6','7','8','9'};
  7. again:
  8.     for(i=0,x=0,y=1,z=2;i<3;i++,x+=3,y+=3,z+=3)
  9.     {
  10.         printf("\t %c | %c | %c\n",st[x],st[y],st[z]);
  11.         if(i==0||i==1)
  12.         {
  13.             for(int j=0;j<1;j++)
  14.             {
  15.                 printf("\t-----------\n");
  16.             }
  17.         }
  18.     }
  19.  
  20.     if((st[0]==st[1]&&st[1]==st[2])||(st[0]==st[3]&&st[3]==st[6])||(st[6]==st[7]&&st[7]==st[8])||(st[2]==st[5]&&st[5]==st[8])||(st[0]==st[4]&&st[4]==st[8])||(st[2]==st[4]&&st[4]==st[6]))
  21. {
  22.     if(p1>p2)
  23.         printf("\n Player 1 Wins\n");
  24.     else
  25.         printf("\n Player 2 Wins\n");
  26. }
  27. else if(count==9)
  28. {
  29.    printf("\nMath Draw\n");
  30. }
  31. else
  32.     {
  33.         if(p1==p2)
  34.         {
  35.             again1:
  36.         printf("\nPlayer 1 ( X ) = ");
  37.     scanf("%d",&n1);
  38.     printf("\n");
  39.       if(st[n1-1]=='X'||st[n1-1]=='O')
  40.           {
  41.               printf("Already Occupied!!\n\tTry Again..\n");
  42.               goto again1;
  43.           }
  44.     st[n1-1]='X';
  45.     p1++;
  46.     count++;
  47.         }
  48.         else
  49.         {
  50.             again2:
  51.           printf("\nPlayer 2 ( O ) = ");
  52.     scanf("%d",&n2);
  53.     printf("\n");
  54.       if(st[n2-1]=='X'||st[n2-1]=='O')
  55.           {
  56.               printf("Already Occupied!!\n\tTry Again..\n");
  57.               goto again2;
  58.           }
  59.     st[n2-1]='O';
  60.     p2++;
  61.     count++;
  62.         }
  63.         goto again;
  64.  
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement