|
|
|
|
@ -11,7 +11,6 @@ public abstract class Race {
|
|
|
|
|
protected BoatInRace[] startingBoats;
|
|
|
|
|
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
|
|
|
|
|
protected Leg[] legs;
|
|
|
|
|
protected int timescale = 1000;
|
|
|
|
|
|
|
|
|
|
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
|
|
|
|
|
|
|
|
|
|
@ -19,26 +18,26 @@ public abstract class Race {
|
|
|
|
|
* Initailiser for Race
|
|
|
|
|
* @param boats Takes in an array of boats that are participating in the race.
|
|
|
|
|
* @param marks Number of marks in order that the boats pass in order to complete the race.
|
|
|
|
|
* @param timescale Number or milliseconds that = 1000ms.
|
|
|
|
|
*/
|
|
|
|
|
public Race(BoatInRace[] boats, Leg[] marks, int timescale) {
|
|
|
|
|
public Race(BoatInRace[] boats, Leg[] marks) {
|
|
|
|
|
this.startingBoats = boats;
|
|
|
|
|
this.legs = marks;
|
|
|
|
|
this.timescale = timescale;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
printStartingDetails();
|
|
|
|
|
preRace();
|
|
|
|
|
simulateRace();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void printStartingDetails() {
|
|
|
|
|
private void preRace() {
|
|
|
|
|
//show the boats participating.
|
|
|
|
|
System.out.println("Boats Participating:");
|
|
|
|
|
System.out.println("====================");
|
|
|
|
|
for (int i = 0; i < startingBoats.length; i++) {
|
|
|
|
|
System.out.println(i + 1 + ". " + startingBoats[i].getName() + ", Speed: " + Math.round(startingBoats[i].getVelocity() * 1.94384) + "kn");
|
|
|
|
|
startingBoats[i].setCurrentLeg(legs[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -60,7 +59,7 @@ public abstract class Race {
|
|
|
|
|
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
|
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
|
|
|
updatePosition(boat, SLEEP_TIME);
|
|
|
|
|
checkPosition(boat);
|
|
|
|
|
checkPosition(boat, totalTimeElapsed);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
timeLoopEnded = System.currentTimeMillis();
|
|
|
|
|
@ -73,15 +72,19 @@ public abstract class Race {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void checkPosition(BoatInRace boat) {
|
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
|
|
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
|
|
|
|
|
//boat has passed onto new leg
|
|
|
|
|
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
|
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
|
if (boat.getCurrentLeg().getLegNumber() > legs.length) {
|
|
|
|
|
if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) {
|
|
|
|
|
//boat has finished
|
|
|
|
|
boat.setTimeFinished(timeElapsed);
|
|
|
|
|
finishingBoats.add(boat);
|
|
|
|
|
} else {
|
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
|
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
|
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|