home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap06 / getclose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-05  |  5.7 KB  |  197 lines

  1. /* getclose.c -- a number game using */
  2. /*               random numbers      */
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7.     /* external variables */
  8.     int number,     /* total number in current game   */
  9.         moves,      /* number of moves in current game*/
  10.         target,     /* target number to reach         */
  11.         done,       /* true if game is over           */
  12.         score,      /* score of current game          */
  13.         wins = 0,   /* number of games won            */
  14.         losses = 0, /* number of games lost           */
  15.         total = 0;  /* total score                    */
  16.  
  17.     char move;
  18.  
  19.     /* function prototype declarations */
  20.     void intro(void);       /* tell player about game   */
  21.     char getyn(void);       /* get yes/no response      */
  22.     int  random(int num);   /* random between 1 and num */
  23.     void new_target(void);  /* target number for game   */
  24.     char get_move(void);    /* get player's move        */
  25.     void do_move(void);     /* generate num from move   */
  26.     void check_move(void);  /* won, lost, or continue?  */
  27.     void show_score(void);  /* show score for game      */
  28.     void show_total(void);  /* show total score         */
  29.  
  30. main()
  31. {
  32.     intro();         /* print instructions */
  33.  
  34.     while (TRUE)     /* new games until user quits */
  35.         {
  36.         printf("\nDo you want to continue? ");
  37.         if (getyn() != 'y')
  38.             break;   /* exit program */
  39.  
  40.         done = FALSE;
  41.         number = moves = score = 0;
  42.         new_target();  /* target number for this game */
  43.         while (!done)  /* play one game */
  44.             {
  45.             get_move();/* user selects random number  */
  46.             do_move(); /* generate random number      */
  47.                        /* and add                     */
  48.             check_move();  /* win, lose, or continue? */
  49.             }
  50.         show_score();  /* score for this game */
  51.         show_total();  /* total score         */
  52.         }
  53. }
  54.  
  55. void intro(void)
  56. {
  57.     printf("Welcome to Getclose\n\n");
  58.     printf("The object of this game is to\n");
  59.     printf("try to get as close to the target\n");
  60.     printf("number as possible in as few\n");
  61.     printf("moves as possible by choosing from\n");
  62.     printf("various ranges of random numbers.\n");
  63.     printf("You score if you get within 4 of the\n");
  64.     printf("target; a 100 point bonus for hitting\n");
  65.     printf("the target, but no score if you go over.\n\n");
  66.     }
  67.  
  68. char getyn(void)
  69.                     /* get yes or no answer      */
  70.                     /* repeats until valid entry */
  71. {
  72.     char ch;  /* character to read and return */
  73.  
  74.     while (TRUE)
  75.         {
  76.         printf(" (y or n) ");
  77.         ch = getche();
  78.         printf("\n");
  79.         if ((ch == 'y') || (ch == 'n'))
  80.         /* valid response, break out of loop */
  81.             break;
  82.         /* give error message and loop again */
  83.         printf("please enter ");
  84.         }
  85.     return(ch);
  86.     }
  87.  
  88. int random(int num)
  89.     /* generate random number between 1 and num     */
  90.     /* doesn't use library function srand() because */
  91.     /* we don't want the same seed each time        */
  92. {
  93.     long seconds, result;
  94.     time(&seconds);   /* randomize with system time */
  95.     return (abs ((int)seconds * rand() % num) + 1);
  96. }
  97.  
  98. void new_target(void)
  99.              /* generate a new target number */
  100.              /* between 50 and 99            */
  101. {
  102.     target = 50 + random(49);
  103.     printf("\nYour target for this game is %d\n",
  104.         target);
  105. }
  106.  
  107. char get_move(void)
  108.     {
  109.     while (TRUE)
  110.         {
  111.         printf("\nPick a random number from 1 to\n");
  112.         printf("a) 5  b) 10  c) 25  d) 50  e) 100 ");
  113.         move = getche();
  114.         if ((move >= 'a') && (move <= 'e'))
  115.             {
  116.             ++moves; /* count the move */
  117.             break;   /* valid response */
  118.             }
  119.         /* invalid response, try again */
  120.         printf("\nPlease type a, b, c, d, or e\n");
  121.         }
  122. }
  123.  
  124. void do_move(void)
  125.     {
  126.     int num = 0;  /* random value to obtain */
  127.     switch (move)
  128.         {
  129.         case 'a' :
  130.             num = random(5);
  131.             break;
  132.         case 'b' :
  133.             num = random(10);
  134.             break;
  135.         case 'c' :
  136.             num = random(25);
  137.             break;
  138.         case 'd' :
  139.             num = random(50);
  140.             break;
  141.         case 'e' :
  142.             num = random(100);
  143.             break;
  144.         }
  145.     number += num;  /* add new number to total */
  146.     printf("\n\nYou got a %d. Number is now: %d ", num, number);
  147.     printf("(Target is %d)\n", target);
  148. }
  149.  
  150. void check_move(void)
  151. {
  152.     int temp;
  153.     if (number > target)
  154.         {
  155.         printf("\nYou went over! ");
  156.         printf("No score this game.\n");
  157.         losses++;
  158.         done = TRUE; /* to break out of loop */
  159.         }
  160.     if (number == target)
  161.         {
  162.         printf("\nYou hit the target ");
  163.         printf("for 100 bonus points!\n");
  164.         score = (100 / moves) + 100;
  165.         total += score;
  166.         wins++;
  167.         done = TRUE;
  168.         }
  169.     if ((number >= (target - 4)) && (number < target))
  170.         {
  171.         temp = 100 / moves;
  172.         /* does player want to go for broke? */
  173.         printf("\nTake %d points (y) or continue (n)? ",
  174.             temp);
  175.         if (getyn() == 'y')
  176.             {
  177.             score = temp;
  178.             total += score;
  179.             wins++;
  180.             done = TRUE;
  181.             }
  182.        }
  183. }
  184.  
  185. void show_score(void)
  186. {
  187.     printf("\nYou scored %d points in %d moves.\n",
  188.         score, moves);
  189. }
  190.  
  191. void show_total(void)
  192. {
  193.     printf("You have won %d games ", wins);
  194.     printf("and lost %d.\n", losses);
  195.     printf("Your total score is %d\n", total);
  196. }
  197.