home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / CONNECT4.ZIP / GAME.C < prev   
C/C++ Source or Header  |  1997-05-17  |  8KB  |  228 lines

  1. /****************************************************************************/
  2. /**                                                                        **/
  3. /**                          Connect-4 Algorithm                           **/
  4. /**                                                                        **/
  5. /**                              Version 3.4                               **/
  6. /**                                                                        **/
  7. /**                            By Keith Pomakis                            **/
  8. /**                          (pomakis@pobox.com)                           **/
  9. /**                                                                        **/
  10. /**                              April, 1997                               **/
  11. /**                                                                        **/
  12. /****************************************************************************/
  13. /**                                                                        **/
  14. /**                          Sample Implementation!                        **/
  15. /**                                                                        **/
  16. /**  This code is poorly written and contains no internal documentation.   **/
  17. /**  Its sole purpose is to quickly demonstrate an actual implementation   **/
  18. /**  of the functions contained in the file "c4.c".  It's a fully working  **/
  19. /**  game which should work on any type of system, so give it a shot!      **/
  20. /**                                                                        **/
  21. /**  The computer is pretty brain-dead at level 3 or less, but at level 4  **/
  22. /**  and up it provides quite a challenge!                                 **/
  23. /**                                                                        **/
  24. /****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "c4.h"
  29.  
  30. #define BUFFER_SIZE 80
  31.  
  32. enum {HUMAN = 0, COMPUTER = 1};
  33.  
  34. static int get_num(char *prompt, int lower, int upper);
  35. static void print_board(int width, int height);
  36. static void print_dot(void);
  37.  
  38. static char piece[2] = { 'X', 'O' };
  39.  
  40. int
  41. main()
  42. {
  43.     int player[2], level[2], turn = 0, num_of_players, move;
  44.     int width, height, num_to_connect;
  45.     int x1, y1, x2, y2;
  46.     char buffer[BUFFER_SIZE];
  47.  
  48.     printf("\n****  Welcome to the game of Connect!  ****\n\n");
  49.     printf("By Keith Pomakis (pomakis@pobox.com)\n");
  50.     printf("June 16, 1994\n\n");
  51.  
  52.     width = get_num("Width of board (7 is standard)? ", 1, 40);
  53.     height = get_num("Height of board (6 is standard)? ", 1, 40);
  54.     num_to_connect = get_num("Number to connect (4 is standard)? ", 1, 40);
  55.  
  56.     num_of_players = get_num("Number of human players (0, 1 or 2)? ", 0, 2);
  57.     switch (num_of_players) {
  58.  
  59.         case 0:
  60.             player[0] = player[1] = COMPUTER;
  61.             level[0] = get_num("Skill level of player X (1 - 10)? ", 1, 10);
  62.             level[1] = get_num("Skill level of player O (1 - 10)? ", 1, 10);
  63.             turn = 0;
  64.             break;
  65.  
  66.         case 1:
  67.             player[0] = HUMAN;
  68.             player[1] = COMPUTER;
  69.             level[1] = get_num("Skill level of computer (1 - 10)? ", 1, 10);
  70.             buffer[0] = '\0';
  71.             do {
  72.                 printf("Would you like to go first? ");
  73.                 if (!fgets(buffer, BUFFER_SIZE, stdin)) {
  74.                     printf("\nGoodbye!\n");
  75.                     exit(0);
  76.                 }
  77.                 buffer[0] = tolower(buffer[0]);
  78.             } while (buffer[0] != 'y' && buffer[0] != 'n');
  79.  
  80.             turn = (buffer[0] == 'y')? 0 : 1;
  81.             break;
  82.  
  83.         case 2:
  84.             player[0] = player[1] = HUMAN;
  85.             turn = 0;
  86.             break;
  87.     }
  88.  
  89.     c4_new_game(width, height, num_to_connect);
  90.     c4_poll(print_dot, CLOCKS_PER_SEC/2);
  91.  
  92.     do {
  93.         print_board(width, height);
  94.         if (player[turn] == HUMAN) {
  95.             do {
  96.                 if (num_of_players == 2)
  97.                     sprintf(buffer, "Player %c, drop in which column? ",
  98.                             piece[turn]);
  99.                 else
  100.                     sprintf(buffer, "Drop in which column? ");
  101.                 move = get_num(buffer, 1, width) - 1;
  102.             }
  103.             while (!c4_make_move(turn, move, NULL));
  104.         }
  105.         else {
  106.             if (num_of_players == 1)
  107.                 printf("Thinking.");
  108.             else
  109.                 printf("Player %c is thinking.", piece[turn]);
  110.             fflush(stdout);
  111.             c4_auto_move(turn, level[turn], &move, NULL);
  112.             if (num_of_players == 1)
  113.                 printf("\n\nI dropped my piece into column %d.\n", move+1);
  114.             else
  115.                 printf("\n\nPlayer %c dropped its piece into column %d.\n",
  116.                        piece[turn], move+1);
  117.         }
  118.  
  119.         turn = !turn;
  120.  
  121.     } while (!c4_is_winner(0) && !c4_is_winner(1) && !c4_is_tie());
  122.  
  123.     print_board(width, height);
  124.  
  125.     if (c4_is_winner(0)) {
  126.         if (num_of_players == 1)
  127.             printf("You won!");
  128.         else
  129.             printf("Player %c won!", piece[0]);
  130.         c4_win_coords(&x1, &y1, &x2, &y2);
  131.         printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  132.     }
  133.     else if (c4_is_winner(1)) {
  134.         if (num_of_players == 1)
  135.             printf("I won!");
  136.         else
  137.             printf("Player %c won!", piece[1]);
  138.         c4_win_coords(&x1, &y1, &x2, &y2);
  139.         printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  140.     }
  141.     else {
  142.         printf("There was a tie!\n\n");
  143.     }
  144.  
  145.     c4_end_game();
  146.     return 0;
  147. }
  148.  
  149.  
  150. /****************************************************************************/
  151.  
  152. static int
  153. get_num(char *prompt, int lower, int upper)
  154. {
  155.     int number;
  156.     static char numbuf[40];
  157.  
  158.     do {
  159.         printf("%s", prompt);
  160.         if (!fgets(numbuf, 40, stdin) || numbuf[0] == 'q') {
  161.             printf("\nGoodbye!\n");
  162.             exit(0);
  163.         }
  164.         sscanf(numbuf, "%d", &number);
  165.     } while (!isdigit(numbuf[0]) || number < lower || number > upper);
  166.  
  167.     return number;
  168. }
  169.  
  170. /****************************************************************************/
  171.  
  172. static void
  173. print_board(int width, int height)
  174. {
  175.     int x, y;
  176.     char **board, spacing[2], dashing[2];
  177.  
  178.     board = c4_board();
  179.  
  180.     spacing[1] = dashing[1] = '\0';
  181.     if (width > 19) {
  182.         spacing[0] = '\0';
  183.         dashing[0] = '\0';
  184.     }
  185.     else {
  186.         spacing[0] = ' ';
  187.         dashing[0] = '-';
  188.     }
  189.  
  190.     printf("\n");
  191.     for (y=height-1; y>=0; y--) {
  192.  
  193.         printf("|");
  194.         for (x=0; x<width; x++) {
  195.             if (board[x][y] == C4_NONE)
  196.                 printf("%s %s|", spacing, spacing);
  197.             else
  198.                 printf("%s%c%s|", spacing, piece[(int)board[x][y]], spacing);
  199.         }
  200.         printf("\n");
  201.  
  202.         printf("+");
  203.         for (x=0; x<width; x++)
  204.             printf("%s-%s+", dashing, dashing);
  205.         printf("\n");
  206.     }
  207.  
  208.     printf(" ");
  209.     for (x=0; x<width; x++)
  210.         printf("%s%d%s ", spacing, (x>8)?(x+1)/10:x+1, spacing);
  211.     if (width > 9) {
  212.         printf("\n ");
  213.         for (x=0; x<width; x++)
  214.             printf("%s%c%s ", spacing, (x>8)?'0'+(x+1)-((x+1)/10)*10:' ',
  215.                               spacing);
  216.     }
  217.     printf("\n\n");
  218. }
  219.  
  220. /****************************************************************************/
  221.  
  222. static void
  223. print_dot(void)
  224. {
  225.     printf(".");
  226.     fflush(stdout);
  227. }
  228.