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.