# Thread: Infinite running loop problems

1. Member
Join Date
Oct 2009
Posts
2
Rep Power
0

## Infinite running loop problems

Hi, I was trying to do my assignment. And at one point, I dont know what I did so that when I execute my program, it runs into an infinite loop. Please help.

here is the details of the assignment

Players: 2 - 6
Equipment: Five 4-sided dice and a supply of poker chips
Object: Be the first to accumulate 30 chips or roll all five dice at once without rolling a 1.
Setup: There is a holding area called the Pot which is initially empty. There is an unlimited supply of chips that come from the Bank. All players start with no chips.
Play: Play begins with a random player. On each turn, one chip from the Bank is added to the Pot. When your turn begins, you roll one die. If you roll a "1" you are "aced out" and your turn is over.
If you do not roll a "1", you may either collect the Pot and end your turn or you may roll again. If you roll, two chips are added to the Pot and you roll two dice. If you roll a "1" on either die, you are "aced out". If you don't roll any 1's, you can either take the pot or keep going with three dice and adding three chips to the Pot.
The process continues, using one more die and adding one more chip each time you repeat. Whether you ace out or pass, the next player always starts again at one die. Anything left in the Pot remains for the next player to shoot for.
Winning: If you make it all the way to 5 dice and still don't ace out, you win the game. Otherwise, the winner is the first player to collect 30 chips.
The program:
Implement a Bogart game in Java. Use good object oriented design and use classes as appropriate.
The program should be interactive, and allow number of players to be selected before play begins.
Each roll should report the size of the Pot and the numbers that come up on the dice.
When a turn ends, report the number of chips that player currently has.
At the end of the game, report which player wins and how many chips each player has.
You can write either a console or GUI based program.

2. gcampton Guest
Ok, well first time I ran it, it hit an infinite loop as soon as I entered the number of players. stuck the code in JCreator just to make it extra buggy, I had to close this window down it stopped resonding.
I then re-opened it and it seems to work but now gives me exceptions:
Java Code:
```Enter the number of players (2 to 6):
5
It's player #2's turn.
Dice Rolled!
The number that comes up on the dice: 3
The size of the Pot: 1 chips.
Dice Rolled!
The number that comes up on the dice: 1
The size of the Pot: 2 chips.
***Player #2 have rolled a 1***.
***Aced out!***
Player #2 has 0 chips.
======
It's player #1's turn.
Dice Rolled!
The number that comes up on the dice: 2
The size of the Pot: 3 chips.
Dice Rolled!
The number that comes up on the dice: 1
The size of the Pot: 4 chips.
***Player #1 have rolled a 1***.
***Aced out!***
Player #1 has 0 chips.
======
It's player #4's turn.
Dice Rolled!
The number that comes up on the dice: 1
The size of the Pot: 5 chips.
***Player #4 have rolled a 1***.
***Aced out!***
Player #4 has 0 chips.
======
It's player #4's turn.
Dice Rolled!
The number that comes up on the dice: 3
The size of the Pot: 6 chips.
Dice Rolled!
The number that comes up on the dice: 1
The size of the Pot: 7 chips.
***Player #4 have rolled a 1***.
***Aced out!***
Player #4 has 0 chips.
======
It's player #4's turn.
Dice Rolled!
The number that comes up on the dice: 3
The size of the Pot: 8 chips.
Dice Rolled!
The number that comes up on the dice: 1
The size of the Pot: 9 chips.
***Player #4 have rolled a 1***.
***Aced out!***
Player #4 has 0 chips.
======
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at BogartGameConsole.main(BogartGameConsole.java:76)```
each consecutive time I run it I get the same problems it runs but get's exceptions if I clear the system processes of all running java procresses I get the infinite loop again, until I kill JCreator and with the old process still running in windows reopen JCreator and start a new one, I get exceptions.

JCreator always has this bug that if it crashes the process stays open, why this would change the outcome of your program is beyond me, But I'm going to say it's definitely a logic error, somewhere somewhere.....somewhere.

are you sure you can do "if(in.hasNextInt() == true)"
I have not thought of doing that before is all so am unsure if this works, usually to validate I use a do while loop like this:

do
{
System.out.print("Enter an int or else");
if (!(in.hasNextInt()))
{
in.nextLine();
}
} while (!(in.hasNextInt())) ;;
int variable = in.nextInt();

this way the user must enter an integer for it to work, if a letter is entered then it simply says ERROR try again.. , but do you need try catches for this?
Last edited by gcampton; 10-10-2009 at 07:28 PM.

3. gcampton Guest
hell i would even stick my do..while in a while(players>6||players<2)
so they have to keep entering data until they enter correct data, throwing exceptions at them will generally annoy people and they will uninstall your game faster than you can release a patch to fix it.

4. gcampton Guest
are you sure you can do "if(in.hasNextInt() == true)"
I have not thought of doing that before is all so am unsure if this works, usually to validate I use a do while loop like this:
i guess you can do this but it's the same as saying :
if (in.hasNextInt()) <--- if statements must always evaluate to true for them to work

5. Member
Join Date
Oct 2009
Posts
1
Rep Power
0

## I think i can help...

I let you know later today i have been looking at it and think i figured it out i'll let you know.

6. Member
Join Date
Oct 2009
Posts
2
Rep Power
0
Thanks guys for helping me out. I've got it all figured out earlier today.

Thanks a lot

#### Posting Permissions

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