Question:
Modify the craps program of Fig. 5.8 to allow wagering. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to bankBalance, and if it’s not, have the user reenter wager until a valid wager is entered. Then, run one game of craps. If the player wins, increase bankBalance by wager and display the new bankBalance. If the player loses, decrease bankBalance by wager, display the new bank-Balance, check whether bankBalance has become zero and, if so, display the message "Sorry. You busted!" As the game progresses, display various messages to create some “chatter,” such as "Oh, you're going for broke, huh?" or "Aw c'mon, take a chance!" or "You're up big. Now's the time to cash in your chips!". Implement the “chatter” as a separate method that randomly chooses the string to display.
Fig. 5.8
Transcribed Image Text:
I // Fig. 5.8: Craps.java 2 // Craps class simulates the dice game craps. import java. security.SecureRandom; 3 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 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; private static final int SEVEN = 7; 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 my Point = 0; // point if no win or loss on first roll Status gameStatus; // can contain CONTINUE, WON or LOST tus://pot int sumOfDice = roll Dice (); // 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 not win or lose, so remember point gameStatus Status. CONTINUE; // game is not over myPoint sumOfDice; // remember the point System.out.printf("Point is %d %n", myPoint); } // while game is not complete while (gameStatus == Status. CONTINUE) { // not WON or LOST sumOfDice = rollDice (); // roll dice again } break; // determine game status if (sumofDice == myPoint) { // win by making point gameStatus = Status. WON; } 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"); } else { 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