Casino Lucky Aces Slot Machine

(Gilles Guillemette) #1

Good morning all. I just wrote a functioning Lucky Aces slot machine that I plan on using in my game casinos, I was wondering if there was a way to make the code more efficient and more compact specially in the calculate winnings part.

Price is a kind of value. $10.99 specifies a price with parts dollars and cents.
A thing has a price. The price of a thing is usually $0.00.

vWheel1 is text variable.
vWheel2 is text variable.
vWheel3 is text variable.
vWheel4 is text variable.

vButtonColor is text variable.
vCustomerBet is a price that varies. vCustomerBet is $0.00.

Casino is a room. "You see a Lucky Aces slot machine here."

When play begins:
	seed the random-number generator with a random number between 1 and 9999;

The player carries a wallet.
The wallet contains money. The price of the money is $500.00.
The printed name of the money is "[price of the money]".
Understand "cash" as the money.

The Lucky Aces Slot Machine is a backdrop. It is in Casino. The description of the slot machine is "This one-armed bandit allows the player to bet money for a chance to win a big jackpot. There are five buttons on the front:[paragraph break]White Button - Bet $1.00[line break]Yellow Button - Bet $10.00[line break]Blue Button - Bet $20.00[line break]Brown button - Bet $50.00[line break]Black Button - Bet $100.00[paragraph break]Just press a color button to place your bet and spin the wheels.[line break]To win, you need:[line break]Four of a Kind (win 10 times your bet)[line break]Three of a Kind (win 5 times your bet)[line break]";

The white button is part of the Lucky Aces Slot Machine. The white button can be switched on or switched off.
The yellow button is part of the Lucky Aces Slot Machine.  The yellow button can be switched on or switched off.
The blue button is part of the Lucky Aces Slot Machine.  The blue button can be switched on or switched off.
The brown button is part of the Lucky Aces Slot Machine.  The brown button can be switched on or switched off.
The black button is part of the Lucky Aces Slot Machine.  The black button can be switched on or switched off.

Understand "press [something switched off]" as switching on.
Understand "press [something switched on]" as switching off.

Instead of switching on the white button: now vCustomerBet is $1.00; now vButtonColor is "white"; set the bet.
Instead of switching on the yellow button: now vCustomerBet is $10.00; now vButtonColor is "yellow"; set the bet.
Instead of switching on the blue button: now vCustomerBet is $20.00; now vButtonColor is "blue"; set the bet.
Instead of switching on the brown button: now vCustomerBet is $50.00; now vButtonColor is "brown"; set the bet.
Instead of switching on the black button: now vCustomerBet is $100.00; now vButtonColor is "black"; set the bet.

To set the bet:
	if price of the money is less than vCustomerBet:
		say "You do not have enough money to cover the [vCustomerBet] bet.[line break]";
	else:
		say "You bet [vCustomerBet], push the [vButtonColor] button and watch the spinning wheels.[line break]";
		decrease the price of the money by vCustomerBet;
		spin the wheels;
		calculate winnings;

To spin the wheels:
	now vWheel1 is the substituted form of "[one of]Hearts[or]Diamonds[or]Clubs[or]Spades[purely at random]";
	now vWheel2 is the substituted form of "[one of]Hearts[or]Diamonds[or]Clubs[or]Spades[purely at random]";
	now vWheel3 is the substituted form of "[one of]Hearts[or]Diamonds[or]Clubs[or]Spades[purely at random]";
	now vWheel4 is the substituted form of "[one of]Hearts[or]Diamonds[or]Clubs[or]Spades[purely at random]";
	say "The result: [vWheel1] - [vWheel2] - [vWheel3] - [vWheel4][line break]";

To calculate winnings:
	say "Calculating winnings and losses...[line break]";
	if "[vWheel1]" exactly matches the text "[vWheel2]" and "[vWheel1]" exactly matches the text "[vWheel3]" and "[vWheel1]" exactly matches the text "[vWheel4]":
		say "FOUR OF A KIND! You win 10 times your bet...[line break]";
		repeat with x running from 1 to 11: [10 times plus recover amount of bet = 11]
			increase the price of the money by the vCustomerBet;
	else if "[vWheel1]" exactly matches the text "[vWheel2]" and "[vWheel1]" exactly matches the text "[vWheel3]":
		say "THREE OF A KIND! You win 5 times your bet...[line break]";
		repeat with x running from 1 to 6: [5 times plus recover amount of bet = 6]
			increase the price of the money by the vCustomerBet;
	else if "[vWheel1]" exactly matches the text "[vWheel3]" and "[vWheel1]" exactly matches the text "[vWheel4]":
		say "THREE OF A KIND! You win 5 times your bet...[line break]";
		repeat with x running from 1 to 6: [5 times plus recover amount of bet = 6]
			increase the price of the money by the vCustomerBet;
	else if "[vWheel2]" exactly matches the text "[vWheel3]" and "[vWheel2]" exactly matches the text "[vWheel4]":
		say "THREE OF A KIND! You win 5 times your bet...[line break]";
		repeat with x running from 1 to 6: [5 times plus recover amount of bet = 6]
			increase the price of the money by the vCustomerBet;
	else:
		say "You lose your bet of [vCustomerBet].[line break]";
	say "You now have [price of the money].[line break]";

Cheers.

1 Like
(matt w) #2

Cool!

There’s a bunch of things here that I would do using kinds. Definitely making the suits a kind of value instead of using text matching would be a good idea. And I’d make the buttons and wheels into kinds, and then you can associate the price of the button with the button itself instead of hand-typing it in everywhere you need to use it.

Price is a kind of value. $10.99 specifies a price with parts dollars and cents.
A thing has a price. The price of a thing is usually $0.00.

A suit is a kind of value. The suits are Hearts, Clubs, Diamonds, and Spades.

A color is a kind of value. The colors are white, yellow, blue, brown, and black.

A button is a kind of device. [automatically means it can be switched on or switched off] A button has a color. A button has a price. Understand the color property as referring to a button. The printed name of a button is usually "[color] button".

A wheel is a kind of thing. A wheel has a suit. The description of a wheel is usually "It displays [suit]."

The Lucky Aces Slot Machine is a backdrop. It is in Casino. 

The first wheel, the second wheel, the third wheel, and the fourth wheel are wheels. The first wheel, the second wheel, the third wheel, and the fourth wheel are part of the Lucky Aces Slot Machine. 

The description of the slot machine is "This one-armed bandit allows the player to bet money for a chance to win a big jackpot. There are five buttons on the front:[paragraph break]White Button - Bet $1.00[line break]Yellow Button - Bet $10.00[line break]Blue Button - Bet $20.00[line break]Brown button - Bet $50.00[line break]Black Button - Bet $100.00[paragraph break]Just press a color button to place your bet and spin the wheels.[line break]To win, you need:[line break]Four of a Kind (win 10 times your bet)[line break]Three of a Kind (win 5 times your bet)[line break]The wheels of the slot machine display [wheel display].";

To say wheel display:
	say "[suit of the first wheel] - [suit of the second wheel] - [suit of the third wheel] - [suit of the fourth wheel]".

One white button is part of the Lucky Aces Slot Machine. The price is $1.00.
One yellow button is part of the Lucky Aces Slot Machine. The price is $10.00.
One blue button is part of the Lucky Aces Slot Machine. The price is $20.00.
One brown button is part of the Lucky Aces Slot Machine. The price is $50.00.
One black button is part of the Lucky Aces Slot Machine. The price is $100.00.

vButtonColor is a color that varies.
vCustomerBet is a price that varies. vCustomerBet is $0.00.

Casino is a room. "You see a Lucky Aces slot machine here."

When play begins:
	seed the random-number generator with a random number between 1 and 9999;

The player carries a wallet.
The wallet contains money. The price of the money is $100.00.
The printed name of the money is "[price of the money]".
Understand "cash" as the money.

Instead of pushing a switched on button: try switching off the noun.
Instead of pushing a switched off button: try switching on the noun. [by redirecting the action instead of relying on the "press" command, we get "push" too.]

Instead of switching on a button (called the play): [since this is an "instead" rule, the button never gets switched on--this is also true in your original code and is probably fine]
	if price of the money is less than the price of the play:
		say "You do not have enough money to cover the [price of the play] bet." instead;
	otherwise:
		now vCustomerBet is the price of the play;
		now vButtonColor is the color of the play;
		decrease the price of the money by vCustomerBet;
		say "You bet [vCustomerBet], push the [vButtonColor] button and watch the spinning wheels.";
		spin the wheels;
		calculate winnings. 
		
To spin the wheels:
	repeat with spinner running through wheels that are part of the Lucky Aces Slot Machine:
		now the suit of spinner is a random suit;
	say "The result: [wheel display]."
	

To calculate winnings:
	say "Calculating winnings and losses...[line break]";
	let won be a truth state;
	now won is false; [probably not necessary because it's the default variable]
	repeat with checked suit running through suits:
		if four wheels display the checked suit: [there may be another way to do this than by defining a custom relation, but custom relation is what I could think of]
			say "FOUR OF A KIND! You win 10 times your bet...[line break]";
			increase the price of the money by (eleven times the vCustomerBet);
			now won is true;
			break; [ends the repeat through suits]
		otherwise if three wheels display the checked suit:
			say "THREE OF A KIND! You win 5 times your bet...[line break]";
			increase the price of the money by (six times the vCustomerBet);
			now won is true;
			break; 
	if won is false:
		say "You lose your bet of [vCustomerBet].[line break]";
	say "You now have [price of the money].[line break]";
	
Displaying relates a wheel (called X) to a suit (called Y) when Y is the suit of X. The verb to display means the displaying relation. [There's probably a more elegant way to do this than defining a new relation but I'm not sure how.]

Also it’s probably not important but I think the casino is going to go broke–if I’m not mistaken the player will on balance win money. (The chance of the first three wheels displaying spades while the fourth wheel displays something else is 3/256. Same for any of the other wheels being the odd wheel out, or for any other suit being the one that matches–that’s 16 total combinations so there’s a 3/16 chance of winning five times your bet. Then there’s a 1/64 chance of winning ten times your bet. Adding up the player averages winning nine cents for every dollar they bet.)

FWIW I changed the player’s starting bankroll to $100.00 because the z-machine didn’t like an initial value of $500.00 and the version of Inform I currently have installed has one of those CocoaGlk bugs that makes it impossible to test Glulx games in the IDE! I should really fix that.

1 Like
(Daniel Stelzer) #3

Yep, that’s an unfortunate limitation of the Z-machine. Internally, $500.00 is stored as 50,000, and the highest number the Z-machine can handle is 2^(16-1)-1, which is 32,767.

Glulx, on the other hand, can handle values up to 2^(32-1)-1, which is 2,147,483,647 (or $21,474,836.47)—and it can go even higher if you’re willing to sacrifice some precision (using floating point). So you should have no issues there.

1 Like
(Gilles Guillemette) #4

Wow Matt, you are an encyclopedia of Inform 7 knowledge. Your code is fluid while mine was somewhat clunky.

Anyways, since you lowered the player’s bankroll, does that mean if the player makes $5000.00 while working that he may no longer play the slot machine at the casino? (I think this may be a stupid question but I need to ask).

Thanks for your revised code.

Cheers.

1 Like
(matt w) #5

Thanks! The main thing here is that if you’ve got a bunch of similar things, it’s often good to turn them into kinds (or values). Also I find that turning something into a relation often helps with writing clauses like “if four wheels display checked suit”–there may be a way of doing that directly with “if four wheels have suit checked suit” or something but I’m not sure. (Oh! If you have wheels that are not part of this particular machine, you’ll need to restrict the check to the wheels that are part of the machine.)

As far as exceeding the cash limit, as Draconis said that’s only an issue if you’re compiling to z-code! Since your initial code compiled, I assume you’re compiling to Glulx, and you don’t have to worry about this unless the player gets really filthy rich. This is something that only arises for me because my Inform setup is out of date and I can’t use Glulx right now.

(But FWIW, what happens is that the values wrap around to negative numbers. “You now have $-324.0-36.”)

(matt w) #6

Reporting that I was able to start from $100 and overflow the z-machine maxint (of $327). Eventually. Slot machines are addictive.

(Piergiorgio d'errico) #7

During the 8 bit era, I sometimes used two variables, unit and fraction, allowing counting up to 32767.9999

Similiarily, I used (back then in the 80s,in Italy even household acc’t was in large numbers) a sort of four-digit BCD math.

But, a betting game can be considered a Z/glulx machine abuse, so putting things back in context, if a player needs betting, should be toward a definite objective, for example, figuring how the house cheats, unconspiciously watching someone/something, or, more pertinent, raising money for some objective, and in this case, I don’t think there’s the need of more than 32767 unit of whatever is the currency or fraction thereof…

(Daniel Stelzer) #8

That would work nicely; unfortunately the Z-machine doesn’t natively support any type larger than sixteen bits (no structs, no longs), so keeping the two variables together is a pain. Inform also has some nice arbitrary-base fixed-point conveniences, so if we could manipulate 32-bit quantities in any elegant way it would raise the limit to $21,474,836.47, just like on Glulx.

But, adding 32-bit support to the Z-machine at this time seems a bit pointless, seeing as Glulx was designed to be “like the Z-machine but 32-bit and without all the cruft of the fixed memory map”.