brainfrz

Guess My Number

Jan 12th, 2016
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. /*************************************************************************************************
  2. *  Program Name: Guess My Number Game                                                           *
  3. *  Author:       Terry Weiss                                                                    *
  4. *  Date Written: 1/12/2016                                                                      *
  5. *************************************************************************************************
  6. *    This program runs the classic number guessing game. The computer guesses a number between  *
  7. *  1 and 100, and the player must then guess that number. The computer will tell the player if  *
  8. *  the guess is high or low, or if it's correct. The player may also decide to choose a number  *
  9. *  and have the computer try to guess it.
  10. *************************************************************************************************/
  11.  
  12. #include <cstdlib>
  13. #include <ctime>
  14. #include <iostream>
  15.  
  16.  
  17.  
  18. enum mainOption { GUESSER = 1, CHOOSER_DUMB, CHOOSER_SMART, EXIT };
  19.  
  20.  
  21. const int MIN_GUESS = 1;
  22. const int MAX_GUESS = 100;
  23. const std::string PLAY_AGAIN_MSG = "\nThat was fun! Let's play again!\n";
  24.  
  25.  
  26. mainOption mainMenu();
  27. void playGuesser();
  28. void playChooser(bool smart);
  29.  
  30.  
  31.  
  32. int main() {
  33.     std::srand(static_cast<unsigned int>(time(0)));
  34.  
  35.     mainOption option;
  36.     do {
  37.         option = mainMenu();
  38.  
  39.         switch (option) {
  40.         case GUESSER:
  41.             playGuesser();
  42.             break;
  43.         case CHOOSER_DUMB:
  44.             playChooser(false);
  45.             break;
  46.         case CHOOSER_SMART:
  47.             playChooser(true);
  48.             break;
  49.         case EXIT:
  50.             std::cout << "We should do this again some time!\n\n";
  51.             break;
  52.         default:
  53.             std::cout << "**ERROR**: Invalid menu option  {" << option << "}\n\n";
  54.         }
  55.     } while (option != EXIT);
  56.  
  57.     return EXIT_SUCCESS;
  58. }
  59.  
  60.  
  61.  
  62. mainOption mainMenu() {
  63.     mainOption option;
  64.  
  65.  
  66.     std::cout << "  1. Play as guesser\n"
  67.               << "  2. Play as chooser\n"
  68.               << "  3. Play as chooser (against smartest guesses)\n"
  69.               << "  4. Exit\n";
  70.  
  71.     do {
  72.         std::cout << "Please choose a game mode to begin: ";
  73.         std::cin >> option;
  74.  
  75.         if (option < 1 || option > EXIT) {
  76.             std::cout << "Invalid option. ";
  77.         }
  78.     } while (option < 1 || option > EXIT);
  79.  
  80.  
  81.     return option;
  82. }
  83.  
  84.  
  85. void playGuesser() {
  86.     int number = (std::rand() % (MAX_GUESS - MIN_GUESS + 1)) + MIN_GUESS;
  87.     int guess, guesses = 0;
  88.  
  89.     std::cout << "\nOkay, I'm ready!" << std::endl;
  90.  
  91.     do { //Repeat until number is guessed correctly
  92.         std::cout << "Please pick an integer between " << MIN_GUESS << " and " << MAX_GUESS << ": ";
  93.         std::cin >> guess;
  94.  
  95.         if (guess < MIN_GUESS || guess > MAX_GUESS) {
  96.             std::cout << "Sorry, invalid guess. ";
  97.         }
  98.         else {
  99.             guesses++;
  100.  
  101.             if (guess < number) {
  102.                 std::cout << "No, that's too low." << std::endl;
  103.             }
  104.             else if (guess > number) {
  105.                 std::cout << "Sorry, that's too high." << std::endl;
  106.             }
  107.             else {
  108.                 std::cout << "Congrats, you got it!! It only took " << guesses << " guess"
  109.                     << ((guesses == 1) ? "" : "es") << "." << std::endl; //pluralize "guesses"
  110.             }
  111.         }
  112.     } while (guess != number);
  113.  
  114.     std::cout << PLAY_AGAIN_MSG;
  115. }
  116.  
  117. void playChooser(bool smart) {
  118.     enum resultOption { HIGH = 1, LOW, CORRECT };
  119.  
  120.     int guess, guesses = 0;
  121.     int min, max;
  122.     resultOption result;
  123.     bool cheater = false;
  124.  
  125.  
  126.  
  127.     std::cout << "\nAlrighty, pick a number between " << MIN_GUESS << " and " << MAX_GUESS
  128.         << " but don't tell me!" << std::endl
  129.         << "Hit any key when you're ready..." << std::endl;
  130.     std::cin.get();
  131.  
  132.  
  133.     min = MIN_GUESS;
  134.     max = MAX_GUESS;
  135.     do { //until the guess is right or it's determined the player is cheating
  136.         if (smart) {
  137.             guess = (max - min) / 2;
  138.         }
  139.         else {
  140.             guess = std::rand() % (max - min + 1) + min;
  141.         }
  142.  
  143.         std::cout << "  1. Too high" << std::endl
  144.             << "  2. Too low" << std::endl
  145.             << "  3. Correct!" << std::endl;
  146.         do {
  147.             std::cout << "Is it " << guess << "? ";
  148.             std::cin >> result;
  149.  
  150.             if (result < HIGH || result > CORRECT) {
  151.                 std::cout << "Invalid option. ";
  152.             }
  153.         } while (result < HIGH || result > CORRECT);
  154.  
  155.         if (result == CORRECT) {
  156.             std::cout << "Awesome! I got it right in " << guesses << " guesses." << std::endl;
  157.         }
  158.         else if (result == HIGH || result == LOW) {
  159.             if (max == min) { //If it's the only option left, it can't be wrong.
  160.                 cheater = true;
  161.             }
  162.             else if (result == HIGH) {
  163.                 if (guess == min) { //It can't be lower than the min.
  164.                     cheater = true;
  165.                 }
  166.                 else {
  167.                     max = guess - 1;
  168.                 }
  169.             }
  170.             else if (result == LOW) {
  171.                 if (guess == max) { //It can't be higher than the max.
  172.                     cheater = true;
  173.                 }
  174.                 else {
  175.                     min = guess + 1;
  176.                 }
  177.             }
  178.  
  179.             if (cheater) {
  180.                 std::cout << "Hmm, it seems like someone likes to cheat ;)" << std::endl
  181.                     << "You had me going there for " << guesses << "guesses." << std::endl;
  182.             }
  183.         }
  184.         else {
  185.             std::cout << "**ERROR**: Invalid result. {" << result << "}" << std::endl;
  186.         }
  187.  
  188.     } while (result != CORRECT && !cheater);
  189.  
  190.  
  191.     std::cout << PLAY_AGAIN_MSG;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment