# Thread: Help with a Dice Game.

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

## 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)
{

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. Moderator
Join Date
Apr 2009
Posts
13,215
Rep Power
23

## 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.

3. ## 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

Last edited by DarrylBurke; 04-23-2012 at 08:55 PM.

4. ## Re: Help with a Dice Game.

Does the roll() method called on lines 32 & 33 return a value?

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

## Re: Help with a Dice Game.

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

6. ## Re: Help with a Dice Game.

Why do you call the methods on those lines?

7. Moderator
Join Date
Apr 2009
Posts
13,215
Rep Power
23

## 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?

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

## 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. Member
Join Date
Apr 2012
Location
St. Louis, MO
Posts
32
Rep Power
0

## Re: Help with a Dice Game.

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?

10. ## 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.

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

## Re: Help with a Dice Game.

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

12. ## Re: Help with a Dice Game.

This statement:
dieA.roll(); // ignores returned value
vs this
int val = dieA.roll(); // saves returned value in val

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

## Re: Help with a Dice Game.

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

So I should delete these?

14. Moderator
Join Date
Apr 2009
Posts
13,215
Rep Power
23

## 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

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

## 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. Moderator
Join Date
Apr 2009
Posts
13,215
Rep Power
23

## 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.

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

## 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. Member
Join Date
Apr 2012
Posts
59
Rep Power
0

## 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. ## Re: Help with a Dice Game.

How would I go about changing the value
Use an assignment statement.

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

## Re: Help with a Dice Game.

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:

Java Code:
```import cs1.Keyboard;
public class DieGame
{
public static void main (String[] args)
{

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 Last

#### Posting Permissions

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