Answer to Exercise 87

| ANSWER TO EXERCISE 87
| Firstly, create an attribute `is_key` and give it to all the keys in the game. Then:
    Global assumed_key;
    [ DefaultLockSub;
      print "(with ", (the) assumed_key, ")^"; <<Lock noun assumed_key>>;
    ];
    [ DefaultLockTest i count;
      if (noun hasnt lockable) rfalse;
      objectloop (i in player)
          if (i has is_key) { count++; assumed_key = i; }
      if (count==1) rtrue; rfalse;
    ];
    Extend "lock" first * noun = DefaultLockTest -> DefaultLock;
(and similar code for "unlock''). Note that "lock strongbox'' is matched by this new grammar line only if the player only has one key: the `DefaultLock strongbox` action is generated: which is converted to, say, `Lock strongbox brass_key`.

Back to the exercise in section 29

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