home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_2 / guess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  1.7 KB  |  81 lines

  1.  
  2.  
  3. // GUESS.C - An example of input driven event loops
  4.  
  5. // I N C L U D E S ////////////////////////////////////////////////////////////
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10.  
  11. // M A I N ////////////////////////////////////////////////////////////////////
  12.  
  13. void main(void)
  14. {
  15.  
  16. int done=0,        // exit flag
  17.      number,        // the random nunber
  18.      num_tries=0,   // number of tries
  19.      guess;         // the players guess
  20.  
  21. unsigned int far *clock = (unsigned int far *)0x0000046CL; // pointer to clock
  22.  
  23. // SECTION 1 //////////////////////////////////////////////////////////////////
  24.  
  25. // print out introductory instructions
  26.  
  27. printf("\nI'm thinking of a number from 1-100.");
  28. printf("\nTry and guess it!\n");
  29.  
  30. // seed the random number generator with the time
  31.  
  32. srand(*clock);
  33.  
  34. // choose a random number from 1-100
  35.  
  36. number = 1 + rand() % 100;
  37.  
  38. // SECTION 2 //////////////////////////////////////////////////////////////////
  39.  
  40. // main event loop
  41.  
  42. while(!done)
  43.       {
  44.  
  45. // SECTION 3 //////////////////////////////////////////////////////////////////
  46.  
  47.       // query user for input (the event)
  48.  
  49.       printf("\nWhat's your guess?");
  50.       scanf("%d",&guess);
  51.  
  52. // SECTION 4 //////////////////////////////////////////////////////////////////
  53.  
  54.       // increment number of tries
  55.  
  56.       num_tries++;
  57.  
  58.       // process the event
  59.  
  60.       if (guess > number)
  61.           printf("\nToo big!\n");
  62.       else
  63.       if (guess < number)
  64.           printf("\nToo small!\n");
  65.       else
  66.           {
  67.           // the user must have guessed the number
  68.  
  69.           printf("\nYou guessed the number in %d tries!!!\n",num_tries);
  70.  
  71.           // set the exit flag
  72.  
  73.           done=1;
  74.  
  75.           } // end else
  76.  
  77.       } // end while
  78.  
  79. } // end main
  80.  
  81.