Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Project runs infinitely

I have a Java project that is due later tonight and I can't seem to figure out why my code doesn't work.

Here is the assignment:
For this assignment, your job is to create a version of a carnival game in which mechanical dogs race along a track. The racing game is called DogTrack. DogTrack is also the name of the java source file you must submit to complete the assignment. The DogTrack class should be written so that the driver class below, DogDriver, works as intended.

A dog track has N discrete positions, 0 through N-1. The value of N is determined interactively in the application driver (below) once your program begins running.

There are three dogs, named Fido, Spot, and Rover. Each starts at position 0. A spinner selects (returns) a random value from among the values 1-2-3-4-5; these values are equally likely. Each dog advances by a separate spin of the spinner. In general a dog advances 1 space when the spinner spins a 1, 2 spaces when the spinner spins a 2, and so forth. The game is over when (at least) one of the dog crosses the finish line, that is, when at least one dog reaches a board value that is greater than or equal to (N-1).

A special condition: if a dog lands exactly at position number N/3 or position number 2N/3, where N is the size of the track, then that dog returns to its position before the move was made, and then moves back one more square, if possible. Because there are no negative positions, a dog at 0 that advances to N/3 on the first move does NOT go to (negative) position -1; after the move it should simply remain at position 0. See sample run below for an example.

At each round, the track should be drawn, using the symbol o, and the letters F, S, and R (for Fido, Spot, and Rover).

You should draw the state of the race as three "lines", one for each dog, using an 'o' to represent unoccupied positions, and F,R, and S to represent dog positions. These 3 line groups should be separated by a row of dashes, as shown in the sample run below. If the track size is 10, then this drawing

ooooooSooo

means that the dog Spot is at location 6 on the track, with earlier positions 0-5 unoccupied (dog tracks always begin with position 0).

Here is the driver class for the racing game:

```import java.util.Scanner;
public class DogDriver{
public static void main(String[] args){
System.out.println("Enter an int > 3: size of the track");
Scanner s = new Scanner(System.in);
int trackSize = s.nextInt();
System.out.println("track Size: " + trackSize);
DogTrack d = new DogTrack(trackSize);
d.playGame();
}
}```

Here is what I have so far:
```public class DogTrack{
private int fidoPos = 0;
private int spotPos = 0;
private int roverPos = 0;
private int trackSize;
private int a;

public void playGame(){
while (isOver() == false){
spin(a);
moveFido();
spin(a);
moveSpot();
spin(a);
moveRover();
showTrack();
}
if (isOver() == true){
showTrack();
showWinners();
}
}

public DogTrack(int size){
trackSize = size;
}

public void spin(int a){
a = ((int)Math.random()*5) + 1;
}

public void moveFido(){
fidoPos = fidoPos + a;
}

public void moveSpot(){
spotPos = spotPos + a;
}

public void moveRover(){
roverPos = roverPos + a;
}

public boolean isOver(){
if (fidoPos >= (trackSize - 1) || spotPos >= (trackSize - 1) || roverPos >= (trackSize - 1)){
return true;
}
else{
return false;
}
}

public void showTrack(){
String fido = "";
for (int j = 0; j < trackSize; j++){
if (j != fidoPos){
fido = fido + "o";
}
else {
fido = fido + "F";
}
}

String spot = "";
for (int j = 0; j < trackSize; j++){
if (j != spotPos){
spot = spot + "o";
}
else {
spot = spot + "S";
}
}

String rover = "";
for (int j = 0; j < trackSize; j++){
if (j != roverPos){
rover = rover + "o";
}
else{
rover = rover + "R";
}
}
System.out.println(fido);
System.out.println(spot);
System.out.println(rover);
}

public String showWinners(){
if(fidoPos >= (trackSize - 1)) return "Fido wins!";

if (spotPos >= (trackSize - 1)) return "Spot wins!";

if (roverPos >= (trackSize - 1)) return "Rover wins!";

else return null;
}
}```

When I try to run the program, it just keeps printing
Fooooooo
Sooooooo
Rooooooo
over and over again infinitely. I suppose that means it won't go back and spin() each time, but I don't know how to fix that and why it's doing this in the first place.

Any suggestions would be greatly appreciated.

--- Update ---

Also, I have been using this thread:
"http://www.javaprogrammingforums.com/whats-wrong-my-code/38040-help-me-my-code-project-not-sure-how-fix-errors.html"
as a reference to fix a lot of what was wrong with my program, but I tried to do it a little differently.

2. ## Re: Project runs infinitely

figure out why my code doesn't work.
What is not working? How are you trying to debug the code to see what the problem is?

I use println statements that print out the values of variables as they are used and changed. Also
print out the value of an expression to see if it is evaluating to what you expect.

3. ## Re: Project runs infinitely

Excellent first post. Thanks for putting some effort into fitting in.

The playGame() method should drive the game's action, but the results of the spin() method are not being applied to each of the dogs. An option might be to have the spin() method return a value which is then sent to the moveDog() methods, something like:

moveFido( spin( a ) );

You might also think about whether 'a' is needed as a parameter to spin().

4. ## Re: Project runs infinitely

In reply to Norm: The issue is that it keeps giving the same output over and over forever. There are no errors. It just does not do what I intended it to.

In reply to Greg: So I could make the spin() method like this

```   public int spin(int a){
a = = ((int)Math.random()*5) + 1
return a;
}```

and then moveFido() like this

```public void moveFido(){
fidoPos = fidoPos + spin(a);
}```

So when I tried adding this in, it stopped going on forever. However it now prints this when the trackSize is set to 4:

oFoo
oSoo
oRoo
ooFo
ooSo
ooRo
oooF
oooS
oooR
oooF
oooS
oooR

So something in my loop must be wrong.

5. ## Re: Project runs infinitely

Almost got it. I recommend that you check the spin() method. As Norm suggested, add some print statements to figure out what values are being generated there.

6. ## Re: Project runs infinitely

Figured out what was wrong with spin!!!

```   public int spin(){
return (1+ (int)(Math.random()*5));
}```
and changed the rest of my code so that there was no "int a", just spin(). So that worked much better! Thank you for the help!

It looks like my loops are still wrong, however, because the code does not stop when a dog reaches the finish line, NOR does it print the winners, NOR does it print the dogs at the starting line. I will have to go back and figure all those out, too.

So I've narrowed down that it looks like my boolean isn't right but I'm not sure what to do about it.

7. ## Re: Project runs infinitely

Good! You're on the right track. Don't be hard on your loops, though. Investigate the loop's exit conditions, and investigate why the exit isn't being calculated as desired.

Style points:

This condition and the others like it:

while ( isOver() == false )

are verbose. Simply

while ( !isOver() )

will do.

8. ## Re: Project runs infinitely

I think the problem may be more with the boolean rather than the loops. The boolean isOver should make it so once one of the dog's position is greater than or equal to the trackSize, the spinning will stop and winners will be printed. Instead, it just prints finishes up spinning and doesn't move on to what happens when the boolean is true.