1
<HTML><HEAD><TITLE>Answer to Exercise 25</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 25</B><TR><TD><TD>
7
[ PrintTime x; print (x/60), ":", (x%60)/10, (x%60)%10; ];
8
Object -> alarm_clock "alarm clock"
9
with name "alarm" "clock",
12
[; print "The alarm is ";
13
if (self has general) print "on, "; else print "off, but ";
14
"the clock reads ", (PrintTime) the_time,
15
" and the alarm is set for ", (PrintTime) self.number, ".";
18
[; Inv: if (self in player) { new_line; <<Examine self>>; }
19
Look: if (self in location) { new_line; <<Examine self>>; }
22
[; if (the_time >= self.number && the_time <= self.number+3
23
&& self has general) "^Beep! Beep! The alarm goes off.";
25
grammar [; return 'alarm,'; ],
27
[; SwitchOn: give self general; StartDaemon(self); "~Alarm set.~";
28
SwitchOff: give self ~general; StopDaemon(self); "~Alarm off.~";
29
SetTo: self.number=noun; <<Examine self>>;
30
default: "~Commands are on, off or a time of day only, pliz.~";
34
"[Try ~clock, something~ to address the clock.]";
39
and add a new verb to the grammar:
41
Verb "alarm," * "on" -> SwitchOn
42
* "off" -> SwitchOff
43
* TimeOfDay -> SetTo;
46
(using the <TABLE Border><TR><TD><TT>TimeOfDay</TT></TABLE> token from the exercises of <A HREF="section27.html">Section 27</A>). Note
47
that since the word "alarm,'' can't be matched by anything the player types,
48
this verb is concealed from ordinary grammar. The orders we produce here
49
are not used in the ordinary way (for instance, the action <TT>SwitchOn</TT> with
50
no <TT>noun</TT> or <TT>second</TT> would never ordinarily be produced by the parser) but this
51
doesn't matter: it only matters that the grammar and the <TT>orders</TT> property
52
agree with each other.
54
<HR>Back to <A HREF="../section16.html#ex25">the exercise in section 16</A><HR>
55
<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>