Advertisement
Guest User

Craps

a guest
Mar 4th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1.  // Craps.java
  2. // Craps class simulates the dice game craps.
  3. // Adapted from Deitel & Deitel JAVA text
  4.  
  5. import java.util.Random;
  6.  
  7. public class Craps
  8. {
  9.     // create random number generator for use in method rollDice
  10.     private Random randomNumbers = new Random();
  11.  
  12.     // enumeration with constants that represent the game status
  13.     private enum Status { CONTINUE, WON, LOST };
  14.  
  15.     // constants that represent common rolls of the dice
  16.     private final static int SNAKE_EYES = 2;
  17.     private final static int TREY = 3;
  18.     private final static int SEVEN = 7;
  19.     private final static int YO_LEVEN = 11;
  20.     private final static int BOX_CARS = 12;
  21.  
  22.     // plays one game of craps
  23.     public int play(int bet)
  24.     {
  25.         //System.out.println("You have a bet of: "+bet);
  26.         int myPoint = 0; // point if no win or loss on first roll
  27.         Status gameStatus; // can contain CONTINUE, WON or LOST
  28.  
  29.         int sumOfDice = rollDice(); // first roll of the dice
  30.  
  31.         // determine game status and point based on first roll
  32.         switch ( sumOfDice )
  33.         {
  34.             case SEVEN: // win with 7 on first roll
  35.             case YO_LEVEN: // win with 11 on first roll          
  36.             gameStatus = Status.WON;
  37.             break;
  38.             case SNAKE_EYES: // lose with 2 on first roll
  39.             case TREY: // lose with 3 on first roll
  40.             case BOX_CARS: // lose with 12 on first roll
  41.             gameStatus = Status.LOST;
  42.             break;
  43.             default: // did not win or lose, so remember point        
  44.             gameStatus = Status.CONTINUE; // game is not over
  45.             myPoint = sumOfDice; // remember the point
  46.             //System.out.printf( "Point is %d\n", myPoint );
  47.             break; // optional at end of switch
  48.         } // end switch
  49.  
  50.         // while game is not complete
  51.         while ( gameStatus == Status.CONTINUE ) // not WON or LOST
  52.         {
  53.             sumOfDice = rollDice(); // roll dice again
  54.  
  55.             // determine game status
  56.             if ( sumOfDice == myPoint ) // win by making point
  57.                 gameStatus = Status.WON;
  58.             else
  59.             if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
  60.                 gameStatus = Status.LOST;
  61.         } // end while
  62.  
  63.         // display won or lost message
  64.         if ( gameStatus == Status.WON )
  65.         {
  66.             //System.out.println( "Player wins!");
  67.             //System.out.println("Player wins: "+bet);
  68.             //bet=bet*2;
  69.             return bet;
  70.             //System.out.println("New total is: "+bet);
  71.         }
  72.         else
  73.         {
  74.             //System.out.println( "Player loses!");
  75.             //System.out.println( "Player loses: "+bet);
  76.             return -1*bet;
  77.             //System.out.println("New total is: "+bet);
  78.         }
  79.     } // end method play
  80.  
  81.     // roll dice, calculate sum and display results
  82.     public int rollDice()
  83.     {
  84.         // pick random die values
  85.         int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
  86.         int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
  87.  
  88.         int sum = die1 + die2; // sum of die values
  89.  
  90.         // display results of this roll
  91.         //System.out.printf( "Player rolled %d + %d = %d\n",
  92.           // die1, die2, sum );
  93.  
  94.         return sum; // return sum of dice
  95.     } // end method rollDice
  96. } // end class Craps
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement