Rosifr Rosifr - 16 days ago 5
Java Question

Implementing a Changing Racetrack into My Java Driver

I need to create a racetrack which looks like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y
---------------------------------------------------------------------------
|AB| | | | | | | | | | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------


where A is a car and B is another car.

they will need to progress along the racetrack in a manner like this using pre-determined speeds and fuel consumption rates:

A B C D E F G H I J K L M N O P Q R S T U V W X Y
---------------------------------------------------------------------------
| | | |A | | | | | | B| | | | | | | | | | | | | | | |
---------------------------------------------------------------------------



A B C D E F G H I J K L M N O P Q R S T U V W X Y
---------------------------------------------------------------------------
| | | | | | | | | | |A | | | | | | | | | |B | | | | |
---------------------------------------------------------------------------


In my main method driver (the controller for my racing game), I have the racetrack output showing for the start of the race:

public static void main(String [] args)
{
System.out.println(" A B C D E F G H I J K L M N O P Q R S T U V W X Y");
System.out.println("---------------------------------------------------------------------------");
System.out.println("|VP| | | | | | | | | | | | | | | | | | | | | | | | |");
System.out.println("---------------------------------------------------------------------------");



}


But I have no idea how I will update the positions of the cars with successive rounds of the race. Should this racetrack drawing be implemented as an array? using
System.out.print("");
creates a static racetrack.

Answer

if car A is at position 5, you can print 4 | before printing the car and then print the rest

something like

    int len = 20;
    String trackBit = "| ";
    int pos = 5;

    for (int x = 0; x < len; x++) {
        if (x == pos) {
          System.out.print("|A");
        }
        else {
          System.out.print(trackBit);
        }
    }
Comments