1
<HTML><HEAD><TITLE>Answer to Exercise 66</TITLE></HEAD>
2
<BODY BGCOLOR="#FFFFFF">
3
<TABLE><TR><TD Valign="top"><IMG SRC="../icons/exercise.gif" ALT="??"><TD bgcolor="#FAA89B"><B>ANSWER TO EXERCISE 66</B><TR><TD><TD>
7
The scheme will work like this: any room that ought to have a name
8
should have a <TT>place_name</TT> property set to a dictionary word;
9
say, the Bedquilt cave could be called <TT>'bedquilt'</TT>. Clearly
10
you should only be allowed to type this from adjacent rooms.
11
So we'll implement the following: you can only move by name to
12
those rooms listed in the current room's <TT>to_places</TT> property.
13
For instance, the Soft Room might have <TT>to_places</TT> set to
15
to_places Bedquilt Slab_Room Twopit_Room;
18
Now the code: if the player's verb is not otherwise understood,
19
we'll check it to see if it's a place name of a nearby room,
20
and if so store that room's object number in <TT>goto_room</TT>, converting
21
the verb to <TT>'go#room'</TT> (which we'll deal with below).
24
[ UnknownVerb word p i;
25
p = location.&to_places; if (p==0) rfalse;
26
for (i=0:(2*i)<location.#to_places:i++)
27
if (word==(p-->i).place_name)
28
{ goto_room = p-->i; return 'go#room';
34
{ print "go to ", (name) goto_room; rtrue; }
39
(The supplied <TT>PrintVerb</TT> is icing on the cake: so the parser can
40
say something like "I only understood you as far as wanting to go to
41
Bedquilt.'' in reply to, say, "bedquilt the nugget''.) It remains
42
only to create the dummy verb:
45
if (goto_room hasnt visited) "But you have never been there.";
48
Verb "go#room" * -> GoRoom;
51
Note that if you don't know the way, you can't go there! A purist might
52
prefer instead to not recognise the name of an unvisited room, back at
53
the <TT>UnknownVerb</TT> stage, to avoid the player being able to deduce names
54
of nearby rooms from this 'error message'.}
57
<HR>Back to <A HREF="../section26.html#ex66">the exercise in section 26</A><HR>
58
<SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>