Question: Guess troll's age. Create a project titled Lab4_Age with a single file titled age.cpp Program the following game. A fair young maiden is locked up

  1. Guess troll's age. Create a project titled Lab4_Age with a single file titled age.cpp Program the following game.

    • A fair young maiden is locked up in an enchanted castle guarded by an ugly old troll. Once a year, a handsome prince is allowed to come to the castle to try to free the maiden. To free her, the prince has to guess the troll's age. If the prince guesses correctly, the maiden is freed, they get married and live happily ever after. If the prince guesses incorrectly, the troll says if he is younger or older and the prince has to come next year.

    The computer selects a random age for the troll. In the beginning of the game, the troll can be between 100 and 200 years old. The user of the game plays the prince. Note that the troll gets one year older every year. The dialog should look as follows (user input is shown in bold):

     Hello, handsome prince. I am an ugly old troll. How old am I? 150 Wrong, I am older. No fair maiden for you. Better luck next year. Hello, handsome prince. I am an ugly old troll. How old am I? 175 Wrong, I am younger. No fair maiden for you. Better luck next year. Hello, handsome prince. I am an ugly old troll. How old am I? 160 Wrong, I am older. No fair maiden for you. Better luck next year. Hello, handsome prince. I am an ugly old troll. How old am I? 163 You got it! Here is the fair maiden. Live happily ever after! 
    For your program, you need to use predefined randomization functions rand() and srand() as well as the time function time()

    • Function time() returns current time (as kept by the computer) in the number of seconds passed since midnight, January 1, 1970. You need to include ctime to use this function. The function takes one argument but for this assignment the argument value is a special named constant nullptr. Check this program for an example usage.

    • To use functions rand() and srand() you need to include cstdlib Function srand() initializes the random number generator of the program. It takes a single integer argument. The value passed to srand() determines the values output by rand(). This value is called seed.

      rand() takes no arguments. Every time rand() is invoked it returns an integer value between 0 and constant RAND_MAX defined in cstdlib. Note that the sequence of values returned by repeated calls of rand() is uniquely determined by the seed passed to srand(). That is, if srand() is passed the same seed, rand() is going to return the same sequence of numbers. This is helpful for debugging. Check this program for an example of random number manipulation.

    Hints: Use fixed values of the seed for your initial debugging, use the output of time() as an unpredictable seed to start the guessing game once you debugged your program.

    Note that random value produced by rand() is out of the required range of 100-200. To get the random number into the required range, remainder it by 101 (that puts the number in 0-100 range) and then add 100.

    Do not put srand() inside the loop or anywhere, where it may be invoked more than once - it resets the random number generator.

Milestone: implement the first assignment (scientific calculator).

Make sure your program adheres to proper programming style. Submit your project to the subversion repository. Do not forget to verify your submission on the web.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!