Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Craps- GAME fine tunning.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Craps- GAME fine tunning.

    How come my cancel button does not work when the user is asked to place a bet and he hits the cancel button?
    if the user hits the cancel button how do i make it end the round and end the game (terminate program)?
    the code in question is highlighted in red font and I am also open to any suggestions that might make my program better
    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Craps- GAME fine tunning.

    JOptionPan.showInputDialog() i believe returns a null string if the cancel button is pushed. You need to check that before you try to parse the number into an int. To exit a program completely, you can use System.exit(), but it's not the option I would recommend. Rather, simply put a return statement.

    //enter bet and display players starting bank roll
                String betString = JOptionPane.showInputDialog("Enter Bet: Your " +
                    "Starting Bank has " + bank + " Dollars!!!!!!");
                if (betString == null)
                {
                      return;
                }
                int bet = Integer.parseInt(betString);

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Beaney (January 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Craps- GAME fine tunning.

    thank you that did the trick

Similar Threads

  1. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  2. Breakout Game
    By Ceasar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2009, 12:30 AM
  3. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 12:46 PM
  4. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM