...

/

Programming a Slot Machine

Programming a Slot Machine

Let's practice what we have learned so far and implement a simple game.

We’ll practice a bit, sum up everything we’ve learned so far, and implement a simple one-armed bandit game. We will put some money in the bank, pull the handle, and see what happens.

Outline

Let’s outline our plan. Variable balance will represent the amount of money we have in the bank. We’ll have three slot reels and a few images. Traditionally, slot machines have images of fruits, a cherry, a bell, and the number 7. To simplify our task, we’ll use the numbers from 0 to 5 instead of the images mentioned above. We can always revisit our game and make changes to it.

Variables x, y, and z will represent the reels. Each reel can hold only one value, from 0 to 5, at a time. A random number generator will define this value.

Win or lose

The next step is to define what “win” and “lose” mean. When all ...