Answer to Exercise 58

| ANSWER TO EXERCISE 58
| Something to note here is that the button can't be called just "coffee'' when the player's holding a cup of coffee: this means the game responds sensibly to the sequence "press coffee'' and "drink coffee''. Also note the way `itobj` is set to the delivered drink, so that "drink it'' works nicely.
    Object -> drinksmat "drinks machine",
      with name "drinks" "machine",
           initial
              "A drinks machine here has buttons for Cola, Coffee and Tea.",
      has  static;
    Object -> thebutton "drinks machine button"
      has  scenery
     with  parse_name
           [ i flag type;
                for (: flag == 0: i++)
                {   flag = 1;
                    switch(NextWord())
                    {   'button', 'for': flag = 0;
                        'coffee': if (type == 0) { flag = 0; type = 1; }
                        'tea':    if (type == 0) { flag = 0; type = 2; }
                        'cola':   if (type == 0) { flag = 0; type = 3; }
                    }
                }
                if (type==drink.number && i==2 && type~=0 && drink in player)
                    return 0;
                self.number=type; return i-1;
            ],
            number 0,
            before
            [; Push, SwitchOn:
                 if (self.number == 0)
                    "You'll have to say which button to press.";
                 if (parent(drink) ~= 0) "The machine's broken down.";
                 drink.number = self.number; move drink to player; itobj = drink;
                 print_ret "Whirr!  The machine puts ", (a) drink, " into your \
                     glad hands.";
               Attack: "The machine shudders and squirts cola at you.";
               Drink:  "You can't drink until you've worked the machine.";
            ];
    Object  drink "drink"
      with  parse_name
            [ i flag type;
                for (: flag == 0: i++)
                {   flag = 1;
                    switch(NextWord())
                    {   'drink', 'cup', 'of': flag = 0;
                        'coffee': if (type == 0) { flag = 0; type = 1; }
                        'tea':    if (type == 0) { flag = 0; type = 2; }
                        'cola':   if (type == 0) { flag = 0; type = 3; }
                    }
                }
                if (type ~= 0 && type ~= self.number) return 0;
                return i-1;
            ],
            short_name
            [;  print "cup of ";
                switch (self.number)
                { 1: print "coffee"; 2: print "tea"; 3: print "cola"; }
                rtrue;
            ],
            number 0,
            before
            [; Drink: remove self;
                "Ugh, that was awful.  You crumple the cup and responsibly \
                 dispose of it.";
            ];

Back to the exercise in section 24

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