Assignment 5 Pot O’ Gold! A Game Using Branches and Loops Solution

$30.00

Description

Overall Assignment

For this assignment you are to write a simple computer game in which a player attempts to locate a pot of Leprechaun gold hidden in a building before it disappears. This game makes extensive use of branches and loops, with specific constructs required in specific situations. The extent of the building is 0 to 99 in all (3) directions, and the player initially has 200 seconds (20 tries) to find the gold before it disappears. Winning makes the game harder, losing makes it easier.

Program Pseudocode

  1. Initialize. Explain the program to the user, set initialTries to 20, call srand( time( NULL ) ). Declare all variables, including bool variables named “won” and “repeat”.

  1. Calculate gold location as random integers for xGold, yGold, and zGold in the range from 0 to 99 inclusive. ( Use rand( ) and the % operator for this step.) Set “won” to false.

  1. Repeat the following steps initialTries times, using a for loop with a loop counter “tries”, which starts out at 0 and continues as long as tries is less than initialTries, incrementing tries at the end of each iteration. ( “tries” is the number of tries already completed. ) For each iteration, do the following:

    1. Inform the user that they have 10 * remainingTries seconds left to find the gold, where remainingTries = initialTries – tries.

    1. Ask the user for a location to test, reading in xGuess, yGuess, and zGuess. Ask for each value separately, and use while loops to only accept numbers from 0 to 99.

    1. Check to see if the guess is right, by comparing the X, Y, and Z values for equality. ( E.g. does xGold equal xGuess, etc. You will need to use && for this test. ) If the guess is correct, set “won” to true, increment tries, and then use break to exit the for loop early.

    1. Calculate the gold detector signal strength using the following (distance) formula, and report it to the user1:

= 1000 ( 1 − = , , ( )2 )

100 √3

  1. After the loop completes, check “won” to see if the user found the gold in time.

    1. If the user won, print “Congratulations!”, and report the time used and remaining.

    2. If they did not, print “POOF! The gold disappeared!” and report the time used.

( Develop and test steps 1 to 4 first. Then add steps 5 to 7 after 1 to 4 are working. )

  1. Ask the user if they want to play again, using a “while” loop to accept only a “yes” or “no” answer. ( Use a string input and comparison. ) Set “repeat” to either true or false.

  1. Use the += and conditional operators to add -1 to initialTries if won is true, or +1 if won is false.

  1. Use a do-while loop to repeat back to step 2 if “repeat” is true.

  1. The summation under the square root = ( xGuess – xGold)2 + ( yGuess – yGold)2 + ( zGuess – zGold )2

Program Details

For this assignment you are to write a simple computer game in which a user attempts to find a pot of Leprauchan gold in a building before it disappears.

Your program should first print out your name and ACCC netID ( e.g. jbell ), and explain to the user what the program does. In addition to the story line explained above, you should inform the user that they have a gold detector that reports a signal from 0 to 1000 depending on how close they are to the gold location, and that each try takes 10 seconds to complete.

Your program should then implement the algorithm shown above. It is recommended that you implement and test steps 1 to 4 first, and then add steps 5 to 7 later.

In order to use the random number generator, you must #include <cstdlib> and <ctime>. srand( time( NULL ) ) must be called one time only (before any looping) to initialize the random number generator. ( See http://www.cplusplus.com/reference/cstdlib/rand/ for more info. )

The math in step 3d should obviously be done using doubles, e.g. 1.0 instead of 1 alone.

Special Notes:

During debugging you may want to print out the gold location after it is calculated, and also set initialTries to a small number like 3 or 4. Obviously you need to remove those print statements and put initialTries back to 20 once the game is working. ( You may also want to make the building smaller. See optional enhancements. )

What to Hand In:

Your code, including a user documentation file, should be handed in using Blackboard.

All files should be zipped together into a single file, whose name is comprised of your ACCC netID followed by the course number followed by the letters “HW”, followed by the assignment number. ( E.g. jbell109HW5.zip ) The zip file should be handed in via Blackboard. ( Your TA may provide alternate instructions, which override these. )

The intended audience for the documentation file is a general end user, who might want to use this program to perform some work. They do not get to see the inner workings of the code, and have not read the homework assignment. You can assume, however, that they are familiar with the problem domain ( e.g. trigonometric identities. )

A secondary purpose of the documentation file is to make it as easy as possible for the grader to understand your program. If there is anything special the grader should know about your program, be sure to document it in the documentation file. In particular, if you do any of the optional enhancements, then you need to document what they are and anything special the TA needs to do to run your program and understand the results.

If there are problems that you know your program cannot handle, it is best to document them as well, rather than have the TA wonder what is wrong with your program.

Make sure that your name appears at the beginning of each of your files. Your program should also print this information when it runs.

Optional Enhancements:

It is course policy that students may go above and beyond what is called for in the base assignment if they wish. These optional enhancements will not raise any student’s score above 100 for any given assignment, but they may make up for points lost due to other reasons.

Make the dimensions of the building a variable, instead of fixed at 100. Three possibilities are: (1) Ask the user what size building they would like to play in. (2) Ask the user for a difficulty level, such as easy, medium, or challenging, and then set the building size accordingly. (3) Have the building size fluctuate as players win or lose, just as the time remaining does. You could also think of others, and/or combine some of these ideas.

Instead of reporting only the latest signal strength, print a table of all past guesses and the resulting signal strengths each time the user makes a new guess. Unfortunately the best way to do this involves arrays, which we have not covered, and the fact that initialTries changes from game to game requires either an upper limit on initialTries or dynamic memory allocation, which will not be covered at all in this course.

A variation on the previous idea would be to report the last N tries instead of all previous tries, where N has an upper limit of 5 or so. This can be done without arrays or dynamic memory allocation, but can be more complicated, especially at the beginning when the number of tries that the user has completed is less than the chosen upper limit.

Write a separate function to evaluate the signal strength, based on the (passed in) gold location and guess location. We will not be covering functions for a while yet.

Find a way to incorporate a switch( ) block into the program. Perhaps at the end of the game you could show them a menu of ( at least 3 ) choices, e.g. “Quit”, “Play Again”, “Review Performance” or others, and then use a switch based on their answer to perform the requested task. Putting the switch into a loop allows users to select multiple choices, one by one.

You could use a switch to let the user select a difficulty level. To do it right you would have to print a menu with numbered choices, and then have the user enter the number of their choice. You would furthermore need to put the menu into a loop, in which you only accept valid choices. The “enum” data type could be useful, but we won’t be covering that in this course.

You can make the game repeatable if you call srand( ) with a specific value before starting each game. Call this the “gameID”. You can calculate a randomly chosen gameID by calling rand( ) and modding the result with a reasonable range, such as 100,000 to yield 5-digit gameIDs. If you allow the user to enter their own gameID, then they can play the same game again, or two different players could play the same game. ( You still need to call srand( time( NULL ) ) exactly once at the beginning of the program. ) If you implement this option, then you should report to the user the game ID that they have either won or lost when you report the results.

Other enhancements that you think of – Check with TA for acceptability.