mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Compare commits
13 Commits
sprint_1.0
...
sprint_1.1
| Author | SHA1 | Date | |
|---|---|---|---|
| abc5df7837 | |||
| debe2c0cca | |||
| cfa851b968 | |||
| 37f4b55b04 | |||
| c1aa38c1b0 | |||
| 260bf06219 | |||
| 8d85557e10 | |||
| d33a88d313 | |||
| d3b71c21e5 | |||
| d10c6a54f5 | |||
| 0a86dde7e4 | |||
| ae80b434f6 | |||
| b0cd7c8c08 |
@@ -1,13 +1,31 @@
|
|||||||
{
|
{
|
||||||
"race-name": "AC35",
|
"race-name": "AC35",
|
||||||
"time-scale": 1.0,
|
"time-scale": 2.0,
|
||||||
"race-size": 4,
|
"race-size": 6,
|
||||||
"teams": [
|
"teams": [
|
||||||
{"team-name": "Oracle Team USA", "velocity": 20.9},
|
{
|
||||||
{"team-name": "Artemis Racing", "velocity": 18.3},
|
"team-name": "Oracle Team USA",
|
||||||
{"team-name": "Emirates Team New Zealand", "velocity": 21.5},
|
"velocity": 20.9
|
||||||
{"team-name": "Groupama Team France","velocity": 19.9},
|
},
|
||||||
{"team-name": "Land Rover BAR", "velocity": 17.6},
|
{
|
||||||
{"team-name": "SoftBank Team Japan", "velocity": 16.6}
|
"team-name": "Artemis Racing",
|
||||||
|
"velocity": 18.3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Emirates Team New Zealand",
|
||||||
|
"velocity": 21.5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Groupama Team France",
|
||||||
|
"velocity": 19.9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Land Rover BAR",
|
||||||
|
"velocity": 17.6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "SoftBank Team Japan",
|
||||||
|
"velocity": 16.6
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1 +1,15 @@
|
|||||||
# User Manual
|
# User Manual
|
||||||
|
|
||||||
|
## Running the application
|
||||||
|
|
||||||
|
When you execute the application, it will try to load a configuration file called config.json located in doc/examples/.
|
||||||
|
|
||||||
|
You can specify a config file using the using the -f flag, for example 'java -jar app.jar -f doc/examples/config1.json'
|
||||||
|
|
||||||
|
## The config file
|
||||||
|
|
||||||
|
The teams/boats are specified in the config file under 'teams', each team requires a team name, and a velocity (in meters per second).
|
||||||
|
|
||||||
|
The 'time-scale' option lets you change how long the race takes to complete. A time-scale of 1.0 is normal speed, 2.0 is 2x etc.
|
||||||
|
|
||||||
|
The 'race-size' option lets you specify how many boats will be selected to compete in each race. There must be at least this many teams defined.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>seng302</groupId>
|
<groupId>seng302</groupId>
|
||||||
@@ -40,7 +40,8 @@
|
|||||||
<version>2.4.3</version>
|
<version>2.4.3</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<transformers>
|
<transformers>
|
||||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
<manifestEntries>
|
<manifestEntries>
|
||||||
<Main-Class>seng302.App</Main-Class>
|
<Main-Class>seng302.App</Main-Class>
|
||||||
<X-Compile-Source-JDK>${maven.compiler.source}</X-Compile-Source-JDK>
|
<X-Compile-Source-JDK>${maven.compiler.source}</X-Compile-Source-JDK>
|
||||||
|
|||||||
@@ -1,87 +1,111 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Builds a race object for the AC35 course
|
|
||||||
*/
|
|
||||||
public static Race createRace() throws Exception{
|
|
||||||
Race race = new Race();
|
|
||||||
|
|
||||||
// Read team names from file
|
/**
|
||||||
FileParser fp = new FileParser("doc/examples/config.json");
|
* Builds a race object for the AC35 course
|
||||||
ArrayList<String> boatNames = new ArrayList<>();
|
*
|
||||||
ArrayList<Map<String, Object>> teams = fp.getTeams();
|
* @return a Race object for the AC35 course
|
||||||
|
*/
|
||||||
|
public static Race createRace(String configFile) throws Exception {
|
||||||
|
Race race = new Race();
|
||||||
|
FileParser fp;
|
||||||
|
|
||||||
//get race size
|
// Read team names from file
|
||||||
int numberOfBoats = (int) fp.getRaceSize();
|
try{
|
||||||
|
fp = new FileParser(configFile);
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException e){
|
||||||
|
System.out.println("Config file does not exist");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
//get time scale
|
ArrayList<String> boatNames = new ArrayList<>();
|
||||||
double timeScale = fp.getTimeScale();
|
ArrayList<Map<String, Object>> teams = fp.getTeams();
|
||||||
race.setTimeScale(timeScale);
|
|
||||||
|
|
||||||
for (Map<String, Object> team : teams) {
|
//get race size
|
||||||
boatNames.add((String) team.get("team-name"));
|
int numberOfBoats = (int) fp.getRaceSize();
|
||||||
}
|
|
||||||
|
|
||||||
// Shuffle team names
|
//get time scale
|
||||||
long seed = System.nanoTime();
|
double timeScale = fp.getTimeScale();
|
||||||
Collections.shuffle(boatNames, new Random(seed));
|
race.setTimeScale(timeScale);
|
||||||
|
|
||||||
if (numberOfBoats > Array.getLength(boatNames.toArray())){
|
for (Map<String, Object> team : teams) {
|
||||||
return null;
|
boatNames.add((String) team.get("team-name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < numberOfBoats; i++) {
|
// Shuffle team names
|
||||||
race.addBoat(new Boat(boatNames.get(i), (Double)(teams.get(i).get("velocity"))));
|
long seed = System.nanoTime();
|
||||||
}
|
Collections.shuffle(boatNames, new Random(seed));
|
||||||
|
|
||||||
race.addLeg(new Leg(35, 100, "Start"));
|
if (numberOfBoats > Array.getLength(boatNames.toArray())) {
|
||||||
race.addLeg(new Leg(10, 300, "Marker 1"));
|
return null;
|
||||||
race.addLeg(new Leg(350, 400, "Leeward Gate"));
|
}
|
||||||
race.addLeg(new Leg(10, 400, "Windward Gate"));
|
|
||||||
|
|
||||||
Leg finishingLeg = new Leg(10, 400, "Leeward Gate");
|
// Add boats to the race
|
||||||
finishingLeg.setFinishingLeg(true);
|
for (int i = 0; i < numberOfBoats; i++) {
|
||||||
|
race.addBoat(new Boat(boatNames.get(i), (Double) (teams.get(i).get("velocity"))));
|
||||||
|
}
|
||||||
|
|
||||||
race.addLeg(finishingLeg);
|
race.addLeg(new Leg(35, 100, "Start"));
|
||||||
|
race.addLeg(new Leg(10, 300, "Marker 1"));
|
||||||
|
race.addLeg(new Leg(350, 400, "Leeward Gate"));
|
||||||
|
race.addLeg(new Leg(10, 400, "Windward Gate"));
|
||||||
|
|
||||||
return race;
|
Leg finishingLeg = new Leg(10, 400, "Leeward Gate");
|
||||||
}
|
finishingLeg.setFinishingLeg(true);
|
||||||
|
|
||||||
public static void main( String[] args )
|
race.addLeg(finishingLeg);
|
||||||
{
|
|
||||||
Race race = null;
|
|
||||||
|
|
||||||
try{
|
return race;
|
||||||
race = createRace();
|
}
|
||||||
}
|
|
||||||
catch (Exception e){
|
|
||||||
System.out.println(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If race was created
|
public static void main(String[] args) {
|
||||||
if (race != null){
|
Race race = null;
|
||||||
race.displayStartingBoats();
|
String raceConfigFile;
|
||||||
|
|
||||||
System.out.println("\n\n");
|
if (args.length == 2 && args[0].equals("-f")){
|
||||||
System.out.println("######################");
|
raceConfigFile = args[1];
|
||||||
System.out.println("# Live Race Updates ");
|
}
|
||||||
System.out.println("######################");
|
else{
|
||||||
race.startRace();
|
// Use default config
|
||||||
|
raceConfigFile = "doc/examples/config.json";
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("\n\n");
|
try {
|
||||||
System.out.println("######################");
|
race = createRace(raceConfigFile);
|
||||||
System.out.println("# Race Results ");
|
} catch (Exception e) {
|
||||||
System.out.println("######################");
|
System.out.println("There was an error creating the race.");
|
||||||
race.showRaceMarkerResults();
|
}
|
||||||
race.displayFinishingOrder();
|
|
||||||
}
|
// If race was created
|
||||||
else{
|
if (race != null) {
|
||||||
System.out.println("There was an error creating the race.");
|
race.displayStartingBoats();
|
||||||
}
|
|
||||||
|
System.out.println("\n\n");
|
||||||
|
System.out.println("######################");
|
||||||
|
System.out.println("# Live Race Updates ");
|
||||||
|
System.out.println("######################");
|
||||||
|
|
||||||
|
race.startRace();
|
||||||
|
|
||||||
|
System.out.println("\n\n");
|
||||||
|
System.out.println("######################");
|
||||||
|
System.out.println("# Race Results ");
|
||||||
|
System.out.println("######################");
|
||||||
|
|
||||||
|
race.showRaceMarkerResults();
|
||||||
|
race.displayFinishingOrder();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
System.out.println("There was an error creating the race. Exiting.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,56 +1,62 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a boat in the race.
|
* 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
|
public class Boat {
|
||||||
{
|
|
||||||
|
|
||||||
private String teamName; // The name of the team, this is also the name of the boat
|
private String teamName; // The name of the team, this is also the name of the boat
|
||||||
private double velocity; // In meters/second
|
private double velocity; // In meters/second
|
||||||
|
|
||||||
public Boat(String teamName) {
|
public Boat(String teamName) {
|
||||||
this.teamName = teamName;
|
this.teamName = teamName;
|
||||||
this.velocity = 10; // Default velocity
|
this.velocity = 10; // Default velocity
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boat(String teamName, double boatVelocity) {
|
/**
|
||||||
this.teamName = teamName;
|
* Represents a boat in the race.
|
||||||
this.velocity = boatVelocity;
|
*
|
||||||
}
|
* @param teamName The name of the team sailing the boat
|
||||||
|
* @param boatVelocity The speed of the boat in meters/second
|
||||||
|
*/
|
||||||
|
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 sailing the boat
|
||||||
* @return The name of the team
|
*
|
||||||
*/
|
* @return The name of the team
|
||||||
public String getTeamName(){
|
*/
|
||||||
return this.teamName;
|
public String getTeamName() {
|
||||||
}
|
return this.teamName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the name of the team sailing the boat
|
* Sets the name of the team sailing the boat
|
||||||
* @param teamName The name of the team
|
*
|
||||||
*/
|
* @param teamName The name of the team
|
||||||
public void setTeamName(String teamName){
|
*/
|
||||||
this.teamName = teamName;
|
public void setTeamName(String teamName) {
|
||||||
}
|
this.teamName = teamName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets velocity of the boat
|
* Gets velocity of the boat
|
||||||
* @param velocity The velocity of boat
|
*
|
||||||
*/
|
* @return a float number of the boat velocity
|
||||||
public void setVelocity(float velocity) {
|
*/
|
||||||
this.velocity = velocity;
|
public double getVelocity() {
|
||||||
}
|
return this.velocity;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets velocity of the boat
|
* Sets velocity of the boat
|
||||||
* @return a float number of the boat velocity
|
*
|
||||||
*/
|
* @param velocity The velocity of boat
|
||||||
public double getVelocity() {
|
*/
|
||||||
return this.velocity;
|
public void setVelocity(double velocity) {
|
||||||
}
|
this.velocity = velocity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,116 +4,137 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event class containing the time of specific event, related team/boat, and
|
* Event class containing the time of specific event, related team/boat, and
|
||||||
* event location such as leg.
|
* 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 {
|
public class Event {
|
||||||
|
|
||||||
private long time;
|
private long time; // Time the event occurs
|
||||||
private Boat boat;
|
private Boat boat;
|
||||||
private Leg leg;
|
private Leg leg; // Leg of the race the event occurs on
|
||||||
private boolean isFinishingEvent = false;
|
private boolean isFinishingEvent = false; // This event occurs when a boat finishes the race
|
||||||
|
|
||||||
public Event(long eventTime, Boat eventBoat, Leg eventLeg) {
|
/**
|
||||||
this.time = eventTime;
|
* Event class containing the time of specific event, related team/boat, and
|
||||||
this.boat = eventBoat;
|
* event location such as leg.
|
||||||
this.leg = eventLeg;
|
*
|
||||||
}
|
* @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 Event(long eventTime, Boat eventBoat, Leg eventLeg) {
|
||||||
|
this.time = eventTime;
|
||||||
|
this.boat = eventBoat;
|
||||||
|
this.leg = eventLeg;
|
||||||
|
}
|
||||||
|
|
||||||
public Event(long eventTime, Boat eventBoat, Leg eventLeg, boolean isFinishingEvent) {
|
/**
|
||||||
this.time = eventTime;
|
* Event class containing the time of specific event, related team/boat, and
|
||||||
this.boat = eventBoat;
|
* event location such as leg.
|
||||||
this.leg = eventLeg;
|
*
|
||||||
this.isFinishingEvent = isFinishingEvent;
|
* @param eventTime, what time the event happens
|
||||||
}
|
* @param eventBoat, the boat that the event belongs to
|
||||||
|
* @param eventLeg, the leg the event happens on
|
||||||
|
* @param isFinishingEvent, true if this event is the boat crossing the finishing line
|
||||||
|
*/
|
||||||
|
public Event(long eventTime, Boat eventBoat, Leg eventLeg, boolean isFinishingEvent) {
|
||||||
|
this.time = eventTime;
|
||||||
|
this.boat = eventBoat;
|
||||||
|
this.leg = eventLeg;
|
||||||
|
this.isFinishingEvent = isFinishingEvent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the time for the event
|
* Gets the time for the event
|
||||||
* @param eventTime the time for event in millisecond
|
*
|
||||||
*/
|
* @return the time for event in millisecond
|
||||||
public void setTime(long eventTime) {
|
*/
|
||||||
this.time = eventTime;
|
public long getTime() {
|
||||||
}
|
return this.time;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the time for the event
|
* Sets the time for the event
|
||||||
* @return the time for event in millisecond
|
*
|
||||||
*/
|
* @param eventTime the time for event in millisecond
|
||||||
public long getTime() {
|
*/
|
||||||
return this.time;
|
public void setTime(long eventTime) {
|
||||||
}
|
this.time = eventTime;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the time in a formatted string
|
* Gets the time in a formatted string
|
||||||
* @return the string of time
|
*
|
||||||
*/
|
* @return the string of time
|
||||||
public String getTimeString() {
|
*/
|
||||||
return (new SimpleDateFormat("mm:ss:SSS")).format(new Date(time));
|
public String getTimeString() {
|
||||||
}
|
return (new SimpleDateFormat("mm:ss:SSS")).format(new Date(time));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the involved boat
|
* Gets the involved boat
|
||||||
* @param eventBoat the involved boat
|
*
|
||||||
*/
|
* @return the boat involved in the event
|
||||||
public void setBoat(Boat eventBoat) {
|
*/
|
||||||
this.boat = eventBoat;
|
public Boat getBoat() {
|
||||||
}
|
return this.boat;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the involved boat
|
* Sets the involved boat
|
||||||
* @return the boat involved in the event
|
*
|
||||||
*/
|
* @param eventBoat the involved boat
|
||||||
public Boat getBoat() {
|
*/
|
||||||
return this.boat;
|
public void setBoat(Boat eventBoat) {
|
||||||
}
|
this.boat = eventBoat;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the involved location/leg
|
* Gets the involved location/leg
|
||||||
* @param eventLeg the involved leg
|
*
|
||||||
*/
|
* @return the leg involved in the event
|
||||||
public void setLeg(Leg eventLeg) {
|
*/
|
||||||
this.leg = eventLeg;
|
public Leg getLeg() {
|
||||||
}
|
return this.leg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the involved location/leg
|
* Sets the involved location/leg
|
||||||
* @return the leg involved in the event
|
*
|
||||||
*/
|
* @param eventLeg the involved leg
|
||||||
public Leg getLeg() {
|
*/
|
||||||
return this.leg;
|
public void setLeg(Leg eventLeg) {
|
||||||
}
|
this.leg = eventLeg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call when the boat reaches the marker, this will tell the marker the order
|
* Called when the boat in this event passes
|
||||||
* in which boats pass it
|
* the marker.
|
||||||
*/
|
*/
|
||||||
public void addBoatToMarker(){
|
public void boatPassedMarker() {
|
||||||
this.leg.addBoatToMarker(boat);
|
this.leg.addBoatToMarker(boat);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this event is the boat finishing the race
|
* Returns true if this event is the boat finishing the race
|
||||||
*
|
*/
|
||||||
*/
|
public boolean getIsFinishingEvent() {
|
||||||
public boolean getIsFinishingEvent(){
|
return this.isFinishingEvent;
|
||||||
return this.isFinishingEvent;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a string that contains the timestamp and course information for this event
|
* 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
|
*
|
||||||
*/
|
* @return A string that details what happened in this event
|
||||||
public String getEventString(){
|
*/
|
||||||
String currentHeading = Integer.toString(this.getLeg().getHeading());
|
public String getEventString() {
|
||||||
|
String currentHeading = Integer.toString(this.getLeg().getHeading());
|
||||||
|
|
||||||
if (this.isFinishingEvent){
|
// This event is a boat finishing the race
|
||||||
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " finished the race");
|
if (this.isFinishingEvent) {
|
||||||
}
|
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " finished the race");
|
||||||
|
}
|
||||||
|
|
||||||
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " passed " + this.getLeg().getMarkerLabel() + " going heading " + currentHeading + "°");
|
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " passed " + this.getLeg().getMarkerLabel() + " going heading " + currentHeading + "°");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import org.json.simple.JSONObject;
|
|||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -18,108 +18,115 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class FileParser {
|
public class FileParser {
|
||||||
|
|
||||||
private String filePath;
|
private String filePath;
|
||||||
private JSONObject content;
|
private JSONObject content;
|
||||||
/** used to construct an instance of file parser
|
|
||||||
*
|
|
||||||
* @param filePath a string like path to show location of desired file to
|
|
||||||
* be parsed
|
|
||||||
*/
|
|
||||||
public FileParser(String filePath) throws Exception {
|
|
||||||
this.filePath = filePath;
|
|
||||||
this.readFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads content from a given file, and return the content as JSONObject.
|
* used to construct an instance of file parser
|
||||||
* Throws FileNotFoundException, if the given file cannot be found.
|
*
|
||||||
*/
|
* @param filePath a string like path to show location of desired file to
|
||||||
private void readFile() throws FileNotFoundException{
|
* be parsed
|
||||||
JSONParser parser = new JSONParser();
|
*/
|
||||||
try {
|
public FileParser(String filePath) throws Exception {
|
||||||
this.content = (JSONObject) parser.parse(new FileReader(filePath));
|
this.filePath = filePath;
|
||||||
|
this.readFile();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
/**
|
||||||
throw e;
|
* Reads content from a given file, and return the content as JSONObject.
|
||||||
} catch (IOException e) {
|
* Throws FileNotFoundException, if the given file cannot be found.
|
||||||
e.printStackTrace();
|
*/
|
||||||
} catch (ParseException e) {
|
private void readFile() throws FileNotFoundException {
|
||||||
e.printStackTrace();
|
JSONParser parser = new JSONParser();
|
||||||
}
|
try {
|
||||||
}
|
this.content = (JSONObject) parser.parse(new FileReader(filePath));
|
||||||
|
|
||||||
/**
|
} catch (FileNotFoundException e) {
|
||||||
* Gets time scale setting parameter.
|
throw e;
|
||||||
* @return long time scale. -1 if parameter is invalid (eg. scale is
|
} catch (IOException e) {
|
||||||
* negative number, or containing non numeric character) or cannot be found.
|
e.printStackTrace();
|
||||||
*/
|
} catch (ParseException e) {
|
||||||
@SuppressWarnings("unchecked")
|
e.printStackTrace();
|
||||||
public double getTimeScale() {
|
}
|
||||||
try {
|
}
|
||||||
double timeScale = (double) this.content.get("time-scale");
|
|
||||||
return timeScale >= 0 ? timeScale : -1;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets race name in the setting file.
|
* Gets time scale setting parameter.
|
||||||
* @return a string of race name. null if race name is invalid or cannot
|
*
|
||||||
* be found.
|
* @return long time scale. -1 if parameter is invalid (eg. scale is
|
||||||
*/
|
* negative number, or containing non numeric character) or cannot be found.
|
||||||
@SuppressWarnings("unchecked")
|
*/
|
||||||
public String getRaceName() {
|
@SuppressWarnings("unchecked")
|
||||||
try {
|
public double getTimeScale() {
|
||||||
return (String) this.content.get("race-name");
|
try {
|
||||||
} catch (Exception e) {
|
double timeScale = (double) this.content.get("time-scale");
|
||||||
return null;
|
return timeScale >= 0 ? timeScale : -1;
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an array of teams who participate the race.
|
* Gets race name in the setting file.
|
||||||
* @return an ArrayList containing strings of team names. null if teams
|
*
|
||||||
* setting is invalid or there is no team.
|
* @return a string of race name. null if race name is invalid or cannot
|
||||||
*/
|
* be found.
|
||||||
@SuppressWarnings("unchecked")
|
*/
|
||||||
public ArrayList<Map<String, Object>> getTeams() {
|
@SuppressWarnings("unchecked")
|
||||||
try {
|
public String getRaceName() {
|
||||||
return (ArrayList<Map<String, Object>>) this.content.get("teams");
|
try {
|
||||||
} catch (Exception e) {
|
return (String) this.content.get("race-name");
|
||||||
return null;
|
} catch (Exception e) {
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the total number of teams.
|
* Gets an array of teams who participate the race.
|
||||||
* @return the number of teams. 0 if no teams or anything goes wrong.
|
*
|
||||||
*/
|
* @return an ArrayList containing strings of team names. null if teams
|
||||||
@SuppressWarnings("unchecked")
|
* setting is invalid or there is no team.
|
||||||
public long getTotalNumberOfTeams() {
|
*/
|
||||||
ArrayList<Map<String, Object>> teams = getTeams();
|
@SuppressWarnings("unchecked")
|
||||||
try {
|
public ArrayList<Map<String, Object>> getTeams() {
|
||||||
return teams.size();
|
try {
|
||||||
} catch (Exception e) {
|
return (ArrayList<Map<String, Object>>) this.content.get("teams");
|
||||||
return 0;
|
} catch (Exception e) {
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number of boats that would compete during a race. Returns the
|
* Gets the total number of teams.
|
||||||
* total number of race size if parameter is invalid or cannot be found.
|
*
|
||||||
* @return an int of the race size.
|
* @return the number of teams. 0 if no teams or anything goes wrong.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public long getRaceSize() {
|
public long getTotalNumberOfTeams() {
|
||||||
long totalTeams = this.getTotalNumberOfTeams();
|
ArrayList<Map<String, Object>> teams = getTeams();
|
||||||
try {
|
try {
|
||||||
long raceSize = (long) this.content.get("race-size");
|
return teams.size();
|
||||||
return raceSize >= 0 && raceSize <= totalTeams? raceSize : totalTeams;
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
return 0;
|
||||||
e.printStackTrace();
|
}
|
||||||
return totalTeams;
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* Gets the number of boats that would compete during a race. Returns the
|
||||||
|
* total number of race size if parameter is invalid or cannot be found.
|
||||||
|
*
|
||||||
|
* @return an int of the race size.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public long getRaceSize() {
|
||||||
|
long totalTeams = this.getTotalNumberOfTeams();
|
||||||
|
try {
|
||||||
|
long raceSize = (long) this.content.get("race-size");
|
||||||
|
return raceSize >= 0 && raceSize <= totalTeams ? raceSize : totalTeams;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return totalTeams;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,108 +1,112 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the leg of a race.
|
||||||
|
*/
|
||||||
public class Leg {
|
public class Leg {
|
||||||
private int heading;
|
private int heading;
|
||||||
private int distance;
|
private int distance;
|
||||||
private boolean isFinishingLeg;
|
private boolean isFinishingLeg;
|
||||||
private Marker startingMarker;
|
private Marker startingMarker;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Create a new leg
|
* Create a new leg
|
||||||
|
*
|
||||||
|
* @param heading, the magnetic heading of this leg
|
||||||
|
* @param distance, the total distance of this leg in meters
|
||||||
|
* @param marker, the marker this leg starts on
|
||||||
|
*/
|
||||||
|
public Leg(int heading, int distance, Marker marker) {
|
||||||
|
this.heading = heading;
|
||||||
|
this.distance = distance;
|
||||||
|
this.startingMarker = marker;
|
||||||
|
this.isFinishingLeg = false;
|
||||||
|
}
|
||||||
|
|
||||||
@param heading, the magnetic heading of this leg
|
/**
|
||||||
@param distance, the total distance of this leg in meters
|
* Create a new leg
|
||||||
@param marker, the marker this leg starts on
|
*
|
||||||
*/
|
* @param heading, the magnetic heading of this leg
|
||||||
public Leg(int heading, int distance, Marker marker){
|
* @param distance, the total distance of this leg in meters
|
||||||
this.heading = heading;
|
* @param markerName, the name of the marker this leg starts on
|
||||||
this.distance = distance;
|
*/
|
||||||
this.startingMarker = marker;
|
public Leg(int heading, int distance, String markerName) {
|
||||||
this.isFinishingLeg = false;
|
this.heading = heading;
|
||||||
}
|
this.distance = distance;
|
||||||
|
this.startingMarker = new Marker(markerName);
|
||||||
|
this.isFinishingLeg = false;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Create a new leg
|
* Get the heading of this leg
|
||||||
|
*/
|
||||||
|
public int getHeading() {
|
||||||
|
return this.heading;
|
||||||
|
}
|
||||||
|
|
||||||
@param heading, the magnetic heading of this leg
|
/**
|
||||||
@param distance, the total distance of this leg in meters
|
* Set the heading for this leg
|
||||||
@param markerName, the name of the marker this leg starts on
|
*/
|
||||||
*/
|
public void setHeading(int heading) {
|
||||||
public Leg(int heading, int distance, String markerName){
|
this.heading = heading;
|
||||||
this.heading = heading;
|
}
|
||||||
this.distance = distance;
|
|
||||||
this.startingMarker = new Marker(markerName);
|
|
||||||
this.isFinishingLeg = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Set the heading for this leg
|
* Get the total distance of this leg in meters
|
||||||
*/
|
*/
|
||||||
public void setHeading(int heading){
|
public int getDistance() {
|
||||||
this.heading = heading;
|
return this.distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Get the heading of this leg
|
* Set the distance of this leg in meters
|
||||||
*/
|
*/
|
||||||
public int getHeading(){
|
public void setDistance(int distance) {
|
||||||
return this.heading;
|
this.distance = distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Set the distance of this leg in meters
|
* Returns the marker this leg started on
|
||||||
*/
|
*/
|
||||||
public void setDistance(int distance){
|
public Marker getMarker() {
|
||||||
this.distance = distance;
|
return this.startingMarker;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Get the total distance of this leg in meters
|
* Set the marker this leg starts on
|
||||||
*/
|
*/
|
||||||
public int getDistance(){
|
public void setMarker(Marker marker) {
|
||||||
return this.distance;
|
this.startingMarker = marker;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Set the marker this leg starts on
|
* Returns the name of the marker this leg started on
|
||||||
*/
|
*/
|
||||||
public void setMarker(Marker marker){
|
public String getMarkerLabel() {
|
||||||
this.startingMarker = marker;
|
return this.startingMarker.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Returns the marker this leg started on
|
* Tell the marker that the boat has passed it
|
||||||
*/
|
*/
|
||||||
public Marker getMarker(){
|
public void addBoatToMarker(Boat boat) {
|
||||||
return this.startingMarker;
|
this.startingMarker.addBoat(boat);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Returns the name of the marker this leg started on
|
* Specify whether or not the race finishes on this leg
|
||||||
*/
|
*
|
||||||
public String getMarkerLabel(){
|
* @param isFinishingLeg whether or not the race finishes on this leg
|
||||||
return this.startingMarker.getName();
|
*/
|
||||||
}
|
public void setFinishingLeg(boolean isFinishingLeg) {
|
||||||
|
this.isFinishingLeg = isFinishingLeg;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Tell the marker that the boat has passed it
|
* Returns whether or not the race finishes after this leg
|
||||||
*/
|
* @return true if this the race finishes after this leg
|
||||||
public void addBoatToMarker(Boat boat){
|
*/
|
||||||
this.startingMarker.addBoat(boat);
|
public boolean getIsFinishingLeg() {
|
||||||
}
|
return this.isFinishingLeg;
|
||||||
|
}
|
||||||
/*
|
|
||||||
Specify whether or not the race finishes on this leg
|
|
||||||
|
|
||||||
@param isFinishingLeg whether or not the race finishes on this leg
|
|
||||||
*/
|
|
||||||
public void setFinishingLeg(boolean isFinishingLeg){
|
|
||||||
this.isFinishingLeg = isFinishingLeg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
@returns true if this the race finishes after this leg
|
|
||||||
*/
|
|
||||||
public boolean getIsFinishingLeg(){
|
|
||||||
return this.isFinishingLeg;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,9 +2,12 @@ package seng302;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the marker at the beginning of a leg
|
||||||
|
*/
|
||||||
class Marker{
|
class Marker{
|
||||||
private String name;
|
private String name;
|
||||||
private ArrayList<Boat> boatOrder;
|
private ArrayList<Boat> boatOrder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the marker at the beginning of a leg
|
* Represents the marker at the beginning of a leg
|
||||||
|
|||||||
+211
-208
@@ -1,246 +1,249 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Race class containing the boats and legs in the race
|
||||||
|
*/
|
||||||
public class Race {
|
public class Race {
|
||||||
private ArrayList<Boat> boats; // The boats in the race
|
private ArrayList<Boat> boats; // The boats in the race
|
||||||
private ArrayList<Leg> legs; // The legs in the race
|
private ArrayList<Leg> legs; // The legs in the race
|
||||||
private ArrayList<Boat> finishingOrder; // The order in which the boats finish the race
|
private ArrayList<Boat> finishingOrder; // The order in which the boats finish the race
|
||||||
private PriorityQueue<Event> events; // The events that occur in the race
|
private PriorityQueue<Event> events; // The events that occur in the race
|
||||||
private int numberOfBoats = 0;
|
private int numberOfBoats = 0;
|
||||||
private long startTime = 0;
|
private long startTime = 0;
|
||||||
private double timeScale = 1;
|
private double timeScale = 1;
|
||||||
|
|
||||||
public Race() {
|
/**
|
||||||
this.boats = new ArrayList<Boat>();
|
* Race class containing the boats and legs in the race
|
||||||
this.legs = new ArrayList<Leg>();
|
*/
|
||||||
this.finishingOrder = new ArrayList<Boat>();
|
public Race() {
|
||||||
|
this.boats = new ArrayList<Boat>();
|
||||||
|
this.legs = new ArrayList<Leg>();
|
||||||
|
this.finishingOrder = new ArrayList<Boat>();
|
||||||
|
|
||||||
// create a priority queue with a custom Comparator to order events
|
// create a priority queue with a custom Comparator to order events
|
||||||
this.events = new PriorityQueue<Event>(new Comparator<Event>() {
|
this.events = new PriorityQueue<Event>(new Comparator<Event>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Event o1, Event o2) {
|
public int compare(Event o1, Event o2) {
|
||||||
Long time1 = o1.getTime();
|
Long time1 = o1.getTime();
|
||||||
Long time2 = o2.getTime();
|
Long time2 = o2.getTime();
|
||||||
|
|
||||||
// order event asc. by time. if tie appears, then order team
|
// order event asc. by time. if tie appears, then order team
|
||||||
// name alphabetically.
|
// name alphabetically.
|
||||||
if (time1 != time2) {
|
if (time1 != time2) {
|
||||||
return time1.compareTo(time2);
|
return time1.compareTo(time2);
|
||||||
} else {
|
} else {
|
||||||
return o1.getBoat().getTeamName().compareTo(o2.getBoat().getTeamName());
|
return o1.getBoat().getTeamName().compareTo(o2.getBoat().getTeamName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Add a boat to the race
|
* Add a boat to the race
|
||||||
@param boat, the boat to add
|
*
|
||||||
*/
|
* @param boat, the boat to add
|
||||||
public void addBoat(Boat boat) {
|
*/
|
||||||
boats.add(boat);
|
public void addBoat(Boat boat) {
|
||||||
numberOfBoats += 1;
|
boats.add(boat);
|
||||||
}
|
numberOfBoats += 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Returns a list of boats in a random order
|
* Returns a list of boats in a random order
|
||||||
|
*
|
||||||
|
* @returns a list of boats
|
||||||
|
*/
|
||||||
|
public Boat[] getShuffledBoats() {
|
||||||
|
// Shuffle the list of boats
|
||||||
|
long seed = System.nanoTime();
|
||||||
|
Collections.shuffle(this.boats, new Random(seed));
|
||||||
|
|
||||||
@returns a list of boats
|
return boats.toArray(new Boat[boats.size()]);
|
||||||
*/
|
}
|
||||||
public Boat[] getShuffledBoats() {
|
|
||||||
// Shuffle the list of boats
|
|
||||||
long seed = System.nanoTime();
|
|
||||||
Collections.shuffle(this.boats, new Random(seed));
|
|
||||||
|
|
||||||
return boats.toArray(new Boat[boats.size()]);
|
/**
|
||||||
}
|
* Returns a list of boats in the order that they
|
||||||
|
* finished the race (position 0 is first place)
|
||||||
|
*
|
||||||
|
* @returns a list of boats
|
||||||
|
*/
|
||||||
|
public Boat[] getFinishedBoats() {
|
||||||
|
return this.finishingOrder.toArray(new Boat[this.finishingOrder.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Returns a list of boats in the order that they
|
* Returns the number of boats in the race
|
||||||
finished the race (position 0 is first place)
|
*
|
||||||
|
* @returns the number of boats in the race
|
||||||
|
*/
|
||||||
|
public int getNumberOfBoats() {
|
||||||
|
return numberOfBoats;
|
||||||
|
}
|
||||||
|
|
||||||
@returns a list of boats
|
/**
|
||||||
*/
|
* Returns a list of boats in the race
|
||||||
public Boat[] getFinishedBoats() {
|
*
|
||||||
return this.finishingOrder.toArray(new Boat[this.finishingOrder.size()]);
|
* @return a list of the boats competing in the race
|
||||||
}
|
*/
|
||||||
|
public Boat[] getBoats() {
|
||||||
|
return boats.toArray(new Boat[boats.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Returns the number of boats in the race
|
* Prints the order in which the boats finished the race
|
||||||
|
*/
|
||||||
|
public void displayFinishingOrder() {
|
||||||
|
int numberOfBoats = this.getNumberOfBoats();
|
||||||
|
Boat[] boats = this.getFinishedBoats();
|
||||||
|
|
||||||
@returns the number of boats in the race
|
System.out.println("--- Finishing Order ---");
|
||||||
*/
|
|
||||||
public int getNumberOfBoats() {
|
|
||||||
return numberOfBoats;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
for (int i = 0; i < Array.getLength(boats); i++) {
|
||||||
Returns a list of boats in the race
|
System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@returns a list of the boats competing in the race
|
/**
|
||||||
*/
|
* Prints the list of boats competing in the race
|
||||||
public Boat[] getBoats() {
|
*/
|
||||||
return boats.toArray(new Boat[boats.size()]);
|
public void displayStartingBoats() {
|
||||||
}
|
int numberOfBoats = this.getNumberOfBoats();
|
||||||
|
Boat[] boats = this.getBoats();
|
||||||
|
|
||||||
/*
|
System.out.println("######################");
|
||||||
Prints the order in which the boats finished
|
System.out.println("# Competing Boats ");
|
||||||
*/
|
System.out.println("######################");
|
||||||
public void displayFinishingOrder() {
|
|
||||||
int numberOfBoats = this.getNumberOfBoats();
|
|
||||||
Boat[] boats = this.getFinishedBoats();
|
|
||||||
|
|
||||||
System.out.println("\n\n");
|
for (int i = 0; i < numberOfBoats; i++) {
|
||||||
System.out.println("--- Finishing Order ---");
|
String velocityKnots = String.format("%1.2f", boats[i].getVelocity() * 1.943844492);
|
||||||
|
|
||||||
for (int i = 0; i < Array.getLength(boats); i++) {
|
System.out.println(boats[i].getTeamName() + " Velocity: " + velocityKnots + " Knots/s");
|
||||||
System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Prints the list of boats competing in the race
|
* Adds a leg to the race
|
||||||
*/
|
*
|
||||||
public void displayStartingBoats() {
|
* @param leg, the leg to add to the race
|
||||||
int numberOfBoats = this.getNumberOfBoats();
|
*/
|
||||||
Boat[] boats = this.getBoats();
|
public void addLeg(Leg leg) {
|
||||||
|
this.legs.add(leg);
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("######################");
|
/**
|
||||||
System.out.println("# Competing Boats ");
|
* Gets legs array
|
||||||
System.out.println("######################");
|
*
|
||||||
|
* @return an array of legs
|
||||||
|
*/
|
||||||
|
public ArrayList<Leg> getLegs() {
|
||||||
|
return this.legs;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < numberOfBoats; i++) {
|
/**
|
||||||
String velocityKnots = String.format("%1.2f", boats[i].getVelocity() * 1.943844492);
|
* Sets time scale
|
||||||
|
*
|
||||||
|
* @param timeScale
|
||||||
|
*/
|
||||||
|
public void setTimeScale(double timeScale) {
|
||||||
|
this.timeScale = timeScale;
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println(boats[i].getTeamName() + " Velocity: " + velocityKnots + " knots.");
|
/**
|
||||||
}
|
* Generate all events that will happen during the race.
|
||||||
}
|
*/
|
||||||
|
private void generateEvents() {
|
||||||
|
//calculate the time every boat passes each leg, and create an event
|
||||||
|
for (Boat boat : this.boats) {
|
||||||
|
long totalDistance = 0;
|
||||||
|
for (Leg leg : this.legs) {
|
||||||
|
long time = (long) (1000 * totalDistance / boat.getVelocity());
|
||||||
|
Event event = new Event(time, boat, leg);
|
||||||
|
events.add(event);
|
||||||
|
totalDistance += leg.getDistance();
|
||||||
|
|
||||||
/*
|
// If finishing leg, add another event for when the boat finishes the race
|
||||||
Adds a leg to the race
|
if (leg.getIsFinishingLeg()) {
|
||||||
|
time = (long) (1000 * totalDistance / boat.getVelocity());
|
||||||
|
event = new Event(time, boat, leg, true);
|
||||||
|
events.add(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@param leg, the leg to add to the race
|
/**
|
||||||
*/
|
* Calculates how far a boat has travelled in meters
|
||||||
public void addLeg(Leg leg) {
|
*
|
||||||
this.legs.add(leg);
|
* @param velocity the velocity of boat
|
||||||
}
|
* @return a float number of distance the boat has been travelled
|
||||||
|
*/
|
||||||
|
public float getDistanceTravelled(long velocity) {
|
||||||
|
long timeDiff = System.currentTimeMillis() - this.startTime;
|
||||||
|
long timeElapse = (long) (timeDiff / 1000 * this.timeScale);
|
||||||
|
return timeElapse * velocity;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets legs array
|
* Iterate over events in the race and print the
|
||||||
*
|
* event string for each event
|
||||||
* @return an array of legs
|
*/
|
||||||
*/
|
public void iterateEvents() {
|
||||||
public ArrayList<Leg> getLegs() {
|
// iterates all events. ends when no event in events.
|
||||||
return this.legs;
|
while (!events.isEmpty()) {
|
||||||
}
|
Event peekEvent = events.peek();
|
||||||
|
long currentTime = (long) ((System.currentTimeMillis() - this.startTime) * this.timeScale);
|
||||||
|
|
||||||
/**
|
if (currentTime > peekEvent.getTime()) {
|
||||||
* Sets time scale
|
Event nextEvent = events.poll();
|
||||||
* @param timeScale
|
|
||||||
*/
|
|
||||||
public void setTimeScale(double timeScale) {
|
|
||||||
this.timeScale = timeScale;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Display a summary of the event
|
||||||
* Temporary method used to generated all the events.
|
System.out.println(nextEvent.getEventString());
|
||||||
*/
|
nextEvent.boatPassedMarker();
|
||||||
private void generateEvents() {
|
|
||||||
|
|
||||||
//calculate the time for every boat passes each leg, and create an event
|
// If event is a boat finishing the race
|
||||||
for (Boat boat : this.boats) {
|
if (nextEvent.getIsFinishingEvent()) {
|
||||||
long totalDistance = 0;
|
this.finishingOrder.add(nextEvent.getBoat());
|
||||||
for (Leg leg : this.legs) {
|
}
|
||||||
long time = (long) (1000 * totalDistance / boat.getVelocity());
|
}
|
||||||
Event event = new Event(time, boat, leg);
|
|
||||||
events.add(event);
|
|
||||||
totalDistance += leg.getDistance();
|
|
||||||
|
|
||||||
// If finishing leg, add another event for when the boat finishes the race
|
// Wait for 100ms to throttle the while loop
|
||||||
if (leg.getIsFinishingLeg()){
|
try {
|
||||||
time = (long) (1000 * totalDistance / boat.getVelocity());
|
Thread.sleep(100);
|
||||||
event = new Event(time, boat, leg, true);
|
} catch (java.lang.InterruptedException e) {
|
||||||
events.add(event);
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Note: this function is useless so far
|
* Start the race and print each marker with the order
|
||||||
* Calculates how far a boat has travelled in meter
|
* in which the boats passed that marker
|
||||||
*
|
*/
|
||||||
* @param velocity the velocity of boat
|
public void startRace() {
|
||||||
* @return a float number of distance the boat has been travelled
|
// record start time.
|
||||||
*/
|
generateEvents();
|
||||||
public float getDistanceTravelled(long velocity) {
|
this.startTime = System.currentTimeMillis();
|
||||||
long timeDiff = System.currentTimeMillis() - this.startTime;
|
iterateEvents();
|
||||||
long timeElapse = (long) (timeDiff / 1000 * this.timeScale);
|
}
|
||||||
return timeElapse * velocity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterate over events in the race and print the
|
* Print the order in which the boats passed each marker
|
||||||
* event string for each event
|
*/
|
||||||
*/
|
public void showRaceMarkerResults() {
|
||||||
public void iterateEvents() {
|
for (Leg leg : this.legs) {
|
||||||
// iterates all events. ends when no event in events.
|
Boat[] boats = leg.getMarker().getBoats();
|
||||||
while (!events.isEmpty()) {
|
|
||||||
Event peekEvent = events.peek();
|
|
||||||
long currentTime = (long) ((System.currentTimeMillis() - this.startTime) * this.timeScale);
|
|
||||||
|
|
||||||
if (currentTime > peekEvent.getTime()) {
|
System.out.println("--- " + leg.getMarkerLabel() + " ---");
|
||||||
// pull out the event
|
|
||||||
Event nextEvent = events.poll();
|
|
||||||
|
|
||||||
// I just simply print it out for testing
|
// Print the order in which the boats passed the marker
|
||||||
System.out.println(nextEvent.getEventString());
|
for (int i = 0; i < this.getNumberOfBoats(); i++) {
|
||||||
nextEvent.addBoatToMarker();
|
System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
|
||||||
|
}
|
||||||
|
|
||||||
if (nextEvent.getIsFinishingEvent()){
|
System.out.println("");
|
||||||
this.finishingOrder.add(nextEvent.getBoat());
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for 100ms to slow down the while loop
|
|
||||||
try{
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
catch(java.lang.InterruptedException e){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Start the race and print each marker with the order
|
|
||||||
in which the boats passed that marker
|
|
||||||
*/
|
|
||||||
public void startRace() {
|
|
||||||
// record start time.
|
|
||||||
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 = leg.getMarker().getBoats();
|
|
||||||
|
|
||||||
System.out.println("--- " + leg.getMarkerLabel() + " ---");
|
|
||||||
|
|
||||||
// Print the order in which the boats passed the marker
|
|
||||||
for (int i = 0; i < this.getNumberOfBoats(); i++) {
|
|
||||||
System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,15 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
public class AppTest
|
public class AppTest {
|
||||||
{
|
|
||||||
@Test
|
@Test
|
||||||
public void testApp()
|
public void testApp() {
|
||||||
{
|
assertTrue(true);
|
||||||
assertTrue( true );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,34 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for the Team class.
|
* Unit test for the Team class.
|
||||||
*/
|
*/
|
||||||
public class BoatTest
|
public class BoatTest {
|
||||||
{
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBoatCreation()
|
public void testBoatCreation() {
|
||||||
{
|
|
||||||
Boat boat1 = new Boat("Team 1");
|
Boat boat1 = new Boat("Team 1");
|
||||||
assertEquals(boat1.getTeamName(), "Team 1");
|
assertEquals(boat1.getTeamName(), "Team 1");
|
||||||
|
assertEquals(boat1.getVelocity(), (double) 10.0, 1e-15);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testChangeTeamName()
|
public void testChangeTeamName() {
|
||||||
{
|
Boat boat1 = new Boat("Team 1");
|
||||||
Boat boat1 = new Boat("Team 1");
|
boat1.setTeamName("Team 2");
|
||||||
boat1.setTeamName("Team 2");
|
assertEquals(boat1.getTeamName(), "Team 2");
|
||||||
assertEquals(boat1.getTeamName(), "Team 2");
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetVelocity() {
|
||||||
|
Boat boat1 = new Boat("Team 1", 29.0);
|
||||||
|
assertEquals(boat1.getVelocity(), (double) 29.0, 1e-15);
|
||||||
|
|
||||||
|
boat1.setVelocity(12.0);
|
||||||
|
assertEquals(boat1.getVelocity(), (double)12.0, 1e-15);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Event class
|
* Test for Event class
|
||||||
@@ -11,11 +10,31 @@ import static org.junit.Assert.*;
|
|||||||
*/
|
*/
|
||||||
public class EventTest {
|
public class EventTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTimeString() throws Exception {
|
public void getTimeString() throws Exception {
|
||||||
Leg leg = new Leg(035, 100, "Start");
|
Leg leg = new Leg(35, 100, "Start");
|
||||||
Boat boat = new Boat("testBoat");
|
Boat boat = new Boat("testBoat");
|
||||||
Event event = new Event(1231242, boat, leg);
|
Event event = new Event(1231242, boat, leg);
|
||||||
assertEquals("20:31:242", event.getTimeString());
|
assertEquals("20:31:242", event.getTimeString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ensure all boats are added as they pass the marker
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void boatOrderTest() throws Exception {
|
||||||
|
Leg leg = new Leg(35, 100, "1");
|
||||||
|
|
||||||
|
Boat boat1 = new Boat("testBoat");
|
||||||
|
Boat boat2 = new Boat("testBoat2");
|
||||||
|
|
||||||
|
Event event1 = new Event(1231242, boat1, leg);
|
||||||
|
Event event2 = new Event(1231242, boat2, leg);
|
||||||
|
|
||||||
|
event1.boatPassedMarker();
|
||||||
|
event2.boatPassedMarker();
|
||||||
|
|
||||||
|
assertEquals(event1.getLeg().getMarker().getBoats()[0].getTeamName(), "testBoat");
|
||||||
|
assertEquals(event2.getLeg().getMarker().getBoats()[1].getTeamName(), "testBoat2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,50 +3,50 @@ package seng302;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/** Unit test for FileParser class
|
/**
|
||||||
|
* Unit test for FileParser class
|
||||||
* Created by Haoming on 5/03/17.
|
* Created by Haoming on 5/03/17.
|
||||||
*/
|
*/
|
||||||
public class FileParserTest {
|
public class FileParserTest {
|
||||||
|
|
||||||
/*
|
/**
|
||||||
test if it fails from reading non existed file
|
* test if it fails from reading non existed file
|
||||||
*/
|
*/
|
||||||
@Test (expected = FileNotFoundException.class)
|
@Test(expected = FileNotFoundException.class)
|
||||||
public void readNonExistedFile() throws Exception {
|
public void readNonExistedFile() throws Exception {
|
||||||
FileParser fileParser = new FileParser("test/java/seng302/non-existed.json");
|
FileParser fileParser = new FileParser("test/java/seng302/non-existed.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
test a valid json file with valid content.
|
* test a valid json file with valid content.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void readValidFile() throws Exception{
|
public void readValidFile() throws Exception {
|
||||||
FileParser fileParser = new FileParser("src/test/java/seng302/valid.json");
|
FileParser fileParser = new FileParser("src/test/java/seng302/valid.json");
|
||||||
|
|
||||||
assertEquals("AC35", fileParser.getRaceName());
|
assertEquals("AC35", fileParser.getRaceName());
|
||||||
|
|
||||||
assertEquals("Oracle Team USA", fileParser.getTeams().get(0).get("team-name"));
|
assertEquals("Oracle Team USA", fileParser.getTeams().get(0).get("team-name"));
|
||||||
assertEquals(20.9, fileParser.getTeams().get(0).get("velocity"));
|
assertEquals(20.9, fileParser.getTeams().get(0).get("velocity"));
|
||||||
assertEquals(2, fileParser.getRaceSize());
|
assertEquals(2, fileParser.getRaceSize());
|
||||||
assertEquals(6, fileParser.getTotalNumberOfTeams());
|
assertEquals(6, fileParser.getTotalNumberOfTeams());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
test an invalid json file within wrong type value and misnamed
|
* test an invalid json file within wrong type value and misnamed
|
||||||
variable name.
|
* variable name.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void readInvalidFile() throws Exception {
|
public void readInvalidFile() throws Exception {
|
||||||
FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json");
|
FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json");
|
||||||
|
|
||||||
assertEquals(null, fileParser.getRaceName());
|
assertEquals(null, fileParser.getRaceName());
|
||||||
assertEquals(null, fileParser.getTeams());
|
assertEquals(null, fileParser.getTeams());
|
||||||
//assertEquals(-1, fileParser.getTimeScale());
|
//assertEquals(-1, fileParser.getTimeScale());
|
||||||
assertEquals(null,fileParser.getTeams());
|
assertEquals(null, fileParser.getTeams());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,20 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for the Leg class.
|
* Unit test for the Leg class.
|
||||||
*/
|
*/
|
||||||
public class LegTest{
|
public class LegTest {
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Test creation of the leg by specifying a string
|
* Test creation of the leg by specifying a string
|
||||||
for the marker label
|
* for the marker label
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testLegCreationUsingMarkerLabel()
|
public void testLegCreationUsingMarkerLabel() {
|
||||||
{
|
|
||||||
Leg leg = new Leg(010, 100, "Marker");
|
Leg leg = new Leg(010, 100, "Marker");
|
||||||
|
|
||||||
assertEquals(leg.getHeading(), 010);
|
assertEquals(leg.getHeading(), 010);
|
||||||
@@ -24,13 +23,12 @@ public class LegTest{
|
|||||||
assertEquals(leg.getIsFinishingLeg(), false);
|
assertEquals(leg.getIsFinishingLeg(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Test creation of the leg by providing a
|
* Test creation of the leg by providing a
|
||||||
Marker object
|
* Marker object
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testLegCreation()
|
public void testLegCreation() {
|
||||||
{
|
|
||||||
Leg leg = new Leg(010, 100, new Marker("Marker"));
|
Leg leg = new Leg(010, 100, new Marker("Marker"));
|
||||||
|
|
||||||
assertEquals(leg.getHeading(), 010);
|
assertEquals(leg.getHeading(), 010);
|
||||||
@@ -39,13 +37,12 @@ public class LegTest{
|
|||||||
assertEquals(leg.getIsFinishingLeg(), false);
|
assertEquals(leg.getIsFinishingLeg(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Test changing whether or not a
|
* Test changing whether or not a
|
||||||
leg is the finishing leg
|
* leg is the finishing leg
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testSetFinishLeg()
|
public void testSetFinishLeg() {
|
||||||
{
|
|
||||||
Leg leg = new Leg(010, 100, "Marker");
|
Leg leg = new Leg(010, 100, "Marker");
|
||||||
|
|
||||||
leg.setFinishingLeg(true);
|
leg.setFinishingLeg(true);
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
package seng302;
|
package seng302;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for the Race class.
|
* Unit test for the Race class.
|
||||||
*/
|
*/
|
||||||
public class RaceTest
|
public class RaceTest {
|
||||||
{
|
/**
|
||||||
/*
|
* Test that all boats were added to the race
|
||||||
Test that all boats were added to the race
|
*/
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddingBoatsToRace(){
|
public void testAddingBoatsToRace() {
|
||||||
Boat boat1 = new Boat("Team 1");
|
Boat boat1 = new Boat("Team 1");
|
||||||
Boat boat2 = new Boat("Team 2");
|
Boat boat2 = new Boat("Team 2");
|
||||||
|
|
||||||
@@ -24,4 +24,16 @@ public class RaceTest
|
|||||||
|
|
||||||
assertEquals(Array.getLength(race.getBoats()), 2);
|
assertEquals(Array.getLength(race.getBoats()), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetShuffledBoats(){
|
||||||
|
Boat boat1 = new Boat("Team 1");
|
||||||
|
Boat boat2 = new Boat("Team 2");
|
||||||
|
|
||||||
|
Race race = new Race();
|
||||||
|
race.addBoat(boat1);
|
||||||
|
race.addBoat(boat2);
|
||||||
|
|
||||||
|
assertEquals(Array.getLength(race.getShuffledBoats()), 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
{
|
{
|
||||||
"time-scale": "abc",
|
"time-scale": "abc",
|
||||||
"race-name": 123,
|
"race-name": 123,
|
||||||
"teams-with-wrong-name":["team1","team2","team3"]
|
"teams-with-wrong-name": [
|
||||||
|
"team1",
|
||||||
|
"team2",
|
||||||
|
"team3"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,29 @@
|
|||||||
"time-scale": 1,
|
"time-scale": 1,
|
||||||
"race-size": 2,
|
"race-size": 2,
|
||||||
"teams": [
|
"teams": [
|
||||||
{"team-name": "Oracle Team USA", "velocity": 20.9},
|
{
|
||||||
{"team-name": "Artemis Racing", "velocity": 18.3},
|
"team-name": "Oracle Team USA",
|
||||||
{"team-name": "Emirates Team New Zealand", "velocity": 21.5},
|
"velocity": 20.9
|
||||||
{"team-name": "Groupama Team France","velocity": 19.9},
|
},
|
||||||
{"team-name": "Land Rover BAR", "velocity": 17.6},
|
{
|
||||||
{"team-name": "SoftBank Team Japan", "velocity": 16.6}
|
"team-name": "Artemis Racing",
|
||||||
|
"velocity": 18.3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Emirates Team New Zealand",
|
||||||
|
"velocity": 21.5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Groupama Team France",
|
||||||
|
"velocity": 19.9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "Land Rover BAR",
|
||||||
|
"velocity": 17.6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"team-name": "SoftBank Team Japan",
|
||||||
|
"velocity": 16.6
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user