Hello All. This is my first topic here and I thought that I would share a piece of code that simulates a Coke machine that dispenses ice cold soft drinks. This code can be adapted for other machines such as a chocolate bar machine, etc. This came about after I saw a portion of code about a Chocolate Machine somewhere (I can’t remember the author’s name). The code works in Inform 7 build 6M62.
A machine is a kind of device. It is fixed in place. Carry out switching on a machine: now the noun is switched on. Carry out switching off a machine: now the noun is switched off. A thing can be drinkable. A thing is usually not drinkable. Check drinking: if the noun is not drinkable: say “[The noun] [are] not drinkable.”; stop. Mess Hall is a room. Understand "push [something switched off]" as switching on. Understand "push [something switched on]" as switching off. [This prevents the parser from printing Can't see any such thing when no more soda pops.] Rule for printing a parser error when the latest parser error is can't see any such thing error: if the player's command includes "drink": say "You do not have anything to drink."; otherwise: say "You can't see any such thing." The Coke Machine is a machine in the Mess Hall. "A Sony 2000 Coke Machine is against the wall. A white button dispenses ice cold soda pops from the machine." The description of the Coke Machine is "A Sony 2000 Coke Machine that dispenses ice cold soda pops.". A container called the hopper is part of the Coke Machine. The white button is part of the Coke Machine. The white button can be switched on or switched off. A soda pop is a kind of thing. A soda pop is drinkable. 3 soda pops are in hopper. The description of a soda pop is "A refreshing soda pop.". Understand "soda" or "pop" as soda pop. Instead of switching on the white button: let chosen pop be a random soda pop in hopper; if chosen pop is nothing: [That is, there were no pops remaining] say "The coke machine is empty."; otherwise: move the chosen pop to the player; say "A soda pop drops down to the machine's dispensing tray. You bend over and retrieve the soda pop." Instead of drinking a soda pop (called the drink): if the player carries the drink: now the drink is nowhere; say “You slurp down a [noun]. Refreshing!”; otherwise: say "You do not have anything to drink.";
That is all. I hope this code will help someone out there.