Lab 9: A Casino Simulation

Copyright RIT 2003
$Id: writeup.xml,v 1.6 2007/08/27 21:34:24 vcss231 Exp vcss231 $


Goal

The primary purpose of this lab is to introduce you to the software development process. In this lab you will be given the requirements, specifications, and high-level design for a simple program. You will work in a team to develop a program that satisfies the requirements and implements the design outlined in the lab. Your instructor will form the teams for this lab and will tell you which team you are on when you arrive for lab.


Team Setup

You are to work on this lab in teams of size 2. Students who do not abide by this rule will not get full points for in-class participation.

Overview

Objectives

Ask Dr. Tiger

Pre - Lab Work

  1. Review your class notes for the preceeding week

  2. Read the Program Requirements in Activity 1

  3. Understand the Program Specifications in Activity 2

  4. Read about creating random numbers in Java (RandomNumbers.html)

In-Lab Activities

This week's lab is structured a bit differently in that not all activities have deliverables. The first few are mostly reading. It is important that you go through this material before the actual start of lab.

Activity #1 : Discuss the program requirements with members of your team.

In this lab you are to write a program that simulates a slot machine found at a typical casino. A slot machine has a number of tumblers with pictures on them that appear in the windows on the front of the machine. The machine has a large arm, that when pulled down, starts the tumblers spinning. The goal of the game is to have a certain combination of pictures on the faces of the tumblers when they stop spinning. A picture of a typical slot machine is shown below:


Slot Machine

The reason you find slot machines in a casino is because people like to bet money that certain combinations of pictures will appear in the windows. The machine pays different amounts based on the combination of pictures that appear. A combination that pays 1:1 will pay you back what you bet, a combination that pays 10:1 will pay you 10 times the amount you bet. One combination pays the jackpot, which is a large amount of money.

The slot machines that you will simulate can have from 2 to 10 tumblers, and each tumbler has the digits '0' through '9' on its face (instead of pictures). The slot machine is connected electronically to a bank, which collects and distributes money for a single slot machine. The slot machine can deposit money into and withdraw money from the bank. (Note: Since this is a casino, and the casino owns the bank, the slot machine can withdraw more money than is in the bank.) In addition to the current balance, the bank also keeps track of the total deposits and withdrawals made by the slot machine. The number of tumblers on a given slot machine is determined when the machine is built. Once the slot machine has been built, the number of tumblers is fixed.

The combinations of numbers on the slot machine that will win money, as well as the payoff for each combination, are given in the table below (N refers to the number of tumblers on the machine):

Combination Pay-off

All numbers the same

*** JACKPOT ***

All Even Numbers

N:1

All Odd Numbers

N:1

Two Numbers the Same

1:1

In the event that more than one of the payoff conditions is met, the condition that gives the highest payback will be paid.

The minimal initial balance in the bank connected to a slot machine is based on the number of tumblers on the machine. The minimal initial balance is set to $50 for each tumbler on the machine, e.g., the initial minimal balance for a three tumbler machine is $150. At the time the simulation is started, the user will be able to specify an initial balance for the bank. If the user specifies a value below the minimum, the minimum initial balance will be used instead.

The jackpot paid by the machine is a percentage of the total money in the bank connected to the slot machine. The percentage is determined by the number of tumblers on the machine. The jackpot is 10% of the balance in the bank for every tumbler on the machine. So, if you hit the jackpot on a 3 tumbler machine you will get 30% of the bank's balance, on a 10 tumbler machine you get it all (100%). The jackpot is always rounded down to the nearest dollar, e.g., a jackpot of $150.99 becomes $150, and will never be less than $100.

When the casino simulation begins, the user will be able to specify the number of players using the machine, the number of tumblers on the machine, and the initial bank balance. The number of players must be greater than 0 and the number of tumblers must be between 2 and 10 inclusive. Your program will then simulate each player, in turn, playing the slot machine a random number of times. Each player will play all of their turns before the next player plays. A player will bet the same amount of money on each of their plays. A player will always bet a whole dollar amount between $1 and $10 inclusive. The amount of the bet will be determined using a random number generator. The number of times each player plays the slot machine will also be determined randomly. The number of plays will be between 10 and 100 inclusive.

For each player, your program will print out the number of times they played, the amount they bet each time, the total amount they bet, the total amount returned to the player by the slot machine, and the difference between the total amount bet and the total amount returned by the machine (their earnings). After all the players have finished, the program will print out the total number of times the slot machine was played, the total amount collected, the total amount paid (money returned to the players), and the amount of money earned by the machine, i.e., the difference between the amount collected and the amount paid. It will also report the number of times the jackpot, N:1, and 1:1 combinations occurred.

Activity #2 : Discuss the program specifications with members of your team.

Starting the Program

The program is started from the command line by typing the name of the program followed by the number of players, the number of tumblers on the slot machine being simulated, and the initial bank balance. The following command line will simulate five players playing a three tumbler slot machine with an initial bank balance of $500:


                                     java CasinoSimulation 5 3 500
      

If the program is not invoked correctly, the following error message will be printed and the program will be terminated without generating any other output:

Usage: CasinoSimulation <players> <tumblers> <balance>

If the program is invoked correctly the argument that specifies the number of players is processed next. If the number that represents the number of players is not a valid number or is less than one, the following error message will be printed and the program will be terminated without generating any other output:

CasinoSimulation: Invalid number of players

After processing the number of players, the program will process the number of tumblers. If the number of tumblers is not a valid number or is not in the range from 2 to 10 (inclusive), the following error message will be printed and the program will be terminated without generating any other output:

CasinoSimulation: Invalid number of tumblers

Finally the program will process the initial bank balance specified by the user. If the value entered is not a valid integer, the following message will be printed and the program will terminate without producing any additional output:

CasinoSimulation: Invalid initial balance

Output

For each player the program prints out the number of times they played, the amount that was bet each time, the total amount bet, the total amount returned to the player by the slot machine, and the difference between the total amount bet and the total amount returned by the machine (earnings). After all the players have finished, the program prints out the total number of times the slot machine was played, the total of the bets that were made, the total amount paid (money returned to the players), and the amount of money earned by the machine (which is the difference between the amount collected and the amount paid). The program also reports the number of times the jackpot, N:1, and 1:1 combinations occurred.

The output from the simulation program will be formatted as shown below (the output shown is for a three tumbler machine with 5 players):

Player 1: Playing 17 times betting $8 each time.
Total bet $136, Total returned $128, Earnings $-8

Player 2: Playing 93 times betting $9 each time.
Total bet $837, Total returned $902, Earnings $65

Player 3: Playing 49 times betting $5 each time.
Total bet $245, Total returned $240, Earnings $-5

Player 4: Playing 19 times betting $2 each time.
Total bet $38, Total returned $44, Earnings $6

Player 5: Playing 92 times betting $1 each time.
Total bet $92, Total returned $111, Earnings $19

Games played: 270
Bets made: $1348
Total paid: $1425
Total earnings: $-77

Jackpots: 3
3:1: 72
1:1: 39

Activity #3 : Study the program design

Overview

The casino simulation consists of 4 classes: Tumbler, Bank, SlotMachine, and CasinoSimulation. The UML diagram class diagram below, shows each of the classes and their relationships in this program.


CasinoSimulation Class Relationships

The CasinoSimulation class contains the main method that runs the simulation. This class is responsible for processing the command line arguments, instantiating the objects necessary to run the simulation, running the simulation, and printing the results. After processing the command line arguments and setting up the simulation, the main method enters a loop that simulates each player's interaction with the slot machine. When all of the players have finished playing with the machine, the final statistics are printed and the program terminates.

The Tumbler class represents a single tumbler in the simulation. A slot machine will instantiate one Tumbler object for each tumbler in the machine. A Tumbler object is spun using the spin() method. The result of the spin, i.e., the number of the face that is now in the window, can then be determined using the getFace() method.

The bank that the slot machine is connected to is an instance of the Bank class. A Bank object stores the curent balance and keeps track of the total deposits and withdrawals made to/from the bank. The Bank class provides constructors, accessors, modifiers, and methods to deposit and withdraw money into/out of the bank. In addition to updating the balances, the deposit() and withdraw() methods also update the total amount of deposits and withdrawals maintained by the class. The resetTotals() method can be used to reset the deposit and withdraw amounts back to zero.

The majority of the logic that makes this simulation work is contained in the class SlotMachine. This class is used to instantiate the slot machine that the players in this simulation will play with. In addition to providing methods that allow players to play the machine, a SlotMachine also maintains a number of statistics. These statistics provide information on the number of games played, and the number of times the machine had a jackpot, N:1, or even odds payoffs. During the construction process, the number of tumblers that are on the slot machine must be specified. A second constructor provides the ability to specify the initial balance in the bank that the slot machine will be connected to. The SlotMachine constructor must create the tumblers contained in the machine, create the bank (with the appropriate initial balance), and initialize any counters that must be maintained to provide staticstics.

The play() method in the SlotMachine class simulates one play of the slot machine. When the method is invoked it is passed the amount of the player's bet, which is immediately deposited into the bank. The machine then spins the tumblers and checks to see if any of the winning combinations appear on the faces of the tumblers. If one of the winning combinations appears on the tumblers, the amount of money that must be paid is calculated, withdrawn from the bank, and the statistics are updated.

The methods; getWinnings() and earningsReport() provide a way to examine the statistics maintained by a SlotMachine. Each slot machine keeps track of the total winnings paid by the machine. In order to provide the ability for the machine to keep track of the winnings for each player, this total is reset to zero every time it is read using getWinnings(). The method, getWinnings(), returns the current winnings maintained by the machine and resets the total winnings back to zero. The method, earningsReport() will print on standard output the following information in the format shown:

Games played: xxx
Bets made: $xxx
Total paid: $xxx
Total earnings: $xxx
	
Jackpots: xxx
x:1: xxx
1:1: xxx

where the xxx's will be replaced with the appropriate values.

Activity #4 : Get the files you need to complete this lab.

Since individual submissions are not allowed in this lab, you must decide from which account you and your partner will submit your work. Your lab instructor will tell you how to indicate the account from which your work will be submitted. Submissions from accounts other than the one you tell your instructor will be ignored.

The files that you need to complete this project are contained in the jar file located here (/~vcss231/pub/lab09/lab9.jar) . Download the jar file and unpack it in an appropriate location in your account.

The lab9 jar file contains two directories: classes, and code. The directory lab9/classes contains Java class files compiled from the working version of the program. Do not modify these class files. They have been included in the jar file so that you will always have working copies of the various classes that make up the casino simulation program. You can run the working version of the simulation by making lab9/classes your working directory, and typing the following command:


                           java CasinoSimulation
      

The directory lab9/code is where you should place all of the code that you write for this lab. Currently this directory contains two files: SlotMachine.java and CasinoSimulation.java. These files contain incomplete implementations for the SlotMachine and CasinoSimulation classes described in the program design. To complete this lab you will finish these two classes and write implementations for the Tumbler and Bank classes.

Activity #5 : Write your code.

DO NOT write any code until you understand what the casino simulation program does, the design of the program, and the output that it produces. Writing code before understanding the program will simply be a waste of your time, and will result in a program that does not work!!

As with any large project, it is probably a good idea to finish the easy parts of the project first and then slowly complete the harder parts. After studying the project description you should realize that the Tumbler and Bank classes are probably the easiest parts of this project, and completing the main() method in the CasinoSimulation class is probably the hardest.

Copy the class files from the classes directory into the code directory. Start working on the parts of this program that you feel are the easiest ones, and work your way to the hardest ones. As you implement the individual pieces of your program, compile that class and be sure that the program compiles without errors. As you compile your code, it will overwrite our solution class file which you copied from the classes directory. You should test each piece of the program as it is completed. This may require that you write special test code for testing the intermediate functionality. If you wait until you have all the code written to start testing, it will be almost impossible to locate and correct errors in your program.

Reminder: You will need to know how to create random numbers in Java (RandomNumbers.html)

Activity #6 : Submit your code

How To Submit

You will submit your work in four steps, one submission for each of the class in the program. The try commands that you must execute to submit your work are shown below:


   try grd-231 lab9-1 Tumbler.java
        


   try grd-231 lab9-2 Bank.java
        


   try grd-231 lab9-3 SlotMachine.java
        


   try grd-231 lab9-4 Tumbler.java Bank.java SlotMachine.java CasinoSimulation.java
        

As in all of the other labs, it is up to you to test your program and verify that it works before you submit it.

When you feel that you have reached an important milestone in the program's development, submit it. This way if you fail to get any further before the due date at least you will have submitted something. If you change any class after you have submitted it, you must resubmit it. For example if while working on the SlotMachine class, you discover that your implementation of the Tumbler class is incorrect, you must resubmit the Tumbler.java file to the lab9-1 target. The versions of each class that you submit to lab9-4 must match the submissions to the previous targets.

Remember to submit early, and often. The labs get very busy the night a lab is due! Although you may start to run try before midnight, it may not finish before midnight. The best strategy is to finish before the labs get crazy, then you can sit back and watch the others panic.


Grade Computation

Grade Breakdown: