More locations and objects
In this stage, you’ll add at least two more locations and two more objects to
your project. At least one of your additional objects must be something the
player can get
and drop
. Note that it must be possible for the player to
pick up an object in one location, and then drop it in another, and have the
look
descriptions of both locations reflect this change. The other major
change is that the player should also be able to type a command like get the rock
and have it mean the same thing as get rock
.
You don’t need to add any new commands, assuming you had a do-nothing
go
command in stage 1. It’s up to you what directions you support, but
I’d suggest considering the compass directions, and maybe up, down, in and out.
Similarly, the layout of your locations is up to you, but should make sense
(if the player goes north from the barn and gets to the farmhouse, going south
from the farmhouse should take them back to the barn.)
You will need to filter out “stopwords” from the player’s input. The list of stopwords we don’t care about are
“the”
“with”
“on”
“in” (unless used as a direction)
Implementation-wise, you should begin to structure your program around functions.
At a minimum, you must have one function for each command. You will probably
have to make some variables from main
into global variables (accessible
by all functions), by moving them to the top of your source file, just
after using namespace std;
.
Implementation hints
(As before, you don’t have to structure your program this way, but if you’re stuck, here are some hints.)
If you have one function for each command, then your if-else if-else
in
main
should look something like this:
...
else if(verb == "get") {
get(noun);
}
else if(verb == "drop") {
drop(noun);
}
...
I.e., none of the game “logic” is in main
; main
just checks the player’s
input and then calls the relevant function, passing it the noun if necessary.
If you have more than one object, using a bool
per-object to keep track of
whether or not the player is carrying it gets tedious. Instead, consider
using several vector<string>
s, one for the player’s inventory, and then
one for each location. The vectors store all the objects which exist in
the player’s inventory or location. get
-ing an item means push_back
-ing it
into the player’s inventory, and then erase
-ing it from the current room.
drop
-ing is the opposite. If you’re clever, you can write a helper
function which moves a string
from one vector to another, and then both
get
and drop
can make use of that.
You’ll need a way to keep track of where the player is. I suggest an string
variable called current_room
or something like that. Store the name of the
room in the variable. The go
command
then just looks at the current room, determines if the direction is valid,
and if it is, updates current_room
to a new value. E.g.,
if(verb == “go” and noun == “north” and currentroom == “kitchen”) { currentroom = “living room”; }
To remove stopwords, you can do a kind of search-and-replace on the user’s
input, before you split it into noun and verb. Use .find()
to search for
a stopword, and then use .erase()
to remove it from the string. Do this
repeatedly until s.find() >= s.length()
.
Submitting
To start on stage 2, copy your code from stage 1 into stage 2 (you only have to do this once):
cp projects/stage1/* projects/stage2/
Then, to edit stage 2:
cd projects
cd stage2
micro whatever.cpp
(Using the same name for your game that you used in stage 1.) Again, repeat
these three commands (cd, cd, micro
) every time you login to work on
stage 2.
Save your source code file(s) into ~/projects/stage2/
.