From 1c2870649ada04ede72aae8df9acf97467572308 Mon Sep 17 00:00:00 2001 From: Alistair McIntyre Date: Thu, 10 Aug 2017 18:58:38 +1200 Subject: [PATCH] Added testing for the tack/gybe settings. tags: #story[1105] --- src/main/java/seng302/model/Yacht.java | 23 ++++----- src/test/java/seng302/models/YachtTest.java | 53 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 src/test/java/seng302/models/YachtTest.java diff --git a/src/main/java/seng302/model/Yacht.java b/src/main/java/seng302/model/Yacht.java index 0dfe0018..d8479a94 100644 --- a/src/main/java/seng302/model/Yacht.java +++ b/src/main/java/seng302/model/Yacht.java @@ -131,9 +131,7 @@ public class Yacht { } } - if (isAuto) { - runAutoPilot(); - } + runAutoPilot(); //UPDATE BOAT LOCATION lastLocation = location; @@ -353,15 +351,13 @@ public class Yacht { * Moves the boat towards the given heading when the auto pilot was set. Disables the auto pilot * in the event that the boat is within the range of 1 turn step of its goal. */ - private void runAutoPilot() { - if (autoHeading == null) { - isAuto = false; - // TODO: 10/08/17 possibly throw some sort of exception here maybe? autopilot shouldn't be true if there's no heading. - } - turnTowardsHeading(autoHeading); - if (Math.abs(heading - autoHeading) - <= TURN_STEP) { //Cancel when within 1 turn step of target. - isAuto = false; + public void runAutoPilot() { + if (isAuto) { + turnTowardsHeading(autoHeading); + if (Math.abs(heading - autoHeading) + <= TURN_STEP) { //Cancel when within 1 turn step of target. + isAuto = false; + } } } @@ -438,8 +434,7 @@ public class Yacht { // Take optimal heading and turn into a boat heading rather than a wind heading. optimalHeading = - optimalHeading + (double) Math - .floorMod(GameState.getWindDirection().longValue(), 360L); + optimalHeading + GameState.getWindDirection(); setAutoPilot(optimalHeading); } diff --git a/src/test/java/seng302/models/YachtTest.java b/src/test/java/seng302/models/YachtTest.java new file mode 100644 index 00000000..00d9dea6 --- /dev/null +++ b/src/test/java/seng302/models/YachtTest.java @@ -0,0 +1,53 @@ +package seng302.models; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import seng302.gameServer.GameState; +import seng302.model.Yacht; + + +public class YachtTest { + + private static Yacht y1; + //Yacht y2; + private static Double windDirection = 45d; + private static Double windSpeed = 20d; + private static GameState gs; + + @BeforeClass + public static void setUp() { + y1 = new Yacht("Yacht", 101, "Y1", "Y1", "Yacht 1", "C1"); + gs = new GameState("localhost"); + } + + @Test + public void tackGybeTest() { + HashMap values = new HashMap<>(); + values.put(280.0, 80.0); + values.put(270.0, 90.0); + values.put(359.0, 1.0); + values.put(180.0, 180.0); + values.put(75.0, 285.0); + + for (Double begin : values.keySet()) { + y1.setHeading(begin); + y1.tackGybe(windDirection); + for (int i = 0; i < 50; i++) { + y1.runAutoPilot(); + } + assertEquals(values.get(begin), y1.getHeading(), 5.0); + } + + } + + + @AfterClass + public static void tearDown() { + y1 = null; + } + +}