Advertisement
Kocolino

Untitled

Mar 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int count = 0;
  5. char matrix[3][3];
  6.  
  7. /** Initialize the matrix. */
  8. void init_matrix()
  9. {
  10.     int i, j;
  11.     for(i=0; i<3; i++)
  12.         for(j=0; j<3; j++)
  13.             matrix[i][j] = ' ';
  14. }
  15.  
  16. /** Display the matrix on the screen. */
  17. void disp_matrix()
  18. {
  19.     printf("\n");
  20.     printf(" %c | %c | %c ",matrix[0][0], matrix[0][1], matrix [0][2]);
  21.     printf("\n---|---|---\n");
  22.     printf(" %c | %c | %c ",matrix[1][0], matrix[1][1], matrix [1][2]);
  23.     printf("\n---|---|---\n");
  24.     printf(" %c | %c | %c ",matrix[2][0], matrix[2][1], matrix [2][2]);
  25.  
  26.     printf("\n\n");
  27. }
  28.  
  29. /** Get a player's move */
  30. void get_player_move()
  31. {
  32.     int x, y;
  33.     printf("I dare you to enter some coordinates: ");
  34.     scanf("%d%*c%d", &x, &y);
  35.     x--;
  36.     y--;
  37.  
  38.     if(matrix[x][y] != ' ')
  39.     {
  40.         printf("You can't do that, hooman.Try again, if you dare.\n");
  41.         get_player_move();
  42.     }
  43.     else matrix[x][y] = 'X';
  44. }
  45.  
  46. /** Get computer move */
  47. void get_computer_move()
  48. {
  49.     int m, n;
  50.     m = rand() % 3;
  51.     n = rand() % 3;
  52.     while(matrix[m][n] == 'X' || matrix[m][n] == 'O')
  53.     {
  54.         m = rand() % 3;
  55.         n = rand() % 3;
  56.     }
  57.     count ++;
  58.  
  59.     if(count == 9)
  60.     {
  61.         printf("Draw...you got lucky.\n");
  62.         exit(0);
  63.     }
  64.     else
  65.         matrix[m][n]  = 'O';
  66. }
  67.  
  68. /** See if there is a winner */
  69. char check()
  70. {
  71.     int i;
  72.     for(i=0; i<3; i++) /** check rows */
  73.         if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2])
  74.             return matrix[i][0];
  75.  
  76.     for(i=0; i<3; i++) /** check columns */
  77.         if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
  78.             return matrix[0][i];
  79. /** test diagonals */
  80.  
  81.     if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
  82.         return matrix[0][0];
  83.  
  84.     if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
  85.         return matrix[0][2];
  86.  
  87.     return ' ';
  88. }
  89.  
  90. int main()
  91. {
  92.     char fini = ' ';
  93.     printf("It is I, the evil computer.\n\n");
  94.     printf("You will be playing Tic Tac Toe against me, myself and I.\n\n");
  95.     printf("Please proceed, hooman.\n");
  96.     init_matrix();
  97.  
  98.     do {
  99.         disp_matrix();
  100.         get_player_move();
  101.         fini = check(); /** see if winner */
  102.  
  103.         if(fini != ' ')
  104.             break; /** winner!*/
  105.  
  106.         get_computer_move();
  107.         fini = check(); /* see if winner */
  108.         } while(fini == ' ');
  109.  
  110.     disp_matrix(); /** show final positions */
  111.     if(fini == 'X')
  112.         printf("You w...you won.Cause I let you win.\n");
  113.     else
  114.         printf("I won.Pff.Obviously.\n");
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement