??ANSWER TO EXERCISE 66

The scheme will work like this: any room that ought to have a name should have a place_name property set to a dictionary word; say, the Bedquilt cave could be called 'bedquilt'. Clearly you should only be allowed to type this from adjacent rooms. So we'll implement the following: you can only move by name to those rooms listed in the current room's to_places property. For instance, the Soft Room might have to_places set to

to_places Bedquilt Slab_Room Twopit_Room;
Now the code: if the player's verb is not otherwise understood, we'll check it to see if it's a place name of a nearby room, and if so store that room's object number in goto_room, converting the verb to 'go#room' (which we'll deal with below).
Global goto_room;
[ UnknownVerb word p i;
    p = location.&to_places; if (p==0) rfalse;
    for (i=0:(2*i)<location.#to_places:i++)
        if (word==(p-->i).place_name)
        {   goto_room = p-->i; return 'go#room';
        }
    rfalse;
];
[ PrintVerb word;
    if (word=='go#room')
    {   print "go to ", (name) goto_room; rtrue; }
    rfalse;
];
(The supplied PrintVerb is icing on the cake: so the parser can say something like "I only understood you as far as wanting to go to Bedquilt.'' in reply to, say, "bedquilt the nugget''.) It remains only to create the dummy verb:
[ GoRoomSub;
    if (goto_room hasnt visited) "But you have never been there.";
    PlayerTo(goto_room);
];
Verb "go#room"  *                                -> GoRoom;
Note that if you don't know the way, you can't go there! A purist might prefer instead to not recognise the name of an unvisited room, back at the UnknownVerb stage, to avoid the player being able to deduce names of nearby rooms from this 'error message'.}


Back to the exercise in section 26
Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.