Advertisement
dimon2242

Untitled

Jul 31st, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define SPACE ' '
  7.  
  8. void disp_matrix(void);
  9. int check(void);
  10. void player(void);
  11. int pc(void);
  12.  
  13. int ininum = 9;
  14. char done;
  15. char matrix[3][3] = {
  16.     {SPACE, SPACE, SPACE},
  17.     {SPACE, SPACE, SPACE},
  18.     {SPACE, SPACE, SPACE}
  19. };
  20.  
  21. int main(void) {
  22.     do {
  23.     //Display matrix
  24.     disp_matrix();
  25.     //Player
  26.     player();
  27.     //Check
  28.     done = check();
  29.     if(done != SPACE)
  30.         break;
  31.     pc();
  32.     done = check();
  33.     }
  34.     while(done == SPACE);
  35.     if(done == 'X') {
  36.         printf("\nYou win!\n");
  37.         disp_matrix();
  38.         return 0;
  39.     }
  40.     else {
  41.         printf("\nYou lose!\n");
  42.         disp_matrix();
  43.         printf("\n\nArray: %s", &matrix);
  44.     return 0;
  45.     }
  46. }
  47.  
  48. //Player
  49. void player(void) {
  50.     int x, y;
  51.     printf("\nEnter you coords (X)\n");
  52.     scanf("%d", &x);
  53.     printf("Enter you coords (Y)\n");
  54.     scanf("%d", &y);
  55.     printf("\n");
  56.     x--;
  57.     y--;
  58.     if(x < 0 || y < 0 || x > 2 || y > 2 || matrix[x][y] != SPACE || matrix[x][y] == 'O') {
  59.         printf("Error move!");
  60.     player();
  61.     }
  62.     else {
  63.         matrix[x][y] = 'X';
  64.         --ininum;
  65.     }
  66. }
  67.  
  68. //Computer
  69. int pc(void) {
  70.     static int ix, iy;
  71.     iy = 0;
  72.     for(static int ix = 0; ix < 3; ix++) {
  73.         if(iy < 3 || ininum > 0){
  74.             if(matrix[ix][iy] == SPACE) {
  75.                 matrix[ix][iy] = 'O';
  76.                 --ininum;
  77.                 iy++;
  78.                 return 0;
  79.             }
  80.             else {
  81.                 iy = 0;
  82.                 continue;
  83.             }
  84.     }
  85. }
  86. }
  87.  
  88. //Matrix display
  89. void disp_matrix(void) {
  90.     for(register int x = 0; x < 3; x++) {
  91.         printf("\n%c | %c | %c\n", matrix[x][0], matrix[x][1], matrix[x][2]);
  92.         if(x < 2)
  93.             printf("\n- | - | -\n");
  94.     }
  95. }
  96.  
  97. //Check party
  98. int check(void) {
  99.     register int t;
  100.     for(t = 0; t < 3; t++) {
  101.         char *p;
  102.         p = &matrix[0][t];
  103.         if(*p == *(p + 1) && *(p + 1) == *(p + 2))
  104.             return *p;
  105.     }
  106.     for(t = 0; t < 3; t++) {
  107.         char *p;
  108.         p = &matrix [t][0];
  109.         if(*p == *(p+3) && *(p+3) == *(p+6))
  110.             return *p;
  111.     }
  112.     if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matrix[2][2])
  113.         return(matrix[0][0]);
  114.     if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matrix[2][0])
  115.         return(matrix[0][2]);
  116.     return SPACE;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement