Coke Machine code - Revised to Version 2

(Gilles Guillemette) #1

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.
Enjoy!

3 Likes

(Marshal Tenner Winter) #2

So…the player drinks the can as well? :crazy_face:

0 Likes

(Gilles Guillemette) #3

Nope, the player eats the can. I did not separate the liquid from the can container but this is a valid point. Can you modify the code to include empty cans? Thanks for your insight.

0 Likes

(Marshal Tenner Winter) #4

Yeah, it would be a cinch to code the can but you’d have to be aware of how many cans can exist in the game and how you do that.
In my experience, when I had some sort of vending-device, I would somehow make it so that the player can only gain one item at a time. So if a player gets a can of soda, he can drink it, leaving him with an empty can and so long as that empty can is in the world, the vending machine will cease working (or whatever fits the story).

0 Likes

(matt w) #5

If you don’t need the empty can, a simple thing to do is just include something like “You slurp down [a noun], crush the can, and toss it aside.” Or just don’t mention the empty can and the player probably won’t notice it!

BTW it can be good to put “[a noun]” rather than “a [noun]” as this lets Inform do its automatic article handling, in case the noun is proper named or has a funky custom article. Or if you have Orange Crush this will get you “an Orange Crush” rather than “a Orange Crush.”

6 Likes

(Gilles Guillemette) #6

Hello again. Thank you all for your comments. After reading them, I decided to modify the code and make it more generic.

The coke machine is gone and replaced with a vending machine that can dispense anything provided it has a different colour button for each item.

The coke machine device has been replaced with a Vending Machine container (got rid of the hopper) so all items are placed directly inside the vending machine. What makes it work is that each button can be switched on and switched off.

Finally the vending machine can dispense foods, liquids, weapons, medpacks, potions, animals, etc. It is left to your imagination.

Oh and about the soda pop cans, they are now disposed of in the recycling bin.

Let me know if you would like me to post my revised code.

3 Likes

(matt w) #7

I would definitely like to see the new code!

0 Likes

(Gilles Guillemette) #8

Here is the revised code PART 1 of 2

[COKE MACHINE -> VENDING MACHINE Version 2.0]

A thing can be drinkable. A thing is usually not drinkable.
A thing can be edible. A thing is usually not edible.

Check drinking something which is not drinkable:
	say “[The noun] [are] not drinkable.”;
	stop.

Check eating something which is not edible:
	say “[The noun] [are] not edible.”;
	stop.

Definition: a container is empty if nothing is in it.

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 twinkies or coca-colas are in the player's inventory.]
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.";
	else if the player's command includes "eat":
		say "You do not have anything to eat.";
	otherwise:
		say "You can't see any such thing."

The Vending Machine is a container in the Mess Hall. "A Sony 2000 Vending Machine is against the wall.[paragraph break]White button: one coca-cola[line break]Brown button: one twinkie". The description of the Vending Machine is "A Sony 2000 Vending Machine that dispenses ice cold coca-colas and delicious twinkies. [if not empty]The vending machine is well stocked.[otherwise]The vending machine is empty.[end if][if not empty][paragraph break]White button: one coca-cola[line break]Brown button: one twinkie[end if]";

The white button is part of the Vending Machine. The white button can be switched on or switched off.
The brown button is part of the Vending Machine. The brown button can be switched on or switched off.

A coca-cola is a kind of thing. A coca-cola is drinkable. The description of a coca-cola is "A refreshing coca-cola drink."
A twinkie is a kind of thing. A twinkie is edible. The description of a twinkie is "A golden sponge cake with creamy filling."
2 Likes

(Gilles Guillemette) #9

Here is the revised code PART 2 of 2

In the Vending Machine are three coca-colas.
In the Vending Machine are three twinkies.

Understand "soda" or "cola" or "pop" as coca-cola.
Understand "golden" or "sponge" or "cake" as twinkie.

Instead of switching on the white button:
	let chosen cola be a random coca-cola in Vending Machine; 
	if chosen cola is nothing: [That is, there were no coca-colas remaining] 
		say "The vending machine is out of coca-colas."; 
	otherwise: 
		move the chosen cola to the player;
		say "A coca-cola drops down to the machine's dispensing tray. You bend over and retrieve the coca-cola."

Instead of switching on the brown button:
	let chosen twinkie be a random twinkie in Vending Machine; 
	if chosen twinkie is nothing: [That is, there were no twinkies remaining] 
		say "The vending machine is out of twinkies."; 
	otherwise: 
		move the chosen twinkie to the player;
		say "A twinkie drops down to the machine's dispensing tray. You bend over and retrieve the twinkie."

Instead of drinking a coca-cola (called the drink):
	if the player carries the drink:
		now the drink is nowhere;
		say “You slurp down [a noun] and put the empty can in the recycling bin. Refreshing!”;
	otherwise:
		say "You do not have any coca-colas to drink.";

Instead of eating a twinkie (called the cake):
	if the player carries the cake:
		now the cake is nowhere;
		say “You eat [a noun] and put the twinkie wrapper in the recycling bin. Delicious!”;
	otherwise:
		say "You do not have any twinkies to eat.";

Test me with "push white button / push brown button / x cola / x cake / inv / drink cola / eat cake / inv"
0 Likes