Advertisement
billymcguffin

Guessing Game 01

Mar 14th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int genRandMax(int nMax)
  8. // Generates an integer between 0 and nMax;
  9. {
  10.     return rand() % nMax;
  11. }
  12.  
  13. int main()
  14. {
  15.     srand(time(0));
  16.  
  17.     bool bRunning = true;
  18.  
  19.     // Difficulty
  20.     const int nEasyMax = 100;
  21.     const int nMedMax = 500;
  22.     const int nHardMax = 1000;
  23.  
  24.     while (bRunning)
  25.     {
  26.         int nRandNumber;
  27.  
  28.         int nPlayerDifficultyChoice;
  29.         const int *nCurrentDifficulty;
  30.         cout << "Please choose your difficulty \n1) Easy (0 - " << nEasyMax << ") \n2) Medium (0 - " << nMedMax << ") \n3) Hard (0 - " << nHardMax << ")" << endl;
  31.         cin >> nPlayerDifficultyChoice;
  32.         cout << endl;
  33.  
  34.         switch (nPlayerDifficultyChoice) {
  35.             case(1): {
  36.                 nCurrentDifficulty = &nEasyMax;
  37.                 break;
  38.             }
  39.             case(2): {
  40.                 nCurrentDifficulty = &nMedMax;
  41.                 break;
  42.             }
  43.             case(3): {
  44.                 nCurrentDifficulty = &nHardMax;
  45.                 break;
  46.             }
  47.             default: {
  48.                 nCurrentDifficulty = &nEasyMax;
  49.                 break;
  50.             }
  51.         }
  52.  
  53.         nRandNumber = genRandMax(*nCurrentDifficulty);
  54.  
  55.         bool bWon = false;
  56.  
  57.         int nPlayerGuess = 0;
  58.  
  59.         while (!bWon)
  60.         {
  61.             //cout << "The number is " << nRandNumber << endl;
  62.  
  63.             cout << "Guess an integer between 0 and " << *nCurrentDifficulty << endl;
  64.             cin >> nPlayerGuess;
  65.             cout << endl;
  66.  
  67.             if (nPlayerGuess > nRandNumber) {
  68.                 cout << "Nope, too high" << endl;
  69.             } else if (nPlayerGuess < nRandNumber) {
  70.                 cout << "Nope, too low" << endl;
  71.             } else if (nPlayerGuess == nRandNumber) {
  72.                 cout << "You got it!" << endl;
  73.                 bWon = true;
  74.             } else {
  75.                 cout << "Something went wrong :(" << endl;
  76.                 exit(-1);
  77.             }
  78.         }
  79.  
  80.  
  81.         char sContinueResponse = 'Y';
  82.         cout << "Would you like to play again? (Y/N)" << endl;
  83.         cin >> sContinueResponse;
  84.         cout << endl;
  85.  
  86.         if (toupper(sContinueResponse) == 'Y') {
  87.             bRunning = true;
  88.         } else if (toupper(sContinueResponse) == 'N') {
  89.             bRunning = false;
  90.         } else {
  91.             exit(-2);
  92.         }
  93.     }
  94.  
  95.     return(0);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement