Advertisement
akevintg

XOX in C

Nov 2nd, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.     char xox[3][3];
  5.     int pemain,x,y,flag;
  6.     do{
  7.         for(x=0;x<3;x++)
  8.             for(y=0;y<3;y++)
  9.                 xox[x][y]='_';
  10.         flag=-1;
  11.         pemain=1;
  12.         do{
  13.             printf("\n\n\n\n\n\n\n\n\n\n\n\n\n XOX GAME\n============\n1 2 3 (Y)\n");
  14.             for(x=0;x<3;x++)
  15.                 printf(" %d %c %c %c\n",x+1,xox[x][0],xox[x][1],xox[x][2]);
  16.             printf("(X)\n");
  17.             printf("Giliran Player %d\n",pemain);
  18.             do{
  19.                 printf("Masukan Koordinat X & Y [contoh: 1 3]: ");
  20.                 scanf("%d %d",&x,&y);fflush(stdin);
  21.                 x--;
  22.                 y--;
  23.                 if(xox[x][y]!='_'){
  24.                     printf("Sudah terisi, coba koordinat yang lain!\n");
  25.                     x=-1;
  26.                 }
  27.             }while(x<0||x>2||y<0||y>2);
  28.             if(pemain==1){
  29.                 xox[x][y]='X';
  30.                 pemain=2;
  31.             }
  32.             else if(pemain==2){
  33.                 xox[x][y]='O';
  34.                 pemain=1;
  35.             }
  36.             for(x=0,y=0;x<3;x++){
  37.                 if(xox[x][y]==xox[x][y+1]&&xox[x][y]==xox[x][y+2]&&xox[x][y]!='_')
  38.                     flag=1;
  39.             }
  40.             for(x=0,y=0;y<3;y++){
  41.                 if(xox[x][y]==xox[x+1][y]&&xox[x][y]==xox[x+2][y]&&xox[x][y]!='_')
  42.                     flag=1;
  43.             }
  44.             if(xox[0][0]==xox[1][1]&&xox[1][1]==xox[2][2]&&xox[2][2]!='_')
  45.                 flag=1;
  46.             if(xox[2][0]==xox[1][1]&&xox[1][1]==xox[0][2]&&xox[1][1]!='_')
  47.                 flag=1;
  48.             if(flag!=1){
  49.                 flag=2;
  50.                 for(x=0;x<3;x++)
  51.                     for(y=0;y<3;y++)
  52.                         if(xox[x][y]=='_')
  53.                             flag=-1;
  54.             }
  55.         }while(flag!=1&&flag!=2);
  56.         printf("\n");
  57.         for(x=0;x<3;x++)
  58.             printf(" %d %c %c %c\n",x+1,xox[x][0],xox[x][1],xox[x][2]);
  59.         if(flag==2)
  60.             printf("\nPermainan Draw\n");
  61.         else if(flag==1){
  62.             if(pemain==2)
  63.                 printf("\nPEMAIN 1 MENANG SELAMAT!\n");
  64.             else if(pemain==1)
  65.                 printf("\nPEMAIN 2 MENANG SELAMAT!\n");
  66.         }
  67.         printf("Main lagi (YES=1,NO=0)? ");
  68.         scanf("%d",&flag);
  69.     }while(flag!=0);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement