From ab1445f1c22d91a249a978ad3f5465a5cf9eb48b Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Wed, 8 Mar 2017 12:31:31 +1300 Subject: [PATCH] Race events now display the boats heading and direction - The boats velocity is being read from the config file - The event text is now being printed when the leg starts #fix Tags: #story[7] #implement --- doc/examples/config.json | 2 +- src/main/java/seng302/App.java | 33 ++++++++++++---- src/main/java/seng302/Boat.java | 33 ++++++++-------- src/main/java/seng302/Event.java | 23 +++++++++++ src/main/java/seng302/Leg.java | 7 ++++ src/main/java/seng302/Marker.java | 12 ++++++ src/main/java/seng302/Race.java | 59 +++++++++++++++++++++-------- src/test/java/seng302/RaceTest.java | 18 --------- 8 files changed, 130 insertions(+), 57 deletions(-) diff --git a/doc/examples/config.json b/doc/examples/config.json index 5b6474bd..c32caf45 100644 --- a/doc/examples/config.json +++ b/doc/examples/config.json @@ -1,6 +1,6 @@ { "race-name": "AC35", - "time-scale": 1, + "time-scale": 100, "race-size": 2, "teams": [ {"team-name": "Oracle Team USA", "velocity": 20.9}, diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 4128889c..4031553b 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -5,6 +5,11 @@ import java.lang.reflect.Array; public class App { + /** + * Builds a race object for the AC35 course + * + * @param numberOfBoats, the number of boats to include in the race + */ public static Race createRace(int numberOfBoats) throws Exception{ Race race = new Race(); @@ -12,6 +17,7 @@ public class App FileParser fp = new FileParser("doc/examples/config.json"); ArrayList boatNames = new ArrayList<>(); ArrayList> teams = fp.getTeams(); + for (Map team : teams) { boatNames.add((String) team.get("team-name")); } @@ -25,14 +31,18 @@ public class App } for (int i = 0; i < numberOfBoats; i++) { - race.addBoat(new Boat(boatNames.get(i))); + race.addBoat(new Boat(boatNames.get(i), (Double)(teams.get(i).get("velocity")))); } race.addLeg(new Leg(035, 100, "Start")); race.addLeg(new Leg(010, 300, "Marker 1")); race.addLeg(new Leg(350, 400, "Leeward Gate")); race.addLeg(new Leg(010, 400, "Windward Gate")); - race.addLeg(new Leg(010, 400, "Leeward Gate")); + + Leg finishingLeg = new Leg(010, 400, "Leeward Gate"); + finishingLeg.setFinishingLeg(true); + + race.addLeg(finishingLeg); return race; } @@ -43,21 +53,30 @@ public class App try{ race = createRace(2); - } catch (Exception e){ System.out.println(e); } + // If race was created if (race != null){ + race.displayStartingBoats(); + + System.out.println("\n\n"); + System.out.println("######################"); + System.out.println("# Live Race Updates "); + System.out.println("######################"); race.startRace(); - race.displayFinishingOrder(); - - + System.out.println("\n\n"); + System.out.println("######################"); + System.out.println("# Race Results "); + System.out.println("######################"); + race.showRaceMarkerResults(); + race.displayFinishingOrder(); } else{ - System.out.println("e"); + System.out.println("There was an error creating the race."); } } } \ No newline at end of file diff --git a/src/main/java/seng302/Boat.java b/src/main/java/seng302/Boat.java index 8e3aec2a..0343541f 100644 --- a/src/main/java/seng302/Boat.java +++ b/src/main/java/seng302/Boat.java @@ -1,35 +1,38 @@ package seng302; -/* - Represents a boat in the race. - - @param teamName The name of the team sailing the boat +/** +* Represents a boat in the race. +* +* @param teamName The name of the team sailing the boat +* @param boatVelocity The speed of the boat in meters/second */ public class Boat { - // The name of the team, this is also the name of the boat - private String teamName = null; - private float velocity = 70; // please set this one to a reasonable num!!!!!, i set it just for testing ;) + + private String teamName; // The name of the team, this is also the name of the boat + private double velocity; // In meters/second public Boat(String teamName) { this.teamName = teamName; + this.velocity = 10; // Default velocity } - public Boat(String teamName, float boatVelocity) { + + public Boat(String teamName, double boatVelocity) { this.teamName = teamName; this.velocity = boatVelocity; } - /* - Returns the name of the team sailing the boat - @returns The name of the team + /** + * Returns the name of the team sailing the boat + * @return The name of the team */ public String getTeamName(){ return this.teamName; } - /* - Sets the name of the team sailing the boat - @param teamName The name of the team + /** + * Sets the name of the team sailing the boat + * @param teamName The name of the team */ public void setTeamName(String teamName){ this.teamName = teamName; @@ -47,7 +50,7 @@ public class Boat * Gets velocity of the boat * @return a float number of the boat velocity */ - public float getVelocity() { + public double getVelocity() { return this.velocity; } } \ No newline at end of file diff --git a/src/main/java/seng302/Event.java b/src/main/java/seng302/Event.java index 5a36fad1..72670285 100644 --- a/src/main/java/seng302/Event.java +++ b/src/main/java/seng302/Event.java @@ -6,6 +6,10 @@ import java.util.Date; /** * Event class containing the time of specific event, related team/boat, and * event location such as leg. + * + * @param eventTime, what time the event happens + * @param eventBoat, the boat that the event belongs to + * @param eventLeg, the leg the event happens on */ public class Event { @@ -74,4 +78,23 @@ public class Event { public Leg getLeg() { return this.leg; } + + /** + * Call when the boat reaches the marker, this will tell the marker the order + * in which boats pass it + */ + public void addBoatToMarker(){ + this.leg.addBoatToMarker(boat); + } + + /** + * Get a string that contains the timestamp and course information for this event + * @return A string that contains the timestamp and course information for this event + */ + public String getEventString(){ + String currentHeading = Integer.toString(this.getLeg().getHeading()); + String velocityKnots = String.format("%1.2f", this.getBoat().getVelocity() * 1.943844492); // Convert meters/second to knots + + return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " passed " + this.getLeg().getMarkerLabel() + " going heading " + currentHeading + " at " + velocityKnots + " knots."); + } } diff --git a/src/main/java/seng302/Leg.java b/src/main/java/seng302/Leg.java index 862725a2..e6af0839 100644 --- a/src/main/java/seng302/Leg.java +++ b/src/main/java/seng302/Leg.java @@ -83,6 +83,13 @@ public class Leg { return this.startingMarker.getName(); } + /* + Tell the marker that the boat has passed it + */ + public void addBoatToMarker(Boat boat){ + this.startingMarker.addBoat(boat); + } + /* Specify whether or not the race finishes on this leg diff --git a/src/main/java/seng302/Marker.java b/src/main/java/seng302/Marker.java index 2bed2dc0..991ab667 100644 --- a/src/main/java/seng302/Marker.java +++ b/src/main/java/seng302/Marker.java @@ -1,10 +1,14 @@ package seng302; +import java.util.ArrayList; + class Marker{ private String name; + private ArrayList boatOrder; public Marker(String name){ this.name = name; + this.boatOrder = new ArrayList(); } public void setName(String name){ @@ -14,4 +18,12 @@ class Marker{ public String getName(){ return this.name; } + + public void addBoat(Boat boat){ + this.boatOrder.add(boat); + } + + public Boat[] getBoats(){ + return this.boatOrder.toArray(new Boat[this.boatOrder.size()]); + } } \ No newline at end of file diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index e1af5351..f354e26a 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -1,12 +1,14 @@ package seng302; import java.util.*; - +import java.lang.reflect.Array; +import java.util.concurrent.TimeUnit; public class Race { - private ArrayList boats; - private ArrayList legs; - private PriorityQueue events; + private ArrayList boats; // The boats in the race + private ArrayList legs; // The legs in the race + private ArrayList finishingOrder; // The order in which the boats finish the race + private PriorityQueue events; // The events that occur in the race private int numberOfBoats = 0; private long startTime = 0; private int timeScale = 1; @@ -14,12 +16,15 @@ public class Race { public Race() { this.boats = new ArrayList(); this.legs = new ArrayList(); - // create a priority queue within custom Comparator to order events + this.finishingOrder = new ArrayList(); + + // create a priority queue with a custom Comparator to order events this.events = new PriorityQueue(new Comparator() { @Override public int compare(Event o1, Event o2) { Long time1 = o1.getTime(); Long time2 = o2.getTime(); + // order event asc. by time. if tie appears, then order team // name alphabetically. if (time1 != time2) { @@ -60,7 +65,7 @@ public class Race { @returns a list of boats */ public Boat[] getFinishedBoats() { - return getShuffledBoats(); + return this.finishingOrder.toArray(new Boat[this.finishingOrder.size()]); } /* @@ -88,9 +93,10 @@ public class Race { int numberOfBoats = this.getNumberOfBoats(); Boat[] boats = this.getFinishedBoats(); - System.out.println("--- Finishing Order ---"); + System.out.println("\n\n"); + System.out.println("--- Finishing Order ---"); - for (int i = 0; i < numberOfBoats; i++) { + for (int i = 0; i < Array.getLength(boats); i++) { System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName()); } } @@ -98,11 +104,13 @@ public class Race { /* Prints the list of boats competing in the race */ - private void displayStartingBoats() { + public void displayStartingBoats() { int numberOfBoats = this.getNumberOfBoats(); Boat[] boats = this.getBoats(); - System.out.println("--- Starting Boats ---"); + System.out.println("######################"); + System.out.println("# Competing Boats "); + System.out.println("######################"); for (int i = 0; i < numberOfBoats; i++) { System.out.println(boats[i].getTeamName()); @@ -136,10 +144,10 @@ public class Race { for (Boat boat : this.boats) { long totalDistance = 0; for (Leg leg : this.legs) { - totalDistance += leg.getDistance(); long time = (long) (1000 * totalDistance / (boat.getVelocity() * this.timeScale)); Event event = new Event(time, boat, leg); events.add(event); + totalDistance += leg.getDistance(); } } } @@ -158,20 +166,34 @@ public class Race { } /** - * Micheal, here is a demo function shows you how to iterate all events + * Iterate over events in the race and print the + * event string for each event */ public void iterateEvents() { // iterates all events. ends when no event in events. while (!events.isEmpty()) { Event peekEvent = events.peek(); long currentTime = System.currentTimeMillis() - this.startTime; + if (currentTime > peekEvent.getTime()) { // pull out the event Event nextEvent = events.poll(); + // I just simply print it out for testing - System.out.println(nextEvent.getTimeString() + ", " + - nextEvent.getBoat().getTeamName() + " passed " + - nextEvent.getLeg().getMarkerLabel()); + System.out.println(nextEvent.getEventString()); + nextEvent.addBoatToMarker(); + + if (nextEvent.getLeg().getIsFinishingLeg()){ + this.finishingOrder.add(nextEvent.getBoat()); + } + } + + // Wait for 100ms to slow down the while loop + try{ + Thread.sleep(100); + } + catch(java.lang.InterruptedException e){ + continue; } } } @@ -185,9 +207,14 @@ public class Race { generateEvents(); this.startTime = System.currentTimeMillis(); iterateEvents(); + } + /* + Print the order in which the boats passed each marker + */ + public void showRaceMarkerResults(){ for (Leg leg : this.legs) { - Boat[] boats = this.getShuffledBoats(); + Boat[] boats = leg.getMarker().getBoats(); System.out.println("--- " + leg.getMarkerLabel() + " ---"); diff --git a/src/test/java/seng302/RaceTest.java b/src/test/java/seng302/RaceTest.java index 6d45766e..741e3752 100644 --- a/src/test/java/seng302/RaceTest.java +++ b/src/test/java/seng302/RaceTest.java @@ -10,24 +10,6 @@ import java.lang.reflect.Array; */ public class RaceTest { - /* - Test that all boats that were added to the race also finish the race - */ - @Test - public void testFinishingBoats() - { - Boat boat1 = new Boat("Team 1"); - Boat boat2 = new Boat("Team 2"); - Boat boat3 = new Boat("Team 3"); - - Race race = new Race(); - race.addBoat(boat1); - race.addBoat(boat2); - race.addBoat(boat3); - - assertEquals(Array.getLength(race.getFinishedBoats()), 3); - } - /* Test that all boats were added to the race */