Parsing English commands
(This stage of the project is done in groups.)
The first stage of the project is to recognize the commands the user enters
and split them up into their relevant words.
Although understanding English might appear to be quite complex, in reality, most
early IF games took a simplified approach: every command was assumed to be
of the form verb noun
.
Your task for this stage of the project is three-fold:
Using
string
andgetline
, accept input (repeatedly) from the user and split it into the noun and verb parts. At this level, you can assume that the user will only type the noun and the verb, with one or more spaces between them.Support the following commands:
Command Description look Describe the location look noun Describe noun get noun Pick up noun drop noun Drop the noun inventory List everything the player is carrying right now go direction Move in the given direction quit Exit the game If you want to add extra commands beyond these, feel free, however, any unrecognized commands should result in an error message, such as
You don’t know how to do that.
Since there’s only one location at this point, the
go
command should always print “You can’t go that way.”.You should only be able to
get
an object that you haven’t already picked up, and likewise, you should only be able todrop
an object that you are already carrying.Create an environment with one “room” (location) and one object. At a minimum, the player should be able to pick up and drop the object. You can make the “setting” of your game anything you like. Both the location and object should have full descriptions so that if you
look
at them they are described in detail.
If you want an example of what your stage 1 project should look like, run
project-if-1
on the server. This is an example game provided for you to
play with. Here’s a transcript of a playthrough of project-if-1
:
You are in a small, damp cave. Moisture oozes down the rock walls.
It's very dark; you can barely see.
There is a small lamp lying on the ground.
> look
You are in a small, damp cave. Moisture oozes down the rock walls.
It's very dark; you can barely see.
There is a small lamp lying on the ground.
> look lamp
The lamp is old and rusty, but still appears functional.
> get lamp
Taken.
> look
You are in a small, damp cave. Moisture oozes down the rock walls.
It's very dark; you can barely see.
> inventory
You are carrying:
a lamp
> look lamp
The lamp is old and rusty, but still appears functional.
> drop lamp
Dropped.
> look rock
You don't see anything interesting.
> look cave
You don't see anything interesting.
> inventory
You aren't carrying anything.
> get lamp
Taken.
> light lamp
The lamp begins to glow...
> look
You are in a small, damp cave. Moisture oozes down the rock walls.
It's very dark; you can barely see.
> drop lamp
Dropped.
> eat lamp
You don't know how to do that.
> look
You are in a small, damp cave. Moisture oozes down the rock walls.
It's very dark; you can barely see.
There is a small lamp lying on the ground.
The lamp is glowing faintly.
> quit
Notice that when you look
, the description of the room changes depending
on whether or not we’re carrying the lamp. Similarly, you can try to look
at things like the rock walls or the cave itself, and you get an error message.
(But one that tries not to break the immersion! Rather than saying “that’s an
error”, the game pretends that you can look at anything, it’s just that most
things aren’t “interesting”.)
The sample adds a command to light the lamp; the code has to keep track of whether or not the lamp is lit, and adjust the messages printed accordingly.
Group project
Stage 1 (and only stage 1) is a group project; if you have friends you want to be in a group with, send me an email to let me know, otherwise, I will assign you to a group. Within your group you can collaborate, share ideas, and even submit identical code if you want.
Implementation hints
If you’re having trouble getting started, try these hints. (You don’t have to follow these hints if you want to structure your program differently.)
Almost all of
main
will be awhile(true)
loop.Any variables which should keep their values from one “game turn” to the next should be declared before the loop. (Remember that variables declared inside a loop are “reset” every time the loop runs.)
Use
getline
to get the player’s input, and then use two string variables, together withsubstr
andfind
/rfind
like we did in class to split the input into noun and verb. Note that thelook
verb comes in two forms: one with a noun (meaning “look at this”, and one without (meaning “look at my general surroundings”). This is why you have to usegetline
, you can’t use the normalcin >>
, because you don’t know whether the input will consist of one word or two (and later stages will add commands with three and more words).Use a big
if-else if-else
chain to compare these variables to all the commands and noun(s) your game understands, and respond to them in the appropriate way. E.g., if you have a variableverb
which holds the first word, and another variablenoun
which holds the second (or the empty string, if there is no second word), you could do something like:if(verb == “look” and noun == “”) { // Print room description } else if(verb == “look” and noun == “lamp”) { // Print lamp description } … else { // All other (unknown) commands, print error }
Use a
bool
variable to keep track of whether or not the player is carrying the object. When the playerGET
s the object, set it totrue
; when the player drops it, set it tofalse
. Use this variable in the aboveif-else if-else
conditions when you need to make something different happen depending on whether or not the player is carrying the object.The
quit
command can justbreak
from thewhile(true)
loop.
Thus, your main will end up looking something like this:
int main() {
bool carrying_potato = false; // Is the player carrying the potato?
while(true) {
string line;
getline(cin, line);
string verb = ... ; // Split line into verb, noun
string noun = ... ;
if(verb == "look" and noun == "") {
}
else if(verb == "get" and noun == "whatever") {
}
else if(verb == "quit") {
break;
}
else {
cout << "You don't know how to do that." << endl;
}
} // end of while
}
Submitting
To start on stage 1, run the commands
cd projects
cd stage1
micro whatever.cpp
(Replace whatever
with the name of your game; you can name it anything
you want.) Repeat these commands every time you login to work on the project.
Save your source code file(s) into ~/projects/stage1/
.