This program simulates the dice game Craps. Here are the rules:
- The shooter rolls two dice.
- If the shooter rolls a 7 or 11, s/he immediately wins.
- If the shooter rolls a 2, a 3 or a 12, s/he immediately loses.
- If the shooter rolls any other number, that number becomes the 'point' and the game continues.
- The shooter then tries to roll the point again before s/he rolls a 7.
- For example: the shooter initially rolls a 6, and 6 becomes the point.
- The shooter then rolls several times and rolls a second 6,
without first rolling a 7. S/he wins.
Alternatively, if the shooter rolls a 7 before rolling a second 6, s/he loses.
Write the playGame() method to simulate the game.
Here is a screenshot that shows playGame() running:
As you can see, once the game concludes, you give the player the opportunity to play again.
This option allows the user to have the computer run as many iterations of Craps as the user wishes. It then displays the number of games won and lost, and the amount of money the user would gain or lose if s/he receives $1 for each win and owes $1 for each loss. Finally, the average gain or loss per game is calculated.
Write the analyzeGame() method to implement this option.
Here is a screenshot that shows analyzeGame() running:
Try running the game 100,000 or 1,000,000 times.
When the program begins, let the user choose whether to simulate the game or analyze it. This can be done in the Output window. Below is a screenshot:
You can download Craps.java to use as a starting point in writing your program.
To see how the program runs when it is done, you can download Simulations.jar. Depending on which browser you are using, the program may run if you choose 'Open'. If not, you will have to select 'Save'. After the file downloads, double-click it and the completed program should run. (The file extension may change from .jar to .zip during the download. If this occurs, either while downloading or after downloading rename the file and change the file extension back to .jar.)
Thanks to Josh for the idea.