Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree1Likes

Thread: Help with a Dice Game.

  1. #1
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Help with a Dice Game.

    The point of this game is for the user to enter a goal number, and for the program to keep rolling two dice until the sum of those dice equals that goal number. During that time, the program should note whenever doubles or snake eyes is rolled. It compiles properly, but after I enter the GOAL, nothing happens. Just to check, I had the program print the values of the two dice, and I got an infinite loop where the same values were being rolled over and over again. How do I make it so that the dice comes up with new results each time?



    Java Code:
    import cs1.Keyboard;
    public class DieGame
    {
        public static void main (String[] args)
        {
    
    
    System.out.println("Please enter a GOAL number");
          int GOAL=Keyboard.readInt();
    
    int snakeEyes=0;
    Die dieA=new Die();
    Die dieB=new Die();
    boolean contin;
    	if(GOAL<=2||GOAL>12)
    	{
    		System.out.println("INVALID NUMBERS");
                   }
    	else
    	{
    
    
    		int num1=dieA.roll();
    		int num2=dieB.roll();
    		int sum=num1+num2;
    
    while(sum!=GOAL)
    {
    	contin=true;
    
    
                  dieA.roll();
                  dieB.roll();
    
    
    
    		    if(num1==1 && num2==1)
    		    {
    		    	snakeEyes++;
    		    	System.out.println("You have rolled snake eyes");
    
    		    if(num1==num2)
    		    	{
    					System.out.println("You have rolled doubles");
    
    				}
    			if(sum==GOAL)
    				{
    					System.out.println("Matched goal of "+GOAL);
    					contin=false;
    				}
    
    
    
    }
    
    
    
        	}
    
    
    
    
    
    
    
    
    	}
        }
    
    
    
    }
    Last edited by HalfAZN; 04-23-2012 at 05:54 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,095
    Rep Power
    20

    Default Re: Help with a Dice Game.

    You need to sort out your indentation.
    It's hard to follow the flow of this code with the spacing all over the place.
    Anyway, I think you don't change the value of 'sum' at all in the loop, so if it enters the loop (ie sum != GOAL) it'll never leave it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help with a Dice Game.

    Also, please take the trouble to remove unnecessary blank lines. Check out Code Conventions for the Java Programming Language: Contents

    db

    edit: Corrected the link
    Last edited by DarrylBurke; 04-23-2012 at 08:55 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Help with a Dice Game.

    Does the roll() method called on lines 32 & 33 return a value?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    Quote Originally Posted by Norm View Post
    Does the roll() method called on lines 32 & 33 return a value?
    Yes, but it's the same for both of them. I had the program print them just to check, and that's when I saw it was an infinite loop

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Help with a Dice Game.

    Why do you call the methods on those lines?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,095
    Rep Power
    20

    Default Re: Help with a Dice Game.

    SO what does the Die class do.

    And as I said above, you never change sum in the loop, as far as I can tell, so how do you expect it to change such that it can exit the loop?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    Here is the die class
    Java Code:
    import java.util.Random;
    public class Die
    {
    	private final int MIN_FACES=4;
    
    	private static Random generator=new Random();
    	private int numFaces;
    	private int faceValue;
    
    	public Die()
    	{
    		numFaces=6;
    		faceValue=1;
    	}
    
    
    	public Die(int faces)
    	{
    		if(faces<MIN_FACES)
    			numFaces=6;
    		else
    			numFaces=faces;
    		faceValue=1;
    	}
    
    	public int roll()
    	{
    		faceValue=generator.nextInt(numFaces) +1;
    		return faceValue;
    	}
    
    	public int getFaceValue()
    	{
    		return faceValue;
    	}
    }

  9. #9
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    Quote Originally Posted by Norm View Post
    Why do you call the methods on those lines?
    Not sure what you mean by this. If you're asking why I have them there, don't I need them to keep the dice rolling?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Help with a Dice Game.

    The method returns a value that the code ignores. Why call the method if you ignore the value it returns.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    Quote Originally Posted by Norm View Post
    The method returns a value that the code ignores. Why call the method if you ignore the value it returns.
    How do I have the code not ignore the method? The method gives me the new numbers I need each time

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Help with a Dice Game.

    This statement:
    dieA.roll(); // ignores returned value
    vs this
    int val = dieA.roll(); // saves returned value in val
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    int num1=dieA.roll();
    int num2=dieB.roll();


    So I should delete these?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,095
    Rep Power
    20

    Default Re: Help with a Dice Game.

    Java Code:
    while(sum!=GOAL) {
       contin=true;
       dieA.roll();
       dieB.roll();
       if(num1==1 && num2==1) {
          snakeEyes++;
          System.out.println("You have rolled snake eyes");
       }
       if(num1==num2) {
          System.out.println("You have rolled doubles");
       }
       if(sum==GOAL) {
          System.out.println("Matched goal of "+GOAL);
          contin=false;
       }
    }
    Step through this code by eye and tell us what you expect it to do.
    This is your code you you should be able to explain to us your thinking behind the lines here and what you think they are doing and how you think this works.
    That'll give us an idea of where your misconceptions are.
    Last edited by Tolls; 04-25-2012 at 05:41 PM. Reason: Fix indentation and add missing bracket
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    1: While the sum of the two dice doesn't equal the goal that the user enters, loop the following code
    2: [deleted] because I don't think it does anything
    3: Roll the first die
    4: Roll the second die
    5: Check to see if value of both dice are equal to 1
    6: [deleted] because doesn't do anything necessary
    7: If both dice are equal to 1, print snake eyes
    8: Close if statement
    9: Check to see if value of both dice are equal
    10: Print "doubles" if values are equal
    11: Close if statement
    12: Check to see if the sum of both dice equals the goal the user set at the beginning of program
    13: If sum does equal goal, print that the user matched goal
    14: [deleted]
    15:
    16:


    I think I need to change my loop, but not sure how to change it, or what to change it to.

    I was thinking of doing:

    of setting boolean contin=true at the start of program
    and then for line one, changing it to while(contin)
    That way, when the user reaches the goal, the program will automatically stop

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,095
    Rep Power
    20

    Default Re: Help with a Dice Game.

    OK, so how do the values from the die rolls (3 and 4) get into the variables you are comparing (in 5 and 9).
    How does the value of 'sum' changed such that 1 is ever achieved and 12 is actually testing the sum of the dice as they now stand?

    Those are the things you are missing.
    You never change the value of 'sum' or either of the 'num' variables.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    How would I go about changing the value? I assumed that by having them in the loops, the roll command would keep generating values

  18. #18
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Help with a Dice Game.

    The roll() call does but sum and GOAL never change so if you keep comparing them, they will keep not being the same.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Help with a Dice Game.

    How would I go about changing the value
    Use an assignment statement.
    k1ng likes this.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with a Dice Game.

    Quote Originally Posted by Norm View Post
    Use an assignment statement.
    Not sure what you mean by this, as I'm fairly new to Java.

    Also thought I'd upload what I've changed:

    Java Code:
    import cs1.Keyboard;
    public class DieGame
    {
        public static void main (String[] args)
        {
    
    
    System.out.println("Please enter a GOAL number");
    
    	int GOAL=Keyboard.readInt();
    
    int snakeEyes=0;
    Die dieA=new Die();
    Die dieB=new Die();
    boolean contin=true;
    	if(GOAL<=2||GOAL>12)
    	{
    		System.out.println("INVALID NUMBERS");
    }
    	else
    	{
    
    
    
    		int num1=dieA.roll();
    		int num2=dieB.roll();
    
    while(contin)
    {
    int sum=num1+num2;
    
    		    if(num1==1 && num2==1)
    		    {
    		    	snakeEyes++;
    		    	System.out.println("You have rolled snake eyes");
    
    		    if(num1==num2)
    		    	{
    					System.out.println("You have rolled doubles");
    
    				}
    			if(sum==GOAL)
    				{
    					System.out.println("Matched goal of "+GOAL);
    					contin=false;
    
    				}
    }
    
        	}
    
    	}
        }
    
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Pig dice game
    By dangyounoobs in forum New To Java
    Replies: 2
    Last Post: 12-09-2013, 04:21 AM
  2. Dice Game
    By Rachel1991 in forum New To Java
    Replies: 1
    Last Post: 11-24-2011, 11:10 PM
  3. Pig Dice Game Help
    By pvccstudent in forum New To Java
    Replies: 3
    Last Post: 07-10-2011, 02:21 AM
  4. Need help with AI in roll dice game
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 11:49 AM
  5. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •