(Game of Craps) Write an application that runs 1000
games of craps (Fig 6.9)
and answers the following questions:
- How many games are won on the
first roll, second roll, ..., twentieth roll, and after
the twentieth roll?
- How many games are lost on the
first roll, second roll, ..., twentieth roll, and after
the twentieth roll?
- What are the chances of winnning at
craps? [Note: You should discover that craps
is one of the fairest casino games. What do you suppose
this means?]
- What is the average length of a game
of craps?
- Do the chances of winning improve
with the length of the game?
First of all, we only need to make very minor changes to
the existing craps
program. I suggest the following:
- Get rid of all the System.out.println
statements. We don’t want thousands of lines of
output when we run the game 1000 times.
- Change the play method to return a boolean
value, true if the player wins, false if the player
loses.
- Keep track of how many rolls of the dice were made
during the course of the game. Provide a
getNumberOfRolls method that returns this
value so the calling routine can determine how long
the most recently played game was.
Secondly, your simulation program should look something
like this (pseudocode):
Create a craps game object
Create an array to track wins by length of game
Create an array to track losses by length of game
Repeat 1000 times:
play a game of craps
increment the wins (or losses) array based on
the length of the game
increment the total wins or loses
For each possible number of rolls (1 to 20+)
print number of wins and losses
Print overall winning percentage
Print average game length
My solution will be available
here and
here after the end of
next week’s class.