Tuesday, December 27, 2016

Programming Challenge 5.21 - Random Number Guessing Game Enhancement

/* Random Number Guessing Game Enhancement - This program generates a
   random number and asks the user to guess what the number is. If the
   user's guess is higher than the random number, the program displays
   "Too high, try again." If the users guess is lower than the random
   number, the program displays "Too low, try again." The program uses
   a loop that repeats until the user correctly guessed the random number.
 
   Enhancement: This program, written in Programming Challenge 5.20 now
   keeps count of the number of guesses. When the user correctly guesses
   the random number, the program displays the number of guesses. */

#include "Utility.h"

int main()
{
    /* Constants: Smallest number to guess, Largest number to guess */
    const int SMALLEST_NUMBER = 1,
              LARGEST_NUMBER = 100;

    /* Variables: Random number, Guess, Guessing counter
    (counter variable) */
    int randomNumber = 0,
        guess = 0,
        guessingCounter = 0;

    /* Enhancement: Number of guesses (counter variable), initialized to 1,
       (to account for the first priming read of guess, that takes place
       outside the for loop */
    int    numGuesses = 1;

    /* Get the system time and seed the random number generator */
    srand((unsigned int)time(NULL));

    /* Generate random number */
    randomNumber = (rand() % (LARGEST_NUMBER - SMALLEST_NUMBER + 1)) +
        SMALLEST_NUMBER;

    /* Display: Welcome message */
    cout << "\t\tRANDOM NUMBER GUESSING GAME\n\n"
        << "Welcome, Brave Hero, I challenge thee!\n"
        << "Guess the right number between 1 to "
        << LARGEST_NUMBER << ",\n"
        << "and you might be set free!\n";

    /* Ask for a first guess */
    cout << "\nGuess my number: ";
    cin >> guess;

    /* While loop::Input Validation: While the guess the user entered is
    lower than SMALLEST_NUMBER (1), or higher than LARGEST_NUMBER,
    this loop will iterate */
    while (guess < SMALLEST_NUMBER || guess > LARGEST_NUMBER)
    {
        /* Display: Error message */
        cout << "\nIt seems thou art not listening to me! The number\n"
            << "to guess is greater than 0, and up to "
            << LARGEST_NUMBER << ", you see?\n";

        /* Ask again: */
        cout << "\nGuess my number: ";
        cin >> guess;

        /* Enhancement::Counter: The number of guesses is decremented
           by -1 so that, whenever the number entered is out of bounds,
           and this loop is executed, the guess will not be counted */
        guessingCounter -= 1;
    }

    /* If Statement::Enhancement: If the number was guessed correctly,
       and the number of guesses is equal to 1, this statement will be
       executed */
    if (guess == randomNumber && numGuesses == 1)
    {
           /* Display: A congratulatory message and crown the user as new
               king or queen */
        cout << "\nShouldst I believe it?! Hardly can I!\n"
            << "The number " << randomNumber << " was right,"
            << " after only " << numGuesses << " try!\n"
            << "Thou art the master, and i can't deny,\n"
            << "the crown and the kingdom is yours now, my\n"
            << "humblest goodbye!\n";
    }

    /* For loop: As long as the guess is not equal to the random number,
       this loop will execute */
    for (guessingCounter; (!(guess == randomNumber)); guessingCounter++)
    {
        /* Nested While loop: While the number is too low, this statement is
        executed */
        while (guess < randomNumber)
        {
            cout << "\nWhat meets my cat's eyes? Your guess was too low,\n"
                << "yet very nice! Aim high, and give it another try!\n";

            /* Ask for the next guess */
            cout << "Guess my number: ";
            cin >> guess;

            /* Nested While loop::Input Validation: While the guess the user
            entered is lower than SMALLEST_NUMBER (1), or higher than
            LARGEST_NUMBER, this loop will iterate */
            while (guess < SMALLEST_NUMBER || guess > LARGEST_NUMBER)
            {
                /* Display: Error message */
                cout << "\nIt seems thou art not listening to me! The number\n"
                    << "to guess is greater than 0, and up to "
                    << LARGEST_NUMBER << ", you see?\n";

                /* Ask for the next guess */
                cout << "Guess my number: ";
                cin >> guess;

                /* Enhancement::Counter: The number of guesses is decremented
                by -1 so that, whenever the number entered is out of bounds,
                and this loop is executed, the guess will not be counted */
                numGuesses -= 1;
            }

            /* Enhancement::Counter: The number of guesses is incremented
               by +1, every time this nested while loop executes */
            numGuesses += 1;
        }

        /* Nested While loop: While the guess is too high, this statement
        is executed*/
        while (guess > randomNumber)
        {
            /* Display: Message that the guess was too high */
            cout << "\nWith your guess you were aiming too high,\n"
                << "how about another try?\n";

            /* Ask for the next guess */
            cout << "Guess my number: ";
            cin >> guess;

            /* Nested While loop::Input Validation: While the guess the
            user entered is lower than SMALLEST_NUMBER (1), or higher
            than LARGEST_NUMBER, this loop will iterate */
            while (guess < SMALLEST_NUMBER || guess > LARGEST_NUMBER)
            {
                /* Display: Error message */
                cout << "\nIt seems thou art not listening to me! The number\n"
                    << "to guess is greater than 0, and up to "
                    << LARGEST_NUMBER << ", you see?\n";

                /* Ask for the next guess */
                cout << "Guess my number: ";
                cin >> guess;

                /* Enhancement::Counter: The number of guesses is decremented
                by -1 so that, whenever the number entered is out of bounds,
                and this loop is executed, the guess will not be counted */
                numGuesses -= 1;
            }

            /* Enhancement::Counter: The number of guesses is incremented
            by +1, every time this nested while loop executes */
            numGuesses += 1;
        }

        /* If Statement: If the number was guessed correctly, this
        statement will execute

        If Statement::Enhancement: If the number was guessed correctly,
        and the number of guesses is greater than or equal to 1, or
        number of guesses is lower than or equal to 5, this statement
        will be executed */
        if (guess == randomNumber && numGuesses <= 5)
        {
            /* Display: A congratulatory message */
            cout << "\nFire and Brimstone!\n"
                << "The number " << randomNumber << " was right!\n"
                << "Yet in only " << numGuesses << " guesses,"
                << " there's something fishy,\n"
                << "i can feel it inside!\n"
                << "Thou must be a witch or a witcher, cursed be thy"
                << " third eye ...\n"
                << "For now you are free, though, if e'er thou'll comethst back,\n"
                << "the challenge'll be harder, believe thou me that  ... !\n";
        }

        /* Else If Statement::Enhancement: If number was guessed correctly, and
        the number of guesses is higher than or equal to 10, this statement
        will be executed */
        else if (guess == randomNumber && numGuesses >= 10)
        {
            /* Display: A message to "try harder" */
            cout << "\nTo guess my number " << randomNumber
                 << " took thee " << numGuesses << " tries!\n"
                 << "Now the doors are wide open, so get out of my eyes!\n"
                 << "Though before thou leavest, and knoweth what's best for thee,\n"
                 << "harken these words of advice, for they are free!\n"
                 << "If thou dost not try harder, shouldst thou ever return,\n"
                 << "thy cell will be waiting, 'til on the pyre thou'll burn.\n";
        }
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment