#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Help with Pig Dice Game Please


    Having trouble getting this to work correctly. Not sure what I am doing wrong. I think I have it set up fairly correctly.
    I also want to put a modulus in so that I only have to use even or odd to determine which player is currently rolling.

    The design for this assignment is really quite simple. It asks player 1 to roll or to hold and they are only rolling 1 die. If they roll a 1 their turnScore goes to 0 and it is the other players turn. This is also a human so two people can play. No computer player in this version at all.

    If you guy see any errors let me know. We just started learning methods and classes so something could be off. thanks!

    Here is the code:
    Code:
    /*Davin Kohnz
     * CS 140 Program 3
     * Summer 2013
     * 6/26/13
     */
    package program.pkg3;
    import javax.swing.JOptionPane;
    import java.util.Random;
    
    public class Program3 {
    public static final int DIE_SIDES=1;
    public static final int TARGET_SCORE=100;
    public static Random rand= new Random();
    public static final int playerOneScore=0, playerTwoScore=0;
        public static void main(String[] args) {
        int die= rand.nextInt(DIE_SIDES)+1;
        int scoreForRound= 0;
        JOptionPane.showMessageDialog(null, "Welcome to Pig!");
        while((playerOneScore < TARGET_SCORE) || playerTwoScore <TARGET_SCORE){
        int reply=JOptionPane.showConfirmDialog(null, "Player 1:"
                + playerOneScore + "\n" +"Player 2:" + playerTwoScore +"\n" +
                 "Player 1's turn\nClick \"Yes\" to roll, \"No\" to hold\n"
                + "Score for round="+ scoreForRound,
                "Pig!",JOptionPane.YES_NO_OPTION);
        if(reply==JOptionPane.YES_OPTION){
            JOptionPane.showMessageDialog(null, "You Rolled a: "+ die);
        }else if(reply==JOptionPane.NO_OPTION){
            JOptionPane.showMessageDialog(null, "You held with round score of: "
                    + scoreForRound);
        }else if(die==1) {
            JOptionPane.showMessageDialog(null, "You Rolled a: "+ die);
            JOptionPane.showMessageDialog(null, "You pigged out!");
        }     
        }
        }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please edit your post and wrap the code in code tags:
    [code]
    the code goes here
    [/code]

    One problem I see is that the main() method is empty. It should create an instance of the class to start execution.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and wrap the code in code tags:
    [code]
    the code goes here
    [/code]

    One problem I see is that the main() method is empty. It should create an instance of the class to start execution.
    Yea i know that but not sure what goes there. I know that is what makes the game run, but have never used methods in code
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    How did you execute your program for testing? There would have to be a class with a main() method that the java program could call to start the program's execution.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by NormR
    How did you execute your program for testing? There would have to be a class with a main() method that the java program could call to start the program's execution.
    Alright I am trying something completely different. I think i can get away without using more classes/methods. Its not going to look pretty but we have only touched on them for a day.

    The problem so far with this one is that the while loop never ends and cannot figure out why.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    the while loop never ends
    That would be because the while's condition is never false. Check the while's condition to see why it does not go false.
    What are the values of the variables used in the while condition as the loop executes?
    Add a println() statement to print them out.
    If either of the compares in the condition are true, the whole condition is true.
    With the OR operator, both compares need to be false for the condition to be false.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by NormR
    That would be because the while's condition is never false. Check the while's condition to see why it does not go false.
    What are the values of the variables used in the while condition as the loop executes?
    Add a println() statement to print them out.
    If either of the compares in the condition are true, the whole condition is true.
    With the OR operator, both compares need to be false for the condition to be false.

    Ok the loop does exit after i hold with a number larger than the TARGET_SCORE.
    New problem arose with this though. When I roll a 1 it does not say you pigged out afterwards, it does after I roll ANOTHER 1 but I need to do it on the first one.
    And lastly why is it giving me a random number the first time I "roll" but keeps giving me the same number the next time I hit roll

    p.s. sorry for long winded and thanks for the help!
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you print out the program's output on the console, copy the console and paste it here so I can see what the program is doing?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by NormR
    Can you print out the program's output on the console, copy the console and paste it here so I can see what the program is doing?
    Listening on javadebug
    User program running
    User program finished

    this what you are looking for?

    I am using netBeans so just tell me where I need to pull the info from
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Sorry, I have not idea where the output from calls to the println() statement go in your IDE. There must be a view/window somewhere that displays the program's output.
    You would need to add calls to println() in parallel to the calls to the JOptionPane class's methods which are not shown in the console.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by NormR
    Sorry, I have not idea where the output from calls to the println() statement go in your IDE. There must be a view/window somewhere that displays the program's output.
    You would need to add calls to println() in parallel to the calls to the JOptionPane class's methods which are not shown in the console.
    ok give me a sec, I will change the JOptions to println so you can see what it is outputting
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Soccerscode
    ok give me a sec, I will change the JOptions to println so you can see what it is outputting
    ok that did not work. When the user clicks yes it shoots out a die 2-6 (1 is special case) it adds it to roundScore which works, but the next time you click yes you get the same number over and over
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Be sure to post the new code if you have any questions about it.

IMN logo majestic logo threadwatch logo seochat tools logo