Source of Craps.java


  1: // Fig. 6.9: 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 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 final static int SNAKE_EYES = 2;
 15:    private final static int TREY = 3;
 16:    private final static int SEVEN = 7;
 17:    private final static int YO_LEVEN = 11;
 18:    private final static int BOX_CARS = 12;
 19: 
 20:    // plays one game of craps
 21:    public void play()
 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\n", 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 method play
 66: 
 67:    // roll dice, calculate sum and display results
 68:    public 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\n", 
 78:          die1, die2, sum );
 79: 
 80:       return sum; // return sum of dice
 81:    } // end method rollDice
 82: } // end class Craps
 83: 
 84: /**************************************************************************
 85:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 86:  * Pearson Education, Inc. All Rights Reserved.                           *
 87:  *                                                                        *
 88:  * DISCLAIMER: The authors and publisher of this book have used their     *
 89:  * best efforts in preparing the book. These efforts include the          *
 90:  * development, research, and testing of the theories and programs        *
 91:  * to determine their effectiveness. The authors and publisher make       *
 92:  * no warranty of any kind, expressed or implied, with regard to these    *
 93:  * programs or to the documentation contained in these books. The authors *
 94:  * and publisher shall not be liable in any event for incidental or       *
 95:  * consequential damages in connection with, or arising out of, the       *
 96:  * furnishing, performance, or use of these programs.                     *
 97:  *************************************************************************/