Lab 15: Zombie, Zombie!
Due by the end of class
Introduction
The goal of this lab is to give you a little practice with C++. As you know, C++ is an object-oriented language like Java. The biggest difference between C++ and C is that C++ uses classes. Recall that C++ memory allocation uses new
and delete
operators. For this lab, use the C++ style input and output objects cin
and cout
instead of scanf()
and printf()
. Also, remember that the keyword this
is a pointer to the current object. Please use g++
to compile your code.
Lab Exercise
The final program created in this lab simulates a fight between a human and a zombie. Stupidly, the human goes toe to toe with the zombie. Although most zombies are stronger than humans, humans do slowly regenerate life and have the chance to make critical hits (and unfortunately critical misses from time to time). All you have to do is go through and finish the tasks marked TODO
in the following files.
There are no tasks for you to complete in the following header files, but you will still need to download them into your working directory.
Once you are finished editing the files, create a makefile which will compile them into an executable called battle
.
Useful Information
The tasks are mostly self-explanatory. Refer to the lecture notes for information on how to use cin
and cout
.
There is one additional issue you should be aware of. There is a getline()
method which allows you to read in an entire line of text at once. This method is useful in this lab and in general cases when you want to read a name with spaces in it or some other long sequence of text in which newlines, not whitespace, are the important delimiters.
The first argument to getline()
is the istream
object you're reading from. The second argument is the string
you want to read data into. For more information about the getline()
method, check here. Below is an example of code used to read a sentence from the command line.
string sentence; getline(cin, sentence);
Note: There is, of course, a hidden gotcha with getline()
similar to other line-reading code we have seen. It will read every character up to a newline (\n
). However, a normal use of cin
will often leave a newline in the input stream. To deal with this problem, you can call the ignore()
method, also inside of cin
. Called with no parameters, it will ignore a single character, which is often what you want. However, you can tell it to ignore a specified number of characters or even a specified number of characters until you hit some specific delimiter. Although the ignore()
method is useful, you should not put it before every use of getline()
. It only helps when some other input has be done immediately before the getline()
and has left a newline sitting in the stream. For more information about the ignore()
method, check here. Below is an example of code used to read an int
value, clear the newline that follows, and then read a verse of a poem.
int value; string verse; cin >> value; cin.ignore(); //ignores a single char, presumably a newline getline(cin, verse);
Sample Output
Below is an example of the output your program should have. In this case, the user entered Mumm-Ra the Ever-Living
as the name of the zombie and gave him a strength of 120. The user named the human Lion-O
and gave him a strength of 90. Even if you enter the same values, your output is likely to be different since the program relies on a random number generator. As usual, user input is given in green
.
Enter zombie's name: Mumm-Ra the Ever-Living Enter zombie's strength: 120 Enter human's name: Lion-O Enter human's strength: 90 Turn 1: Lion-O hit Mumm-Ra the Ever-Living for 2 points of damage. Turn 2: Mumm-Ra the Ever-Living hit Lion-O for 3 points of damage. Turn 3: Lion-O hit Mumm-Ra the Ever-Living for 2 points of damage. Turn 4: Mumm-Ra the Ever-Living hit Lion-O for 5 points of damage. Turn 5: *Critical hit!* Lion-O hit Mumm-Ra the Ever-Living for 20 points of damage. Turn 6: Mumm-Ra the Ever-Living hit Lion-O for 2 points of damage. Turn 7: *Critical hit!* Lion-O hit Mumm-Ra the Ever-Living for 20 points of damage. Turn 8: Mumm-Ra the Ever-Living hit Lion-O for 7 points of damage. Turn 9: *Critical hit!* Lion-O hit Mumm-Ra the Ever-Living for 20 points of damage. Turn 10: Mumm-Ra the Ever-Living hit Lion-O for 7 points of damage. Turn 11: Lion-O hit Mumm-Ra the Ever-Living for 5 points of damage. Turn 12: Mumm-Ra the Ever-Living hit Lion-O for 10 points of damage. Turn 13: Lion-O hit Mumm-Ra the Ever-Living for 5 points of damage. Turn 14: Mumm-Ra the Ever-Living hit Lion-O for 5 points of damage. Turn 15: Lion-O hit Mumm-Ra the Ever-Living for 3 points of damage. Turn 16: Mumm-Ra the Ever-Living hit Lion-O for 3 points of damage. Turn 17: Lion-O hit Mumm-Ra the Ever-Living for 5 points of damage. Turn 18: Mumm-Ra the Ever-Living hit Lion-O for 5 points of damage. Turn 19: Lion-O hit Mumm-Ra the Ever-Living for 3 points of damage. Turn 20: Mumm-Ra the Ever-Living hit Lion-O for 4 points of damage. Turn 21: Lion-O hit Mumm-Ra the Ever-Living for 6 points of damage. Turn 22: Mumm-Ra the Ever-Living hit Lion-O for 3 points of damage. Turn 23: *Critical miss!* Lion-O hit Mumm-Ra the Ever-Living for 0 points of damage. Turn 24: Mumm-Ra the Ever-Living hit Lion-O for 7 points of damage. Turn 25: Lion-O hit Mumm-Ra the Ever-Living for 6 points of damage. Turn 26: Mumm-Ra the Ever-Living hit Lion-O for 9 points of damage. Turn 27: Lion-O hit Mumm-Ra the Ever-Living for 5 points of damage. Turn 28: Mumm-Ra the Ever-Living hit Lion-O for 5 points of damage. Turn 29: Lion-O hit Mumm-Ra the Ever-Living for 4 points of damage. Turn 30: Mumm-Ra the Ever-Living hit Lion-O for 8 points of damage. Turn 31: Lion-O hit Mumm-Ra the Ever-Living for 6 points of damage. Turn 32: Mumm-Ra the Ever-Living hit Lion-O for 5 points of damage. Turn 33: Lion-O hit Mumm-Ra the Ever-Living for 3 points of damage. Turn 34: Mumm-Ra the Ever-Living hit Lion-O for 9 points of damage. Turn 35: Lion-O hit Mumm-Ra the Ever-Living for 9 points of damage. The zombie Mumm-Ra the Ever-Living was defeated by the human Lion-O!
Turn In
Zip the contents of your lab directory, including the makefile and the source C++ files. Upload this zip file to Brightspace. Do not include any object files or executables. Running the make
command must compile the required C++ source code files and generate an executable named battle
.
All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.