Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Cannot break infinite loop

Mathew Brown
Greenhorn
Posts: 7
Hi Gurus;

I try to write small craps game. It will roll 2 dices, and if the player throws 2, 3, or 12 at first throw he loses. If he throw 7 or 11 he wins. If he throws none of these numbers, this number becomes "result". So the player continues to throw dices until he throws either he throws this "result" or 7. If he throws "result" he wins, if he throws 7 he looses. these are basic rules.

So main method asks player if he wants to play and he chooses "yes" he calls method play(). Actually first part of my program looks working correctly. If the player wins or looses at first throw, I receive correct output. However, if the player throws either 7 or "result" (the dices he threw at first throw), while loop is not broken and continues to throw dices in an infinite loop. I assume I made a mistake when I assigned variable but can't figure out what is. play is a boolean method who returns a false or true value. If the player looses it returns false otherwise true. Here is my play method code:

And a part of my output is like:

The die came to 8.
The die came to 2.
The die came to 8.
The die came to 7.
The die came to 11.
The die came to 9.
The die came to 7.
The die came to 8.
The die came to 12.
The die came to 9.

As you see, code won't stop if die comes to "result" neither it does if it came to 7. Any response will highly appreciated.

Thanks

Mathew Brown
Greenhorn
Posts: 7
Sorry I skipped; these are my declarations:

private static int Point = 0; //Holds players following throws value
private static int result = 0; //Holds players first throw value
private static int Dice1 = 0; //Holds Dice 1 Value
private static int Dice2 = 0; //Holds Dice 2 Value
private static int i = 0; //Game counter
private static String Playing = "Playing: ";
static Scanner sc = new Scanner(System.in); //Creates scanner so user can input information.

Tony Docherty
Bartender
Posts: 2957
59

Yes you are correct, your mistake is here.
I suggest you work through the logic of what happens if result = 5 and you roll a 5 or a 7. It's probably easiest if you write out a truth table with a column for each of the 3 tests.

Tony Docherty
Bartender
Posts: 2957
59
BTW when you find yourself writing the same basic unit of code over and over again in different sections of the program then that's a big clue that the repeated code should be in a method of it's own.
For example all those "You have won" messages are the same except for the number rolled so put them in a method called something like displayWinMsg() and pass in the number rolled so you can concatenate it into the message. The same applies for the losing messages.

Also I notice the messages have hard coded values of the number of times you have won/lost, if you are keeping track of scores then they will need to be a variables.

fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
System.out.println can be your best friend. When my code isn't doing what I want, i liberally sprinkle them throughout. So, I would put in lines like

System.out.println("Point is " + Point);
System.out.println("result is " + result);

put those JUST after your while statement, and see what is really happening.

also, your have your terminology backwards. On the initial roll, a 4,5,6,8,9,10 becomes the point. As an experienced craps player, reading your code is hard because you are using the variable "Point" (which btw should be lowercase) to hold the result of a specific roll, and the variable "result" to hold the point.

and one other note..."die" is singular. "dice" is plural. "dices" is not a word.