Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, your job is to create a version of a carnival game in which

ID: 663027 • Letter: F

Question

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


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).

A requirement: your DogTrack class must include and MAKE ESSENTIAL USE OF the following methods, in addition to the DogTrack constructor and principal method playGame(). These are:

Explanation / Answer

//DogTrack.java
public class DogTrack
{
  
   //create private spin variables for s,r and f
   private int s;
   private int r;
   private int f;
   int trackSize; //declare a track size
  
   //constructor that sets the size
   public DogTrack(int size)
   {
       trackSize = size;
       s=0;
       r=0;
       f=0;
   }
  
  
   //The method playGame that calls the spin and corrspodning dog to advance
   //the positiono of dog until the game is over
   //if game is over then the game stops and display the track
   //adn winner
   public void playGame()
   {
       while (isOver()==false)
       {
           spin();
           moveRover();
           spin();
           moveSpot();
           spin();
           moveFido();
           showTrack();
       }
       if (isOver()==true)
       {
           showTrack();
           showWinners();
       }
   }

   //The metod moveRover calls th spin to get the random value between 1 and 5
   public void moveRover()
   {
       r = r + spin();
   }
  
   //The metod moveSpin calls th spin to get the random value between 1 and 5
   public void moveSpot()
   {
       s = s + spin();
   }
  
   //The metod moveFido calls th spin to get the random value between 1 and 5
   public void moveFido()
   {
       f = f + spin();
   }
  
   //Return a random value between 1 and 5
   public int spin ()
   {
      
       return 1+(int)(Math.random()*5);
   }
   //check if the dog crosss the track size
   public boolean isOver()
   {
       if(r >= (trackSize - 1) || s >= (trackSize - 1) || f >= (trackSize -1))
           return true;
       else
           return false;
   }
   //Displays the tracks of three dogs
   public void showTrack()
   {
       String rover = "";
       for (int x = 0; x <trackSize; x++)
       {
           if(r >= x)
               rover = rover + "R";
           else
               rover = rover + "o";
       }
       String spot = "";
       for (int x = 0; x <trackSize; x++)
       {
           if(s >= x)
               spot = spot + "S";
           else
               spot = spot + "o";
       }
       String fido = "";
       for (int x = 0; x <trackSize; x++)
       {
           if(f >= x)
               fido = fido + "F";
           else
               fido = fido + "o";
       }
      
       System.out.println(rover);
       System.out.println(spot);
       System.out.println(fido);
      
   }
  
   //Displays winner of the game
   public void showWinners()
   {
       String winner = "";
       if (r>=(trackSize-1))
           winner= "Rover Wins!";
       else if (s>=(trackSize-1))
           winner= "Spot Wins!";
       else if (f>=(trackSize-1))
           winner= "Fido Wins!";

       System.out.println(winner);
   }
}

-------------------------------------------------------------------------------------------------------------------

//The program prompts user to enter the size of the track
//and prints the winner of the game with locations of dogs
//DogDriver.java
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();  
}
}

-------------------------------------------------------------------------------------------------------------------

sample ouput:

Enter an int > 3: size of the track
6
track Size: 6
RRRRoo
SSSSSS
FFoooo
RRRRoo
SSSSSS
FFoooo
Spot Wins!

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote