Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. char** game;
  6. void print_game(void);
  7.  
  8. int check_winner(void) {
  9.  
  10.     // check vertical
  11.     for (int row = 6; row > 0; row--) {
  12.         for (int col = 6; col > 0; col--) {
  13.             if (game[row][col] == '1') {
  14.                 if (game[row-1][col] == '1') {
  15.                     if (game[row-2][col] == '1') {
  16.                         if (game[row-3][col] == '1') {
  17.                             return 1;
  18.                         }
  19.                     }
  20.                 }
  21.             }
  22.             if (game[row][col] == '2') {
  23.                 if (game[row-1][col] == '2') {
  24.                     if (game[row-2][col] == '2') {
  25.                         if (game[row-3][col] == '2') {
  26.                             return 2;
  27.                         }
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     // check horizontal
  34.     for (int row = 6; row > 0; row--) {
  35.         for (int col = 6; col > 0; col--) {
  36.             if (game[row][col] == '1') {
  37.                 if (game[row][col-1] == '1') {
  38.                     if (game[row][col-2] == '1') {
  39.                         if (game[row][col-3] == '1') {
  40.                             return 1;
  41.                         }
  42.                     }
  43.                 }
  44.             }
  45.             if (game[row][col] == '2') {
  46.                 if (game[row][col-1] == '2') {
  47.                     if (game[row][col-2] == '2') {
  48.                         if (game[row][col-3] == '2') {
  49.                             return 2;
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     // check diagonal
  57.     for (int row = 6; row > 0; row--) {
  58.         for (int col = 6; col > 0; col--) {
  59.             if (game[row][col] == '1') {
  60.                 if (game[row-1][col-1] == '1') {
  61.                     if (game[row-2][col-2] == '1') {
  62.                         if (game[row-3][col-3] == '1') {
  63.                             return 1;
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.             if (game[row][col] == '2') {
  69.                 if (game[row-1][col-1] == '2') {
  70.                     if (game[row-2][col-2] == '2') {
  71.                         if (game[row-3][col-3] == '2') {
  72.                             return 2;
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     return 0;
  80. }
  81.  
  82. int make_move(int move, int turn) {
  83.  
  84.     for (int row = 0; row < 7; row++) {
  85.  
  86.         /* Theres already another counter in this column */
  87.         if (game[row][move] == '1' || game[row][move] == '2') {
  88.             /* Check its not the top row */
  89.             if (row == 0) {
  90.                 printf("Column full, try again\n");
  91.                 return 0;
  92.             /* Otherwise add it to the column */
  93.             } else {
  94.                 if (!turn) {
  95.                     game[row-1][move] = '2';
  96.                 } else {
  97.                     game[row-1][move] = '1';
  98.                 }
  99.                 break;
  100.             }
  101.         /* Empty cell, but not the bottom row */
  102.         } else if ((game[row][move] == ' ') && (row != 6)) {
  103.             continue;
  104.         /* Empty cell and also the bottom row */
  105.         } else {
  106.             if (!turn) {
  107.                 game[row][move] = '2';
  108.             } else {
  109.                 game[row][move] = '1';
  110.             }
  111.             break;
  112.         }
  113.     }
  114.     if (check_winner() == 1) {
  115.         print_game();
  116.         printf("Player 1 wins!\n");
  117.         exit(0);
  118.     } else if (check_winner() == 2){
  119.         print_game();
  120.         printf("Player 2 wins!\n");
  121.         exit(0);
  122.     } else {
  123.         print_game();
  124.     }
  125.     return 1;
  126. }
  127.  
  128. void play_game(void) {
  129.  
  130.     char buffer[256];
  131.     int move;
  132.     int turn = 0;
  133.  
  134.     while (1) {
  135.         if (!turn) {
  136.             printf("Player 1> ");
  137.             turn = 1;
  138.         } else {
  139.             printf("Player 2> ");
  140.             turn = 0;
  141.         }
  142.         fflush(stdout);
  143.         if (fgets(buffer, 254, stdin) != NULL) {
  144.             if (buffer[0] == '\n') {
  145.                 turn = !turn;
  146.                 continue;
  147.             }
  148.             if (isdigit(buffer[0]) && buffer[1] == '\n' && buffer[2] == '\0') {
  149.                 if (buffer[0] <= '6' && buffer[0] >= '0') {
  150.                     move = (int)strtol(buffer, NULL, 10);
  151.                     if (!make_move(move, turn)) {
  152.                         turn = !turn;
  153.                     }
  154.                 } else {
  155.                     printf("Invalid input\n");
  156.                     turn = !turn;
  157.                 }
  158.             } else {
  159.                 printf("Invalid input\n");
  160.                 turn = !turn;
  161.             }
  162.  
  163.  
  164.         } else {
  165.             if (feof(stdin)) {
  166.                 fprintf(stdout, "Player %d quit\n", turn);
  167.                 exit(1);
  168.             }
  169.         }
  170.     }
  171.  
  172. }
  173.  
  174. void print_game(void) {
  175.  
  176.     printf("|0|1|2|3|4|5|6|\n");
  177.     for (int i = 0; i < 7; i++) {
  178.         printf("|%c|%c|%c|%c|%c|%c|%c|\n", game[i][0],
  179.                                             game[i][1],
  180.                                             game[i][2],
  181.                                             game[i][3],
  182.                                             game[i][4],
  183.                                             game[i][5],
  184.                                             game[i][6]);
  185.     }
  186.     printf("|-------------|\n");
  187. }
  188.  
  189. void setup_game(void) {
  190.  
  191.     for (int i = 0; i < 7; i++) {
  192.         for (int j = 0; j < 7; j++) {
  193.             game[i][j] = ' ';
  194.         }
  195.     }
  196. }
  197.  
  198. int main(int argc, const char * argv[]) {
  199.  
  200.  
  201.     game = malloc(sizeof(char*) * 7);
  202.     for (int i = 0; i < 7; i++) {
  203.         game[i] = malloc(sizeof(char) * 7);
  204.     }
  205.  
  206.     setup_game();
  207.     print_game();
  208.     play_game();
  209.  
  210.     for (int i = 0; i < 7; i++) {
  211.         free(game[i]);
  212.     }
  213.     free(game);
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement