Craps Simulation
[This is Problem 2 in Savitch's Absolute Java] In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the dice in a game round is called "the come out roll". A come out roll of 7 or 11 automatically wins, and a come out roll of 2,3 or 12 automatically loses. If any other value is rolled (4, 5, 6, 8, 9, 10) on the come out roll, it becomes the point. The player now continues to roll until either a 7 or the point value is rolled. In the former case, the player loses, in the latter, the player wins.
- Write a program (Craps.java) that simulates a game of craps using these rules without human input. Instead of asking for wagers, the program will merely calculate whether the player wins or loses. The program should print each roll until the round ends, then print either "player wins" or "player loses". You will need to use a random number generator to simulate a die being rolled.
Use the generator twice to generate a value between 1 and 6 and sum those numbers to determine each roll's value.
Write a pseudo-code solution. An instructor must okay your pseudo-code in lab.
- Convert your pseudo-code into Java. Test your code several times. Watch out for infinite loops! Make sure you neither win every time or lose every time.
- Add a loop to your program so that it plays the game 10,000 times. Add counters to keep track of how many times the player loses and wins. Comment-out the statements that printed each roll (else you'll get at least 10,000 lines of output when you run your program). At the end of the 10,000 games, print the number of wins and losses as well as the probability of winning, in general. The probability should be expressed as a fraction. For example, "The probability of winning is 0.723."
- Run your program several times. Do the results vary? If so, do they vary much? Would you rather be the owner of the casino providing this game, or a player?
- Submit your code to the dropbox. Your lab document should include the output of three runs of your program, and your answers to the these questions:
-
Do the results vary?
- If so, by how much do they seem to vary?
- Would you rather be the owner of the casino, or the player? Why?