Hi, I need to complete this assignment. Everything seems to work except I need to output "T" and "H" along the dashed lines (respectively) everytime either the Tortoise or the Hare moves. Here is my code, thank you in advance.

import javax.swing.JOptionPane;

public class Tort
{
    public static void main(String[] args) 
        {
            Races app = new Races();
            boolean Choice = true;

            app.displayTrack();
            app.beginRace();
            app.moveRacers();
            app.tieCheck();
            app.displayWinner();
            app.raceReset();
            /*while (Choice)
            {
                String option = JOptionPane.showInputDialog("Run Again?");
                if (option.equals("y"))
                    Choice = true;      
                else 
                    Choice = false;
            }*/
        }
}




public class Races 
{
    String [] ttrack = new String [70];
    String [] htrack = new String [70];
    int tort = 0;
    int hare = 0;

    public Races()
    {

    }

    public void beginRace()
    {
        System.out.println("BANG !!!!!");
        System.out.println("AND THEY'RE OFF !!!!!");
    }

    public void displayTrack()
    {
        for (int trackLoop = 0; trackLoop < 70; trackLoop++)
        {
            ttrack[0] = "T";
            for (int tLoop = 1; tLoop < 70; tLoop ++)
                ttrack[tLoop] = "-";

            htrack[0] = "H";
            for (int hLoop = 1; hLoop < 70; hLoop ++)
                htrack[hLoop] = "-";

            for (int rtLoop = 0; rtLoop < ttrack.length; rtLoop ++)
                System.out.print(ttrack[rtLoop]);
                System.out.print("\n");

            for (int rhLoop = 0; rhLoop < htrack.length; rhLoop ++)
                System.out.print(htrack[rhLoop]);
                System.out.print("\n"); 


        }
    }

    public void moveRacers()
    {
        while (tort < 70)
        {   
            int percentTort = 1 + (int)(11 * Math.random());

            //Slip
            if (percentTort == 1 || percentTort == 2)
                tort -= 6;

            //Slow Plod
            else if (percentTort == 3 || percentTort == 4 || percentTort == 5)
                tort += 1;

            //Fast Plod
            else if (percentTort == 6 || percentTort == 7 || percentTort == 8 || percentTort == 9 || percentTort == 10)
                tort += 3;

            else if (tort < 0)
                tort = 0;


        //ttrack[tort].replace('-', 'T');
        }

        while (hare < 70)
        {
            int percentHare = 1 + (int)(11 * Math.random());

            //Big Slip
            if (percentHare == 1)
                hare -= 12;

            //Sleep
            if (percentHare == 2 || percentHare == 3)
                hare += 0;

            //Big Hop
            if (percentHare == 4 || percentHare == 5)
                hare += 9;

            //Small Slip
            if (percentHare == 6 || percentHare == 7)
                hare -= 2;

            //Small Hop
            if (percentHare == 8 || percentHare == 9 || percentHare == 10)
                hare += 1;

            if (hare < 0)
                hare = 0;   


        //htrack[hare].replace('-', 'T');
        }


    }

    public void tieCheck()
    {

    }

    public void displayWinner()
    {

        if (tort > hare)
        {
            System.out.println("TORTOISE WINS!!! YAY!!!");
        }

        else if (hare > tort)
        {
            System.out.println("Yuch");
        }

        else
        {
            System.out.println("It's a tie.");
        }
    }

    public void raceReset()
    {
        tort = 0;
        hare = 0;
    }
}

Recommended Answers

All 3 Replies

You need to fill the strings ttrack and htrace with dashes after you create them. Then your code to substitute the dash with 'T' or 'H' will work. You also need to display the strings after each round of moveRacers(). Also, your code will not set the correct element in the string unless you also keep track of their current position, and set that position with the 'T' or 'H', and not the element at 'tort' or 'hare'. IE, you need to add that computed value to the last position to determine where each is in the race.

String [] ttrack = new String []{"-"};
String [] htrack = new String []{"-"};

I've declared them w/ dashes but this will only fill one space, is there a way to declare will all spots (70) filled?

Were you able to figure out how to fix it? I'm interested in seeing how it turned out. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.