diff --git a/.idea/compiler.xml b/.idea/compiler.xml index c9e75922..e1957136 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,32 +1,18 @@ - - - - - - - - - - - - - - - - + - + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 6361584b..4347747d 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/src/main/java/seng302/ConstantVelocityRace.java b/src/main/java/seng302/ConstantVelocityRace.java index b045d2a8..7439ce52 100644 --- a/src/main/java/seng302/ConstantVelocityRace.java +++ b/src/main/java/seng302/ConstantVelocityRace.java @@ -8,21 +8,21 @@ import java.util.Collections; public class ConstantVelocityRace extends Race { /** * Initialiser for a Race with constant velocity. - * @param boats array of boats + * @param startingBoats array of boats * @param marks array of RaceMarkers that the boats need to pass in order to finish the course. * @param timescale integer that the race is at timescale = 1000ms * @see seng302.Boat * @see seng302.RaceMarker */ - public ConstantVelocityRace(Boat[] boats, RaceMarker[] marks, int timescale) { - super(boats, marks, timescale); + public ConstantVelocityRace(Boat[] startingBoats, RaceMarker[] marks, int timescale) { + super(startingBoats, marks, timescale); } /** * Generates the Race with respects to boat speed. */ protected void generateRace() { - for(Boat boat : boats) { + for(Boat boat : startingBoats) { for(int i = 0; i < marks.length; i++) { RaceMarker raceMarker = marks[i]; if (i != marks.length - 1) { diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index 30195da8..08ea0212 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -8,12 +8,14 @@ import java.util.*; * Created by fwy13 on 3/03/17. */ public abstract class Race { - protected Boat[] boats; + protected Boat[] startingBoats; + protected ArrayList finishingBoats = new ArrayList<>(); protected RaceMarker[] marks; protected int timescale = 1000; protected LinkedList events = new LinkedList(); protected Timer timer = new Timer(); + private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop /** * Initailiser for Race @@ -22,7 +24,7 @@ public abstract class Race { * @param timescale Number or milliseconds that = 1000ms. */ public Race(Boat[] boats, RaceMarker[] marks, int timescale) { - this.boats = boats; + this.startingBoats = boats; this.marks = marks; this.timescale = timescale; generateRace(); @@ -36,13 +38,37 @@ public abstract class Race { //show the boats participating. System.out.println("Boats Participating:"); System.out.println("===================="); - for (int i = 0; i < boats.length; i ++){ - System.out.println(i + 1 + ". " + boats[i].getName() + ", Speed: " + Math.round(boats[i].getVelocity() * 1.94384) + "kn"); + 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"); } System.out.println("\nRace Events:"); System.out.println("============"); //show all the events that happen in the race + + long timeStarted = System.currentTimeMillis(); + long timeElapsed; + + long timeLoopStarted; + long timeLoopEnded; + + while (finishingBoats.size() < startingBoats.length) { + timeLoopStarted = System.currentTimeMillis(); + timeElapsed = System.currentTimeMillis() - timeStarted; + for (Boat boat : startingBoats) { + checkPosition(boat, timeElapsed); + } + try { + timeLoopEnded = System.currentTimeMillis(); + Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted)); + } catch (InterruptedException e) { + return; + } + } + + + + for (Event event: events) { timer.schedule(new TimerTask(){ @Override @@ -73,6 +99,12 @@ public abstract class Race { }, (events.getLast().getTime() + 1) * timescale); } + + + private void checkPosition() { + + } + /** * This function is a function that generates the Race and populates the events list. * Is automatically called by the initialiser function, so that simulateRace() does not return an empty race.