File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Cannot break infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot break infinite loop" Watch "Cannot break infinite loop" New topic
Author

Cannot break infinite loop

Mathew Brown
Greenhorn

Joined: Jan 07, 2013
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.
Your point is 8.
The die came to 2.
Your point is 8.
The die came to 8.
Your point is 8.
The die came to 7.
Your point is 8.
The die came to 11.
Your point is 8.
The die came to 9.
Your point is 8.
The die came to 7.
Your point is 8.
The die came to 8.
Your point is 8.
The die came to 12.
Your point is 8.
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

Joined: Jan 07, 2013
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

Joined: Aug 07, 2007
Posts: 2173
    
  47

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

Joined: Aug 07, 2007
Posts: 2173
    
  47
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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot break infinite loop
 
Similar Threads
Help with Control Structures.
Craps Game Code
Bad operand types for binary operator?
While loop that depends on inputs form another class?
Calling a class to output print within another class?