CS 2073
Assignment 5
Due Monday, Mar 26
Slot Machine
Purpose
This assignment offers practice in writing functions and in using a
random number generator (RNG).
Description
You are to simulate a slot machine game using the RNG.
As the picture shows, the slot machine has 3 windows or slots.
After pulling the lever, each window displays one of the following fruits:
| BANANA | ORANGE | CHERRY | LEMON | GRAPE | WILD |
To win the game, all three fruits must match. WILD matches all other fruits.
Examples winners are
| CHERRY | CHERRY | CHERRY
|
| WILD | CHERRY | CHERRY
|
| WILD | WILD | CHERRY
|
| WILD | WILD | WILD
|
Required Functions
int slot (nbr_fruits);
int rand_int (int a, int b);
int won_game (int fruit1, int fruit2, int fruit3);
slot() returns the displayed fruit for one slot window:
1=BANANA, 2=ORANGE, 3=CHERRY, etc.
The parameter nbr_fruits is the number of possible fruits.
Although you know in advance that nbr_fruits=6,
do not hard code this number. The function must handle any number
of fruits.
Use a #define statement to specify number of possible fruits
and pass this value when calling slot() from main().
rand_int() returns a random interger in the range [a,b]
(see Etter & Ingber, page 179).
Note that slot() calls rand_int() to select which
fruit to display.
won_game() returns 1 if the slot game was won, and 0 if lost.
Input from keyboard
Output to console window
- results of the first 5 games (which three fruits were displayed?)
- number games played
- number games won
- winning percentage
Tests to turn in
- 10 games
- 1000 games
- 1000000 games (notice your counters must be type
double)
Code skeleton
A skeleton of the code is available at
slot-skeleton.c.
Reminder
Follow directions on the handout
General Rules for Programming Assignments.