home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / cecko / install / devcpp4920.exe / Examples / Jackpot / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-18  |  2.3 KB  |  98 lines

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6. void Start();
  7. void GetResults();
  8.  
  9. int  i, j, life, max;
  10. char c;
  11. void Start()
  12. {
  13.      i = 0;
  14.      j = 0;
  15.      life = 0;
  16.      max = 6;
  17.  
  18.      cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
  19.      cout << "1 : Easy (0-15)\n";
  20.      cout << "2 : Medium (0-30)\n";
  21.      cout << "3 : Difficult (0-50)\n";
  22.      cout << "or type another key to quit\n";
  23.      c = 30;
  24.  
  25.      cin >> c;                   // read the user's choice
  26.      cout << "\n";
  27.  
  28.      switch (c)
  29.      {
  30.         case '1' : max = 15;  // the random number will be between 0 and max
  31.         break;
  32.         case '2' : max = 30;
  33.         break;
  34.         case '3' : max = 50;
  35.         break;
  36.         default : exit(0);
  37.         break;
  38.      }
  39.  
  40.      life = 5;         // number of lifes of the player
  41.      srand( (unsigned)time( NULL ) ); // init Rand() function
  42.      j = rand() % max;  // j get a random value between 0 and max
  43.  
  44.      GetResults();
  45.  
  46. }
  47.  
  48. void GetResults()
  49. {
  50.      if (life <= 0)
  51.         // if player has no more life then he lose
  52.      {
  53.         cout << "You lose !\n\n";
  54.         Start();
  55.      }
  56.  
  57.      cout << "Type a number: \n";
  58.      cin >> i;          // read user's number
  59.  
  60.      if ((i>max) || (i<0)) // if the user number isn't correct, restart
  61.      {
  62.         cout << "Error : Number not between 0 and \n" << max;
  63.         GetResults();
  64.      }
  65.  
  66.      if (i == j)
  67.      {
  68.         cout << "YOU WIN !\n\n"; // the user found the secret number
  69.         Start();
  70.      }
  71.  
  72.      else if (i>j)
  73.      {
  74.         cout << "Too BIG\n";
  75.         life = life - 1;    // -1 to the user's "life"
  76.         cout << "Number of remaining life: " << life << "\n\n";
  77.         GetResults();
  78.      }
  79.  
  80.      else if (i<j)
  81.      {
  82.         cout << "Too SMALL\n";
  83.         life = life - 1;
  84.         cout << "Number of remaining life:\n" << life << "\n\n";
  85.         GetResults();
  86.      }
  87. }
  88.  
  89. int main()
  90. {
  91.      cout << "** Jackpot game **\n";
  92.      cout << "The goal of this game is to guess a number. You will be ask to type\n";
  93.      cout << "a number (you have 5 guess)\n";
  94.      cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to find\n\n";
  95.      Start();
  96.      return 0;
  97. }
  98.