diff --git a/src/main/java/seng302/Model/Race.java b/src/main/java/seng302/Model/Race.java index 4533c646..a7b30fe2 100644 --- a/src/main/java/seng302/Model/Race.java +++ b/src/main/java/seng302/Model/Race.java @@ -20,7 +20,7 @@ public abstract class Race implements Runnable { protected int boatsFinished = 0; protected long totalTimeElapsed; - protected int scaleFactor = 1; + protected int scaleFactor = 15; private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop protected int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race @@ -52,8 +52,12 @@ public abstract class Race implements Runnable { } public Race(BoatInRace[] boats, ArrayList legs, int scaleFactor) { - for (BoatInRace boat : boats) { - boat.setScaledVelocity(boat.getVelocity() * scaleFactor); + if (boats.length > 0) { + for (BoatInRace boat : boats) { + if (boat != null) { + boat.setScaledVelocity(boat.getVelocity() * scaleFactor); + } + } } this.startingBoats = FXCollections.observableArrayList(boats); this.legs = legs; diff --git a/src/test/java/seng302/Model/RaceTest.java b/src/test/java/seng302/Model/RaceTest.java index ab1d2ed3..10e56bce 100644 --- a/src/test/java/seng302/Model/RaceTest.java +++ b/src/test/java/seng302/Model/RaceTest.java @@ -30,16 +30,16 @@ public class RaceTest { new BoatInRace("AU", 2800, Color.BEIGE, "AU") }; ArrayList 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)); + 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())); + Arrays.sort(boats, (a, b) -> (int) (b.getVelocity() - a.getVelocity())); race.run(); - for(int i = 0; i < boats.length; i++) + for (int i = 0; i < boats.length; i++) assertTrue(boats[i].equals(race.getStartingBoats().get(i))); } @@ -84,7 +84,8 @@ public class RaceTest { Leg leg1 = new Leg("1", new GPSCoordinate(0, 0), new GPSCoordinate(1, 1), 0); Leg leg2 = new Leg("2", new GPSCoordinate(0, 0), new GPSCoordinate(1, 1), 1); - legs.add(leg2); legs.add(leg2); + legs.add(leg2); + legs.add(leg2); ConstantVelocityRace race = new ConstantVelocityRace(new BoatInRace[1], legs); @@ -114,7 +115,7 @@ public class RaceTest { @Test public void scalerScalesVelocityCorrectly() { - int scaleFactor = 0; + int scaleFactor = 3; float vel1 = 0; float vel2 = (float) 1.999; float vel3 = (float) 32.5; @@ -123,12 +124,29 @@ public class RaceTest { BoatInRace boat2 = new BoatInRace("test", vel2, Color.ALICEBLUE, "tt"); BoatInRace boat3 = new BoatInRace("test", vel3, Color.ALICEBLUE, "tt"); BoatInRace boat4 = new BoatInRace("test", vel4, Color.ALICEBLUE, "tt"); - BoatInRace[] boats = new BoatInRace[] {boat1, boat2, boat3, boat4}; + BoatInRace[] boats = new BoatInRace[]{boat1, boat2, boat3, boat4}; ConstantVelocityRace race = new ConstantVelocityRace(boats, new ArrayList(), scaleFactor); - assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-8); - assertEquals(race.getStartingBoats().get(1).getScaledVelocity(), vel2 * scaleFactor, 1e-8); - assertEquals(race.getStartingBoats().get(2).getScaledVelocity(), vel3 * scaleFactor, 1e-8); - assertEquals(race.getStartingBoats().get(3).getScaledVelocity(), vel4 * scaleFactor, 1e-8); + assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-6); + assertEquals(race.getStartingBoats().get(1).getScaledVelocity(), vel2 * scaleFactor, 1e-6); + assertEquals(race.getStartingBoats().get(2).getScaledVelocity(), vel3 * scaleFactor, 1e-6); + assertEquals(race.getStartingBoats().get(3).getScaledVelocity(), vel4 * scaleFactor, 1e-6); + } + + @Test + public void scalerScalesRaceClockTo1MinCorrectly() { + int scaleFactor = 10; + ConstantVelocityRace race = new ConstantVelocityRace(new BoatInRace[5], new ArrayList(), scaleFactor); + race.totalTimeElapsed = 6000; //6 seconds + assertTrue(race.calcTimer().equals("Race clock: 00:01:00")); + } + + @Test + public void scalerScalesRaceClockHoursMinutesAndSecondsCorrectly() { + int scaleFactor = 3; + ConstantVelocityRace race = new ConstantVelocityRace(new BoatInRace[5], new ArrayList(), scaleFactor); + race.totalTimeElapsed = 3213000; + assertTrue(race.calcTimer().equals("Race clock: 02:40:39")); + } }