Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Fix

By: Axed99 on Feb 23rd, 2012  |  syntax: C++  |  size: 4.61 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_VALUE = 1000;
  8.  
  9. // declare functions
  10. /** \brief the game introduction
  11.  * \return true to play or false to quit
  12.  */
  13.  
  14. bool intro ();
  15. /** \brief get a value between 1 to maximum allowed
  16. * restrict input to allowable values
  17. * \return a valid value
  18. */
  19.  
  20. int getNumberInput();
  21.  
  22. /** \brief get a random value between minVal and maxVal (inclusive)
  23.  * \note Not testing if the min is greater than max
  24.  * \param minVal lowest value for the random number
  25.  * \param maxVal highest value for the random number
  26.  * \return the random number
  27.  */
  28. int getRandom( int minVal, int maxVal);
  29.  
  30. /** \brief can play again if y is initialzed
  31.  * \return playA
  32.  *
  33.  */
  34. bool playAgain();
  35.  
  36. /** \brief gives the user a hint to what the number is
  37.  * /return 0
  38.  */
  39. int getNumber();
  40.  
  41. /** \brief initialise any data that does not change during the game
  42.  */
  43. void initGameData();
  44.  
  45. /** \brief reset any data that does not change during the game
  46.  */
  47. void resetGameData();
  48.  
  49. //! the number that the player has currently guessed
  50. int numberGuessed;
  51.  
  52. //! the current random value to guess
  53. int randomNumber;
  54.  
  55. //! the maximum attempts the player is allowed
  56. int maxAttempts;
  57.  
  58. //! an attempt counter
  59. int attemptCounter;
  60.  
  61. //! running the instance
  62. bool running;
  63.  
  64. int main()
  65. {
  66.     srand( time(NULL) );
  67.     if (!running)
  68.     {
  69.         cout << "The time is: " << time(NULL) << endl;
  70.         cout << "Max random number is: " << RAND_MAX << " rand " << rand() << endl;
  71.         // intro to the game and play or not
  72.         running = intro ();
  73.     }
  74.  
  75.     // only execute if intro () returns true
  76.     // the main game loop
  77.     // set the random value, reset the attempt counter
  78.     while (running)
  79.     {
  80.         resetGameData();
  81.         do
  82.         {
  83.             // get the current value to guess from the player
  84.             numberGuessed = getNumberInput();
  85.             getNumber();
  86.         }
  87.         while (numberGuessed != randomNumber);
  88.     }
  89.  
  90.     cout << "Goodbye and thanks for the fish" << endl;
  91.     return 0;
  92. }
  93.  
  94. int getRandom(int minVal, int maxVal)
  95. {
  96.     // get the range of values
  97.     int range = maxVal - minVal + 1;
  98.     int random = (rand() % range) + minVal;
  99.     return random;
  100. }
  101.  
  102. void initGameData()
  103. {
  104.     maxAttempts = 12;
  105.     cout << "You have reached the max limit of turns, try again next time!" << endl;
  106.     playAgain();
  107. }
  108.  
  109. void resetGameData()
  110. {
  111.     // getting the next random number to guess
  112.     randomNumber = getRandom( 1, MAX_VALUE );
  113.     // reseting the maxAttempts
  114.     maxAttempts = 0;
  115.     // set the turn counter to 0
  116.     attemptCounter = 0;
  117.     cout << "The number is:" << randomNumber << endl;
  118. }
  119.  
  120. int getNumberInput()
  121. {
  122.     int userGuess;
  123.     do
  124.     {
  125.         cout << "Enter a number between 1 and " << MAX_VALUE << " then press Enter ";
  126.         cin >> userGuess;
  127.         maxAttempts++;
  128.         attemptCounter++;
  129.         if (maxAttempts == 12 && attemptCounter == 12)
  130.         {
  131.             initGameData();
  132.         }
  133.  
  134.     }
  135.     while( userGuess < 1 || userGuess > MAX_VALUE);
  136.     cout << "You entered " << userGuess << endl;
  137.     return userGuess;
  138. }
  139.  
  140. bool intro()
  141. {
  142.     bool flag = false;
  143.     // intro to the game
  144.     cout << "A Guess the magic Number Game" << endl;
  145.     cout << "Press Y to Continue, any other key to Exit" << endl;
  146.     char ch;
  147.     cin >> ch;
  148.     // convert the char variable to upper case
  149.     ch = toupper( ch );
  150.     // the test condition
  151.     if ( ch == 'Y' )
  152.     {
  153.         // what to do if the test is true
  154.         cout << "Let's play!" << endl;
  155.         flag = true;
  156.     }
  157.     else
  158.     {
  159.         // what to do if the test is fale
  160.         cout << "See you next time!" << endl;
  161.     }
  162.  
  163.     return flag;
  164. }
  165.  
  166. bool playAgain()
  167. {
  168.     bool playAgain = false;
  169.     char ch;
  170.     cout << "Press Y to play again or any other key to exit!" << endl;
  171.     cin >> ch;
  172.     ch = toupper(ch);
  173.  
  174.     if (ch == 'Y')
  175.     {
  176.         playAgain = true;
  177.         running = true;
  178.         main();
  179.     }
  180.     else
  181.     {
  182.         playAgain = false;
  183.         cout << "See you next time!" << endl;
  184.         exit(0);
  185.     }
  186.     return playAgain;
  187. }
  188.  
  189. int getNumber()
  190. {
  191.     // check if too high/low or correct
  192.     if( numberGuessed < randomNumber )
  193.     {
  194.         cout << numberGuessed << " is too low." << endl;
  195.     }
  196.     else if(numberGuessed > randomNumber )
  197.     {
  198.         cout << numberGuessed << " is too high." << endl;
  199.     }
  200.     else // must be equal
  201.     {
  202.         cout << "The winner!" << endl;
  203.         running = false;
  204.         running = playAgain();
  205.     }
  206.     return 0;
  207. }