MS 7: Forced motion
When the player tries to go through a locked passage without the necessary key, the game has to indicate that the motion is prohibited. One possible strategy would be to design a whole new data structure to represent messages of this type. A simpler way, however, is to make a small extension to the structure that is already in place.
When Willie Crowther faced this problem in his original Adventure game, he chose the simple approach. He simply created new rooms whose descriptions contained the necessary messages he wanted to deliver. When the player entered one of those rooms, the code that you have been running all along would print out the necessary message, just like any other room description. The only problem is that you don’t actually want the player to end up in that room, but rather to be moved automatically to some other room. To implement this idea, Crowther came up with the idea of using a special motion verb called "FORCED" to specify forced motion.
When the player enters a room in which one of the connections is associated with the motion verb FORCED (and the player is carrying any object that the FORCED verb requires to unlock the passage), your program should display the description of that room and then immediately move the player to the specified destination, without waiting for the player to enter any input. This feature makes it possible to display a message to the player and then continue on from there.
The facility is illustrated by the room named "MissingKeys", which has the following definition:
MissingKeys
-
The grate is locked and you don't have any keys.
-----
FORCED: OutsideGrate
The effect of this definition is to ensure that whenever the player enters this room, after printing the message, the room will automatically be set to "OutsideGrate", without the player doing anything.
It is possible for a single room to use both the locked passage and forced motion options. The CrowtherRooms.txt file, for example, contains the following entry for the room just north of the curtain in the building:
Curtain1
-
-----
FORCED: Curtain2/NUGGET
FORCED: MissingTreasures
The effect of this set of motion rules is to force the player to the room named Curtains2 if the player is carrying the nugget and to the room named MissingTreasures otherwise. When you are testing your code for locked and forced passages, you might want to pay particular attention to the last eight rooms in the CrowtherRooms.txt file. These rooms, all of which have no lines at all in their long description, implement the shimmering curtain that marks the end of the game.
You should notice that the rooms with forced motions do not supply a meaningful short description, although the data files use a single hyphen so that the code to read the file still works. Forced motion should always display the long description. Also, be aware that sometimes several forced rooms can be chained together, so it is not enough to just check for a single forced room at a time.
Once you have this working, congratulations! You have a working Adventure game! While you may be sick of wandering around the rooms at this point, I highly suggest you put in some time to play the "Crowther" prefix of the game and try to beat it. Doing so will often reveal several issues that you thought were fine but show up in certain circumstances.