For this assignment, you will simulate a slots game. If you are not familiar with the game, a slot machine contains three windows or slots in which an image is shown. To play the game, the player inserts a token and pulls on a handle, causing the images in the slots to be randomized (usually by spinning the wheels on which the images are printed). If the three resulting images in the slots are identical, say three lemons, then the player wins and receives some payoff.
The SlotMachine class, which has been provided for you,
does not utilize graphics, but instead represents the result of
a spin with text. The
spin method returns a String that describes the spin
and whether it was a win or a loss. For example,
a call to
spin might return
"cherry-bar-cherry: YOU LOSE!"
"lemon-lemon-lemon: YOU WIN!".
EXERCISE 1: Load this class into the BlueJ IDE and create a SlotMachine object. Call the spin method 5 times and write the returned values below. Were any of your spins winners? Would you expect them to be? Explain.
EXERCISE 2: Currently, the
SlotMachineclass has a private field,
numSpins, which keeps track of the number of times
spinshas been called. Define an accessor method named
getNumberOfSpinsthat returns that number. That is, a call to your
getNumberOfSpinsshould return the value of the
numSpinsfield (i.e., number of times that
spinhas been called). Be sure to include reasonable comments, so that your method is properly documented in the Interface view of BlueJ. Test your method to make sure that it behaves as desired.
SlotMachine class allows us to model a simple slot machine
and perform repeated spins, it lacks many essential features of a real slot machine.
Most notably, it doesn't support wagering that results in winning or losing tokens.
In order to support wagering, the
SlotMachine object must keep track of the
player's current bankroll, subtracting some number of tokens for each spin and awarding
tokens for a win. For our simple machine, we will assume that each spin costs
1 token. If the resulting spin is a winner, then the player is awarded 9 tokens.
Thus, a winning spin results in a net gain of 8 tokens.
EXERCISE 3: Modify the SlotMachine class so that it maintains the customer's bankroll and updates it as spins are made. In particular, you will need to make the following additions/changes:
- Add an integer field to the class, which keeps track of the player's bankroll (i.e., number of tokens remaining).
- Add a mutator method named
addToBankrollthat allows the player to add more tokens to their bankroll. The method should have one parameter, the number of tokens to be added, and result in that amount being added to the bankroll field.
- Add an accessor method named
getBankrollthat returns the current value of the bankroll field.
- Modify the
spinmethod so that it correctly updates the bankroll field on each spin. Every spins should cost the player 1 token. If the resulting spin is a winner, then 9 tokens should be awarded.
Be sure to test your modifications carefully by creating objects and inspecting their state before and after method calls.
EXERCISE 4: Perform 5 different simulations with your modified slot machine. For each simulation, start with a bankroll of 10 tokens, and repeatedly spin until you either reach 30 tokens or else run out of money. For each simulation, list the final bankroll (either 0 or 30) and the number of spins it took to reach that bankroll. Are these results what you expected? Explain.
EXERCISE 5: What happens if the player runs out of money but continues playing? That is, if the bankroll is 0 and
spinis called, will an error occur? What happens to the bankroll? If it becomes negative, is it possible for it ever to become positive again? Explain your answers.
While your modifed
SlotMachine class can now support wagering,
it is not very robust in its behavior. For instance, there is nothing to stop the player
from adding a negative amount to the bankroll. More alarmingly,
there is nothing to stop the player from continuing to play even though
they are out of money.
EXERCISE 6: In order to make the
SlotMachineclass more robust, make the following changes:
- Insert an if statement into your
addToBankrollmethod so that it checks to make sure that the amount to be added is positive. If it is, then that amount should be added to the bankroll field as before. If not, the bankroll field should be left unchanged.
- Insert an if statement into your
spinmethod so that it checks to make sure that the bankroll is positive. If it is, then the spins should be made and the bankroll updated as before. If not, no spin should occur and the message
"SORRY - YOU ARE OUT OF TOKENS."should simply be returned by the method.
Be sure to test your modifications carefully to make sure they behave as desired.
Submit your modified
SlotMachine.javafile, along with your answers to the exercises, via the Digital Dropbox.