Monday, December 26, 2016

Programming Challenge 5.20 - Random Number Guessing Game

/* Random Number Guessing Game - 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. */

#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;

    /* 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;
    }

    /* 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 again: */
                cout << "\nGuess my number: ";
                cin >> guess;
            }
        }

        /* 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 again: */
                cout << "\nGuess my number: ";
                cin >> guess;
            }
        }

        /* If statement: If the number was guessed correctly, this
           statement will execute */
        if (guess == randomNumber)
        {
            /* Display: A congratulatory message */
            cout << "\nFire and Brimstone!\n"
                 << "The number, " << randomNumber
                 << " was right, yet I wonder why?!\n"
                 << "Thou must be a witch or a witcher with a third eye!\n"
                 << "For now, don't thank me, i'll tell you goodbye,\n"
                 << "but dare and come back, you'll end as fish fry ...!\n";
        }
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment