Advertisement
Guest User

Naughts & Crosses (fixed)

a guest
Apr 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  5.  
  6. #define NAUGHT  -1
  7. #define CROSS   1
  8.  
  9. int x, y, maxdist, board[3][3];
  10.  
  11. /* Prototypes */
  12.  
  13. void getmove (int);
  14.  
  15. void printboard (void);
  16.  
  17. void updatemax (void);
  18.  
  19. int inbounds (int, int);
  20.  
  21. int distancegoing (int, int, int, int, int);
  22.  
  23. int horizontaldistance (int, int);
  24.  
  25. int verticaldistance (int, int);
  26.  
  27. int ascdiagdistance (int, int);
  28.  
  29. int dscdiagdistance (int, int);
  30.  
  31. /* Functions */
  32.  
  33. int main (void) {
  34.     int lastplayer = NAUGHT;
  35.     int moves = 0;
  36.     do {
  37.         fprintf(stdout, "Turn for: %c\n", (lastplayer == NAUGHT ? 'O' : 'X'));
  38.         getmove(lastplayer);
  39.         printboard();
  40.         updatemax();
  41.         lastplayer *= -1;
  42.     } while (maxdist < 2 && ++moves < 9);
  43.    
  44.     if (moves < 9) {
  45.         fprintf(stdout, "%c wins!\n", (-lastplayer == NAUGHT ? 'O' : 'X'));
  46.     } else {
  47.         fprintf(stdout, "Tie!\n");
  48.     }
  49.    
  50.     return 0;
  51. }
  52.  
  53. void getmove (int player) {
  54.     do {
  55.         fprintf(stdout, "(x,y) = ");
  56.         scanf("%d %d", &x, &y);
  57.     } while (!inbounds(x,y) || board[x][y]);
  58.     board[x][y] = player;
  59. }
  60.  
  61. void printboard (void) {
  62.     putc('\n', stdout);
  63.     for (int i = 0; i < 3; i++) {
  64.         for (int j = 0; j < 3; j++) {
  65.             fprintf(stdout, " %c ", (board[i][j] ? (board[i][j] > 0 ? 'X' : 'O') : '_'));
  66.         }
  67.         putc('\n', stdout);
  68.     }
  69.     putc('\n', stdout);
  70. }
  71.  
  72. void updatemax (void) {
  73.     int h = horizontaldistance(x, y);
  74.     int v = verticaldistance(x, y);
  75.     int ad = ascdiagdistance(x, y);
  76.     int dd = dscdiagdistance(x, y);
  77.     maxdist = MAX(h, MAX(v, MAX(ad, dd)));
  78. }
  79.  
  80. /* Supporting functions */
  81.  
  82. int inbounds (int x, int y) {
  83.     return (x >= 0 && x < 3 && y >= 0 && y < 3);
  84. }
  85.  
  86. int distancegoing (int player, int x, int y, int dx, int dy) {
  87.     int d = 0;
  88.     while (inbounds((x += dx), (y += dy)) && (player * board[x][y] > 0)) {
  89.         d++;
  90.     }
  91.     return d;
  92. }
  93.  
  94. int horizontaldistance (int x, int y) {
  95.     return distancegoing(board[x][y], x, y, -1, 0) + distancegoing(board[x][y], x, y, 1, 0);
  96. }
  97.  
  98. int verticaldistance (int x, int y) {
  99.     return distancegoing(board[x][y], x, y, 0, -1) + distancegoing(board[x][y], x, y, 0, 1);
  100. }
  101.  
  102. int ascdiagdistance (int x, int y) {
  103.     return distancegoing(board[x][y], x, y, 1, -1) + distancegoing(board[x][y], x, y, -1, 1);
  104. }
  105.  
  106. int dscdiagdistance (int x, int y) {
  107.     return distancegoing(board[x][y], x, y, -1, -1) + distancegoing(board[x][y], x, y, 1, 1);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement