Question: Write an application that runs 1,000,000 games of craps (Fig. 5.8) and answers the following questions: a) How many games are won on the first

Write an application that runs 1,000,000 games of craps (Fig. 5.8) and answers the following questions:

a) How many games are won on the first roll, second roll, …, twentieth roll and after the twentieth roll?

b) How many games are lost on the first roll, second roll, …, twentieth roll and after the twentieth roll?

c) What are the chances of winning at craps?

d) What is the average length of a game of craps?

e) Do the chances of winning improve with the length of the game?

Fig. 5.8

I 2 3 import java.security.SecureRandom; 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

I 2 3 import java.security.SecureRandom; 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // Fig. 5.8: Craps.java // Craps class simulates the dice game craps. 69 70 71 72 73 74 public class Craps { // create secure random number generator for use in method rollDice private static final SecureRandom randomNumbers = new SecureRandom(); 75 76 77 78 79 80 81 } // enum type with constants that represent the game status private enum Status {CONTINUE, WON, LOST}; // constants that represent common rolls of the dice private static final int SNAKE_EYES = 2; private static final int TREY = 3; final int SEVEN = 7; private static private static final int YO_LEVEN = 11; private static final int BOX_CARS = 12; //plays one game of craps public static void main (String [] args) { int myPoint = 0; // point if no win or loss on first roll Status gameStatus; // can contain CONTINUE, WON or LOST tus://pot int sumOfDice = rollDice (); // first roll of the dice // determine game status and point based on first roll switch (sumoOfDice) { case SEVEN: // win with 7 on first roll case YO LEVEN: // win with 11 on first roll gameStatus = Status. WON ; break; } } } case SNAKE_EYES: // lose with 2 on first roll case TREY: // lose with 3 on first roll. case BOX_CARS: // lose with 12 on first roll gameStatus Status . LOST; break; default: // did } // while game is not complete while (gameStatus == Status. CONTINUE) { // not WON or LOST sumOfDice = rollDice (); // roll dice again } gameStatus myPoint sumOfDice; // remember the point System.out.printf("Point is %d %n", myPoint); break; } // determine game status if (sumofDice = myPoint) { // win by making point gameStatus = Status. WON ; } else { } else { if (sumOfDice == SEVEN) { // lose by rolling 7 before point gameStatus Status. LOST; } // display won or lost message if (gameStatus == Status. WON) { System.out.println("Player wins"); not win or lose, so remember point Status. CONTINUE; // game is not over System.out.println("Player loses"); // roll dice, calculate sum and display results public static int rollDice () { //pick random die values int diel = 1 + randomNumbers.nextInt (6); // first die roll int die2= 1 + randomNumbers.nextInt (6); // second die roll int sum diel + die2; // sum of die values // display results of this roll System.out.printf("Player rolled %d + %d = %d %n", diel, die2, sum); return sum; Player rolled 5+ 6 = 11 Player wins Player rolled 5 + 4 = 9 Point is 9 Player rolled 4+ 2 = 6 Player rolled 3 + 6 = 9 Player wins Player rolled 1 + 2 = 3 Player loses Player rolled 2 + 6 = 8 Point is 8 Player rolled 5 + 1 = 6 Player rolled 2 + 1 = 3 Player rolled 1 + 6 = 7 Player loses

Step by Step Solution

3.58 Rating (158 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the provided image which appears to be a fragment of Java code for simulating the game of c... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!