Fixed finish order

- Added test to check order is determined by velocity
- Modified Race class to allow for unit testing

#story[15]
main
Connor Taylor-Brown 9 years ago
parent 05fe402fd0
commit 26f7b94e13

@ -34,8 +34,9 @@ public class Leg {
* Construction Method * Construction Method
* @param name Name of the Leg * @param name Name of the Leg
*/ */
public Leg(String name) { public Leg(String name, int number) {
this.name = name; this.name = name;
this.legNumber = number;
} }
/** /**

@ -26,6 +26,7 @@ public abstract class Race implements Runnable {
private int SLEEP_TIME = 50; //time in milliseconds to pause in a paced loop private int SLEEP_TIME = 50; //time in milliseconds to pause in a paced loop
private int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race private int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race
private boolean timerEnabled = true;
/** /**
* Initailiser for Race * Initailiser for Race
@ -35,7 +36,7 @@ public abstract class Race implements Runnable {
public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller) { public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller) {
this.startingBoats = FXCollections.observableArrayList(boats); this.startingBoats = FXCollections.observableArrayList(boats);
this.legs = legs; this.legs = legs;
this.legs.add(new Leg("Finish")); this.legs.add(new Leg("Finish", this.legs.size()));
this.controller = controller; this.controller = controller;
} }
@ -54,10 +55,14 @@ public abstract class Race implements Runnable {
public void run() { public void run() {
setControllerListeners(); setControllerListeners();
preRace(); preRace();
countdownTimer(); if(timerEnabled) countdownTimer();
simulateRace(); simulateRace();
} }
public void disableTimer() {
timerEnabled = false;
}
/** /**
* Set up the state in waiting for the race starts. * Set up the state in waiting for the race starts.
*/ */
@ -146,8 +151,8 @@ public abstract class Race implements Runnable {
} }
} }
controller.updateMap(startingBoats); if(controller != null) controller.updateMap(startingBoats);
updateTime(calcTimer()); if(timerEnabled) updateTime(calcTimer());
try { try {
timeLoopEnded = System.currentTimeMillis(); timeLoopEnded = System.currentTimeMillis();
Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted)); Thread.sleep(SLEEP_TIME - (timeLoopEnded - timeLoopStarted));

@ -1,5 +1,8 @@
package seng302.Model; package seng302.Model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.paint.Color;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
@ -8,6 +11,8 @@ import seng302.Model.ConstantVelocityRace;
import seng302.Model.Leg; import seng302.Model.Leg;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Observable;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -41,4 +46,24 @@ public class RaceTest {
// ConstantVelocityRace race = new ConstantVelocityRace(boats, legs); // ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
// race.run(); // race.run();
// } // }
@Test
public void finishOrderDeterminedByVelocity() {
BoatInRace[] boats = {
new BoatInRace("NZ", 2000, Color.BEIGE, "NZ"),
new BoatInRace("AU", 2800, Color.BEIGE, "AU")
};
ArrayList<Leg> legs = new ArrayList<>();
legs.add(new Leg("Start", new GPSCoordinate(32.296577, -64.854304),new GPSCoordinate(32.293039, -64.843983),0));
legs.add(new Leg("Start", new GPSCoordinate(32.293039, -64.843983),new GPSCoordinate(32.284680, -64.850045),1));
Race race = new ConstantVelocityRace(boats, legs);
race.disableTimer();
// Boats should finish in an order determined by their velocity
Arrays.sort(boats, (a,b) -> (int)(b.getVelocity()-a.getVelocity()));
race.run();
for(int i = 0; i < boats.length; i++)
assertTrue(boats[i].equals(race.getStartingBoats().get(i)));
}
} }

Loading…
Cancel
Save