# Help with a Dice Game.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-23-2012, 06:27 PM
HalfAZN
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?

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;                                 } }             }         }     } }```
• 04-23-2012, 06:46 PM
Tolls
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.
• 04-23-2012, 08:28 PM
DarrylBurke
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

• 04-23-2012, 08:32 PM
Norm
Re: Help with a Dice Game.
Does the roll() method called on lines 32 & 33 return a value?
• 04-23-2012, 09:45 PM
HalfAZN
Re: Help with a Dice Game.
Quote:

Originally Posted by Norm
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
• 04-23-2012, 10:11 PM
Norm
Re: Help with a Dice Game.
Why do you call the methods on those lines?
• 04-24-2012, 10:39 AM
Tolls
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?
• 04-25-2012, 06:06 PM
HalfAZN
Re: Help with a Dice Game.
Here is the die class
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;         } }```
• 04-25-2012, 06:07 PM
HalfAZN
Re: Help with a Dice Game.
Quote:

Originally Posted by Norm
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?
• 04-25-2012, 06:12 PM
Norm
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.
• 04-25-2012, 06:21 PM
HalfAZN
Re: Help with a Dice Game.
Quote:

Originally Posted by Norm
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
• 04-25-2012, 06:27 PM
Norm
Re: Help with a Dice Game.
This statement:
dieA.roll(); // ignores returned value
vs this
int val = dieA.roll(); // saves returned value in val
• 04-25-2012, 06:31 PM
HalfAZN
Re: Help with a Dice Game.
int num1=dieA.roll();
int num2=dieB.roll();

So I should delete these?
• 04-25-2012, 06:39 PM
Tolls
Re: Help with a Dice Game.
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.
• 04-25-2012, 06:53 PM
HalfAZN
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
• 04-25-2012, 07:13 PM
Tolls
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.
• 04-26-2012, 06:07 PM
HalfAZN
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
• 04-26-2012, 06:13 PM
k1ng
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.
• 04-26-2012, 06:19 PM
Norm
Re: Help with a Dice Game.
Quote:

How would I go about changing the value
Use an assignment statement.
• 04-26-2012, 06:34 PM
HalfAZN
Re: Help with a Dice Game.
Quote:

Originally Posted by Norm
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:

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;                                 } }             }         }     } }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last