Java Reference
In-Depth Information
We can fix this by adding a method for the look command to the Game class:
private void look()
{
System.out.println(currentRoom.getLongDescription());
}
You should, of course, also add a comment for this method. After this, we only need to add
a case for the look command in the processCommand method, which will invoke the look
method when the look command is recognized:
if(commandWord.equals("help")) {
printHelp();
}
else if(commandWord.equals("go")) {
goRoom(command);
}
else if(commandWord.equals("look")) {
look();
}
else if(commandWord.equals("quit")) {
wantToQuit = quit(command);
}
Try this out, and you will see that it works.
Exercise 6.14 Add the look command to your version of the zuul game.
Exercise 6.15 Add another command to your game. For a start, you could choose some-
thing simple, such as a command eat that, when executed, just prints out “ You have eaten
now and you are not hungry any more .” Later, we can improve this so that you really get hun-
gry over time and you need to find food.
Coupling between the Game , Parser , and CommandWords classes so far seems to have been
very good—it was easy to make this extension, and we got it to work quickly.
The problem that was mentioned before—implicit coupling—becomes apparent when we now
issue a help command. The output is
You are lost. You are alone. You wander
around at the university.
Your command words are:
go quit help
Now we notice a small problem. The help text is incomplete: the new command, look , is not
listed.
This seems easy to fix: we can just edit the help text string in the Game 's printHelp method.
This is quickly done and does not seem a great problem. But suppose we had not noticed this
error now. Did you think of this problem before you just read about it here?
Search WWH ::




Custom Search