Advertisement
Guest User

tinctac.c

a guest
Jul 28th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. char playground[3][3] =
  4. {
  5.     {' ', ' ', ' '},
  6.     {' ', ' ', ' '},
  7.     {' ', ' ', ' '}
  8. };
  9.  
  10. void game();
  11. int check_combo();
  12. void make_turn(int x, int y, char player);
  13. void print_playground();
  14.  
  15. int main(){
  16.     printf("Wanna play ?(y/n)\n");
  17.     char choice = 'y';
  18.     choice = getchar();
  19.     if((choice == 'y') || (choice == 'Y')) game();
  20.     else return 0;
  21.  
  22. }
  23.  
  24. void game(){
  25.     char player = 'X';
  26.     int turnscount = 0;
  27.     int x, y;
  28.     system("cls");
  29.     while(1){
  30.         system("cls");
  31.         turnscount++;
  32.         print_playground();
  33.         top : ;
  34.         scanf("%d %d", &x, &y);
  35.        
  36.         if (!(playground[x][y] == ' ')){ printf("Try another position !\n"); goto top;}
  37.         make_turn(x, y, player);
  38.         if(player == 'X') player = 'O';
  39.         else if(player == 'O') player = 'X';
  40.         if((check_combo())){
  41.             printf("The winner is %c!\n",(player == 'X')  ? 'O' : 'X' );
  42.             break;
  43.         }
  44.         if(turnscount > 9){
  45.             printf("Noones ! wp\n");
  46.             break;
  47.         }
  48.        
  49.        
  50.  
  51.     }
  52. }
  53. int check_combo(){
  54.     if(((playground[0][0]=='X') && (playground[0][1]=='X') && (playground[0][2]=='X')) ||
  55.         ((playground[0][0]=='O') && (playground[0][1]=='O') && (playground[0][2]=='O'))){
  56.         return 1;
  57.     }
  58.     else if(((playground[1][0]=='X') && (playground[1][1]=='X') && (playground[1][2]=='X')) ||
  59.         ((playground[1][0]=='O') && (playground[1][1]=='O') && (playground[1][2]=='O'))){
  60.         return 1;
  61.     }
  62.     else if(((playground[2][0]=='X') && (playground[2][1]=='X') && (playground[2][2]=='X')) ||
  63.         ((playground[2][0]=='O') && (playground[2][1]=='O') && (playground[2][2]=='O'))){
  64.         return 1;
  65.     }
  66.  
  67.  
  68.     else if(((playground[0][0]=='X') && (playground[1][0] == 'X') && (playground[2][0] == 'X'))||
  69.         ((playground[0][0]=='O') && (playground[1][0] == 'O') && (playground[2][0] == 'O'))){
  70.  
  71.         return 1;
  72.     }
  73.     else if(((playground[0][1]=='X') && (playground[1][1] == 'X') && (playground[2][1] == 'X'))||
  74.         ((playground[0][1]=='O') && (playground[1][1] == 'O') && (playground[2][1] == 'O'))){
  75.        
  76.         return 1;
  77.     }else if(((playground[0][2]=='X') && (playground[1][2] == 'X') && (playground[2][2] == 'X'))||
  78.         ((playground[0][2]=='O') && (playground[1][2] == 'O') && (playground[2][2] == 'O'))){
  79.  
  80.         return 1;
  81.     }
  82.     else if(((playground[0][0] == 'X') && (playground[1][1] == 'X') && (playground[2][2] == 'X')) ||
  83.      ((playground[0][0] == 'O') && (playground[1][1] == 'O') && (playground[2][2] == 'O'))){
  84.  
  85.         return 1;
  86.     }else if(((playground[0][2] == 'X') && (playground[1][1] == 'X') && (playground[2][0] == 'X')) || ((playground[0][2] == 'O') && (playground[1][1] == 'O') && (playground[2][0] == 'O'))){
  87.  
  88.         return 1;
  89.     }else {
  90.         return 0;
  91.     }  
  92. }
  93. void make_turn(int x, int y, char player){
  94.     playground[x][y] = player;
  95.     return ;
  96. }
  97. void print_playground(){
  98.     for (int i = 0; i < 3; ++i)
  99.     {
  100.         for (int j = 0; j < 3; ++j)
  101.         {
  102.             printf("%c ",playground[i][j] );
  103.         }printf("\n");
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement