brainfrz

Guess My Number #2

Jan 13th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 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. int 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.     int 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. int mainMenu() {
  63.     int 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.     int 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.         guesses++;
  143.  
  144.         std::cout << "  1. Too high"  << std::endl
  145.                   << "  2. Too low"   << std::endl
  146.                   << "  3. Correct!"  << std::endl;
  147.         do {
  148.             std::cout << "Is it " << guess << "? ";
  149.             std::cin >> result;
  150.  
  151.             if (result < 1 || result > CORRECT) {
  152.                 std::cout << "Invalid option. ";
  153.             }
  154.         } while (result < 1 || result > CORRECT);
  155.  
  156.         if (result == CORRECT) {
  157.             std::cout << "Awesome! I got it right in " << guesses << " guesses." << std::endl;
  158.         }
  159.         else if (result == HIGH || result == LOW) {
  160.             if (max == min) { //If it's the only option left, it can't be wrong.
  161.                 cheater = true;
  162.             }
  163.             else if (result == HIGH) {
  164.                 if (guess == min) { //It can't be lower than the min.
  165.                     cheater = true;
  166.                 }
  167.                 else {
  168.                     max = guess - 1;
  169.                 }
  170.             }
  171.             else if (result == LOW) {
  172.                 if (guess == max) { //It can't be higher than the max.
  173.                     cheater = true;
  174.                 }
  175.                 else {
  176.                     min = guess + 1;
  177.                 }
  178.             }
  179.  
  180.             if (cheater) {
  181.                 std::cout << "Hmm, it seems like someone likes to cheat ;)" << std::endl
  182.                           << "You had me going there for " << guesses << " guesses." << std::endl;
  183.             }
  184.         }
  185.         else {
  186.             std::cout << "**ERROR**: Invalid result. {" << result << "}" << std::endl;
  187.         }
  188.  
  189.     } while (result != CORRECT && !cheater);
  190.  
  191.  
  192.     std::cout << PLAY_AGAIN_MSG;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment