package craps; /* * @author Beaney */ import javax.swing.*; import java.util.Random; public class Craps { //create random number generator for use in method rolldice private static final Random randomNumbers = new Random(); //enumeration 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; //Continue Option private static int option; //game statistics private static int bank = 500; private static int lost = 0 ; private static int won = 0 ; private static int myPoint = 0;//point if no win or loss on first roll //plays many rounds of craps public static void main(String[] args) { Status gameStatus; // can contain CONTINUE, WON or LOST //Game Rules JOptionPane.showMessageDialog(null,"GAME RULES:\n\nYou roll two dice." + " Each die has six faces, which contain one, two, three, four" + "," + " five, and \nsix spots, respectively. After the dice " + "have come" + " to rest, the sum of the spots on the two " + "\nupward faces is " + "calculated. If the sum is 7 or 11 on " + "the first throw, you win." + " If the sum \nis 2, 3 or 12 on" + " the first throw (called " + "\"craps\"), you lose (i.e., " + "the \"house\" wins). If the \nsum is " + "4, 5, 6, 8, 9, or " + "10 on the first throw, that sum becomes your" + " \"point.\" To win, \nyou must continue rolling the dice " + "until" + " you \"make your point\" (i.e., roll that same " + "\npoint value). " + "You lose by rolling a 7 before making " + "your point."); //game continues until user enters NO option while (option == JOptionPane.YES_OPTION) //first while loop { boolean cont = true;//continue playing rounds till user selects no while ( cont )//second while loop { [COLOR="Red"]//enter bet and display players starting bank roll String betString = JOptionPane.showInputDialog("Enter Bet: Your " + "Starting Bank has " + bank + " Dollars!!!!!!"); int bet = Integer.parseInt(betString); //if ( betString == JOptionPane.CANCEL_OPTION)[/COLOR] int sumOfDice = rollDice();//first roll of the dice //determine game status and point based on first roll switch (sumOfDice) { case SEVEN: // win with 7 on first roll case YO_LEVEN: //win with 11 on first roll JOptionPane.showMessageDialog(null, "You rolled: " + sumOfDice); 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 JOptionPane.showMessageDialog(null, "You rolled: " + sumOfDice); gameStatus = Status.LOST; break; default://did not win or lose, so remember point gameStatus = Status.CONTINUE;//game is not over myPoint = sumOfDice;//remember this point JOptionPane.showMessageDialog(null, "Point is " + myPoint + "\nKeep Rolling " + "until the same point value is obtained, okay?"); break;//optional at end of switch }//end of switch // while game is not complete while ( gameStatus == Status.CONTINUE )// not won or lost...point { sumOfDice = rollDice(); //roll dice again JOptionPane.showMessageDialog(null,"Sum of Dice is: " + sumOfDice + "\nPoint is " + myPoint ); //display a message in between rolls where neither point or loss //were achieved if ( sumOfDice != myPoint && sumOfDice != SEVEN) { JOptionPane.showMessageDialog(null,"Keep Rolling! Your on FIRE!"); } //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; }//end third while //display won or lost message if ( gameStatus == Status.WON ) { //System.out.print( "Player wins" ); won += bet;//increment winning amount bank += bet;//increment bank amount //display winning message along with round stats JOptionPane.showMessageDialog(null,"Player wins \nPlayer rolled: " + sumOfDice + "\nPoint was: " + myPoint + "\nMoney won: " + won + "\nMoney lost: " + lost + "\nNew Bank Total: " + bank); }//end if else { lost += bet;//increment lost amount bank -= bet;//decrement bank amount //display losing message along with round stats JOptionPane.showMessageDialog(null,"Player loses \nPlayer " + "rolled: " + sumOfDice + "\nPoint was: " + myPoint + "\nMoney won: " + won + "\nMoney lost: " + lost + "\nNew Bank Total: " + bank); }//end else //ask user if they want to play another round option = JOptionPane.showConfirmDialog(null, "Play Another Round?"); //if user answers no if ( (option == JOptionPane.NO_OPTION) || (option == JOptionPane.CANCEL_OPTION)) { //displays final stats for that round JOptionPane.showMessageDialog(null, "FINAL STATS FOR THIS ROUND: " + "\nMoney Won: " + won + "\nMoney lost: " + lost + "\nNew Bank Total: " + bank); //break out of second while loop cont = false; }//end if }//end second while //ask user if they want to continue playing game option = JOptionPane.showConfirmDialog(null, "continue playing game?"); //if user does not want to play anyfurther display final game stats and //exit if ( (option == JOptionPane.NO_OPTION) || (option == JOptionPane.CANCEL_OPTION)) JOptionPane.showMessageDialog(null, "FINAL Game STATS: \nMoney Won: " + won + "\nMoney lost: " + lost + "\nNew Bank Total: " + bank + "\n THANK YOU FOR PLAYING!!!"); }//end first while }//end main //roll dice, calculate sum and display results public static int rollDice() { //pick random die values int die1 = 1 + randomNumbers.nextInt( 6 ); //first die roll int die2 = 1 + randomNumbers.nextInt( 6 ); //second die roll int sum = die1 + die2; // sum of die values return sum; //return sum of dice }//end method on rollDice }//end class Craps