??ANSWER TO EXERCISE 34

This is a crude implementation, for brevity (the real Zork thief has an enormous stock of attached messages). A life routine is omitted, and of course this particular thief steals nothing. See 'The Thief' for a much fuller, annotated implementation.

Object -> thief "thief"
  with name "thief" "gentleman" "mahu" "modo",
       each_turn "^The thief growls menacingly.",
       daemon
       [ i p j n k;
           if (random(3)~=1) rfalse;
           p=parent(thief);
           objectloop (i in compass)
           {   j=p.(i.door_dir);
               if (j ofclass Object && j hasnt door) n++;
           }
           if (n==0) rfalse;
           k=random(n); n=0;
           objectloop (i in compass)
           {   j=p.(i.door_dir);
               if (j ofclass Object && j hasnt door) n++;
               if (n==k)
               {   move self to j;
                   if (p==location) "^The thief stalks away!";
                   if (j==location) "^The thief stalks in!";
                   rfalse;
               }
           }
       ],
  has  animate;
(Not forgetting to StartDaemon(thief) at some point, for instance in the game's Initialise routine.) So the thief walks at random but never via doors, bridges and the like (because these may be locked or have rules attached); it's only a first approximation, and in a good game one should occasionally see the thief do something surprising, such as open a secret door. As for the name, note that 'The Prince of darkness is a gentleman. Modo he's called, and Mahu' (William Shakespeare, King Lear III iv).

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