|
|
|
|
@ -12,9 +12,9 @@ import java.util.*;
|
|
|
|
|
*/
|
|
|
|
|
public abstract class Race implements Runnable {
|
|
|
|
|
protected BoatInRace[] startingBoats;
|
|
|
|
|
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
|
|
|
|
|
protected Leg[] legs;
|
|
|
|
|
protected ArrayList<Leg> legs;
|
|
|
|
|
protected RaceController controller;
|
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
|
|
|
|
|
|
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
|
|
|
|
|
|
|
|
|
|
@ -23,22 +23,21 @@ public abstract class Race implements Runnable {
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
|
|
|
|
public Race(BoatInRace[] boats, Leg[] marks, RaceController controller) {
|
|
|
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> marks, RaceController controller) {
|
|
|
|
|
this.startingBoats = boats;
|
|
|
|
|
this.legs = marks;
|
|
|
|
|
this.legs.add(new Leg("Finish"));
|
|
|
|
|
this.controller = controller;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Race(BoatInRace[] boats, Leg[] marks) {
|
|
|
|
|
this.startingBoats = boats;
|
|
|
|
|
this.legs = marks;
|
|
|
|
|
public Race(BoatInRace[] boats, ArrayList<Leg> marks) {
|
|
|
|
|
this(boats, marks, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
|
updateController();
|
|
|
|
|
preRace();
|
|
|
|
|
simulateRace();
|
|
|
|
|
if(controller != null) controller.updateInfoTable(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void preRace() {
|
|
|
|
|
@ -47,7 +46,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
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]);
|
|
|
|
|
startingBoats[i].setCurrentLeg(legs.get(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -64,7 +63,7 @@ public abstract class Race implements Runnable {
|
|
|
|
|
long timeLoopStarted;
|
|
|
|
|
long timeLoopEnded;
|
|
|
|
|
|
|
|
|
|
while (finishingBoats.size() < startingBoats.length) {
|
|
|
|
|
while (boatsFinished < startingBoats.length) {
|
|
|
|
|
timeLoopStarted = System.currentTimeMillis();
|
|
|
|
|
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
|
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
|
|
|
@ -83,27 +82,29 @@ public abstract class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
|
|
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
|
|
|
|
|
updateController();
|
|
|
|
|
//boat has passed onto new leg
|
|
|
|
|
if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) {
|
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
|
//boat has finished
|
|
|
|
|
boat.setTimeFinished(timeElapsed);
|
|
|
|
|
boat.setCurrentLeg(new Leg("Finish"));
|
|
|
|
|
finishingBoats.add(boat);
|
|
|
|
|
boatsFinished++;
|
|
|
|
|
} else {
|
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
|
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
|
|
|
|
|
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void updateController() {
|
|
|
|
|
if(controller != null) controller.updateInfoTable(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ArrayList<BoatInRace> getFinishingBoats() {
|
|
|
|
|
return finishingBoats;
|
|
|
|
|
public BoatInRace[] getStartingBoats() {
|
|
|
|
|
return startingBoats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|