D.10. Case Study: A Game of Chance; Introducing Enumerations

A popular game of chance is a dice game known as craps, which is played in casinos and back alleys throughout the world. The rules of the game are straightforward:

You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point.” To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making your point.

Figure D.5 simulates the game of craps, using methods to implement the game’s logic. The main method (lines 21–65) calls the rollDice method (lines 68–81) as necessary to roll the dice and compute their sum. The sample outputs show winning and losing on the first roll, and winning and losing on a subsequent roll.


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

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


Fig. D.5 | Craps class simulates the dice game craps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset