Answer to Exercise 25

| ANSWER TO EXERCISE 25
    [ PrintTime x; print (x/60), ":", (x%60)/10, (x%60)%10; ];
    Object -> alarm_clock "alarm clock"
      with name "alarm" "clock",
           number 480,
           description
           [;  print "The alarm is ";
               if (self has general) print "on, "; else print "off, but ";
               "the clock reads ", (PrintTime) the_time,
               " and the alarm is set for ", (PrintTime) self.number, ".";
           ],
           react_after
           [;  Inv:  if (self in player)   { new_line; <<Examine self>>; }
               Look: if (self in location) { new_line; <<Examine self>>; }
           ],
           daemon
           [;  if (the_time >= self.number && the_time <= self.number+3
                   && self has general) "^Beep! Beep! The alarm goes off.";
           ],
           grammar [; return 'alarm,'; ],
           orders
           [;  SwitchOn:  give self general; StartDaemon(self); "~Alarm set.~";
               SwitchOff: give self ~general; StopDaemon(self); "~Alarm off.~";
               SetTo:     self.number=noun; <<Examine self>>;
               default: "~Commands are on, off or a time of day only, pliz.~";
           ],
           life
           [;  Ask, Answer, Tell:
                  "[Try ~clock, something~ to address the clock.]";
           ],
      has  talkable;
and add a new verb to the grammar:
    Verb "alarm," * "on"      -> SwitchOn
                  * "off"     -> SwitchOff
                  * TimeOfDay -> SetTo;
(using the
`TimeOfDay`
token from the exercises of Section 27). Note that since the word "alarm,'' can't be matched by anything the player types, this verb is concealed from ordinary grammar. The orders we produce here are not used in the ordinary way (for instance, the action `SwitchOn` with no `noun` or `second` would never ordinarily be produced by the parser) but this doesn't matter: it only matters that the grammar and the `orders` property agree with each other.

Back to the exercise in section 16

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