Added single test to debug race process
This commit is contained in:
Generated
+2
-15
@@ -28,20 +28,7 @@
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="masterDetails">
|
||||
<states>
|
||||
<state key="ProjectJDKs.UI">
|
||||
<settings>
|
||||
<last-edited>1.8</last-edited>
|
||||
<splitter-proportions>
|
||||
<option name="proportions">
|
||||
<list>
|
||||
<option value="0.2" />
|
||||
</list>
|
||||
</option>
|
||||
</splitter-proportions>
|
||||
</settings>
|
||||
</state>
|
||||
</states>
|
||||
<component name="SvnConfiguration">
|
||||
<configuration>$USER_HOME$/.subversion</configuration>
|
||||
</component>
|
||||
</project>
|
||||
@@ -8,6 +8,9 @@ import javafx.scene.layout.BorderPane;
|
||||
import javafx.stage.Stage;
|
||||
import seng302.Controllers.Controller;
|
||||
import seng302.Controllers.MainController;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.ConstantVelocityRace;
|
||||
import seng302.Model.Leg;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -11,8 +11,14 @@ public class BoatInRace extends Boat {
|
||||
private Leg currentLeg;
|
||||
private double distanceTravelledInLeg;
|
||||
private GPSCoordinate currentPosition;
|
||||
private long timeFinished;
|
||||
|
||||
BoatInRace(String name, double velocity) {
|
||||
public void setTimeFinished(long timeFinished) {
|
||||
this.timeFinished = timeFinished;
|
||||
}
|
||||
|
||||
|
||||
public BoatInRace(String name, double velocity) {
|
||||
super(name, velocity);
|
||||
}
|
||||
|
||||
@@ -37,6 +43,7 @@ public class BoatInRace extends Boat {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the bearing of the travel via map coordinates of the raceMarkers
|
||||
* @return
|
||||
|
||||
@@ -13,19 +13,18 @@ public class ConstantVelocityRace extends Race {
|
||||
* Initialiser for a Race with constant velocity.
|
||||
* @param startingBoats array of boats
|
||||
* @param marks array of RaceMarkers that the boats need to pass in order to finish the course.
|
||||
* @param timescale integer that the race is at timescale = 1000ms
|
||||
* @see Boat
|
||||
* @see Leg
|
||||
*/
|
||||
|
||||
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks, int timescale) {
|
||||
super(startingBoats, marks, timescale);
|
||||
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks) {
|
||||
super(startingBoats, marks);
|
||||
}
|
||||
|
||||
protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
|
||||
|
||||
//distanceTravelled = velocity (nm p hr) * time taken to update loop
|
||||
double distanceTravelled = boat.getVelocity() * TimeUnit.MILLISECONDS.toHours(millisecondsElapsed);
|
||||
double distanceTravelled = boat.getVelocity() * millisecondsElapsed/3600000;
|
||||
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
|
||||
|
||||
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
|
||||
|
||||
@@ -7,7 +7,7 @@ import seng302.GraphCoordinate;
|
||||
* Created by cbt24 on 6/03/17.
|
||||
*/
|
||||
public class Leg {
|
||||
private String name;
|
||||
private String name; //nautical miles
|
||||
private double distance;
|
||||
|
||||
|
||||
@@ -19,10 +19,8 @@ public class Leg {
|
||||
* Leg Initialiser
|
||||
* @param name Name of the Leg
|
||||
* @param distance Total Distance located in the Race
|
||||
* @param latitude Latitude of where it exists in the race (x coordinate)
|
||||
* @param longitude Longitude of where it exists in the race (y coordinate)
|
||||
*/
|
||||
public Leg(String name, double distance, double latitude, double longitude, GPSCoordinate start, GPSCoordinate end, int number) {
|
||||
public Leg(String name, double distance, GPSCoordinate start, GPSCoordinate end, int number) {
|
||||
this.name = name;
|
||||
this.distance = distance;
|
||||
this.startGPSCoordinate = start;
|
||||
|
||||
@@ -11,7 +11,6 @@ public abstract class Race {
|
||||
protected BoatInRace[] startingBoats;
|
||||
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
|
||||
protected Leg[] legs;
|
||||
protected int timescale = 1000;
|
||||
|
||||
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
|
||||
|
||||
@@ -19,26 +18,26 @@ public abstract class Race {
|
||||
* Initailiser for Race
|
||||
* @param boats Takes in an array of boats that are participating in the race.
|
||||
* @param marks Number of marks in order that the boats pass in order to complete the race.
|
||||
* @param timescale Number or milliseconds that = 1000ms.
|
||||
*/
|
||||
public Race(BoatInRace[] boats, Leg[] marks, int timescale) {
|
||||
public Race(BoatInRace[] boats, Leg[] marks) {
|
||||
this.startingBoats = boats;
|
||||
this.legs = marks;
|
||||
this.timescale = timescale;
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
printStartingDetails();
|
||||
preRace();
|
||||
simulateRace();
|
||||
|
||||
}
|
||||
|
||||
private void printStartingDetails() {
|
||||
private void preRace() {
|
||||
//show the boats participating.
|
||||
System.out.println("Boats Participating:");
|
||||
System.out.println("====================");
|
||||
for (int i = 0; i < startingBoats.length; i++) {
|
||||
System.out.println(i + 1 + ". " + startingBoats[i].getName() + ", Speed: " + Math.round(startingBoats[i].getVelocity() * 1.94384) + "kn");
|
||||
startingBoats[i].setCurrentLeg(legs[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ public abstract class Race {
|
||||
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
|
||||
for (BoatInRace boat : startingBoats) {
|
||||
updatePosition(boat, SLEEP_TIME);
|
||||
checkPosition(boat);
|
||||
checkPosition(boat, totalTimeElapsed);
|
||||
}
|
||||
try {
|
||||
timeLoopEnded = System.currentTimeMillis();
|
||||
@@ -73,15 +72,19 @@ public abstract class Race {
|
||||
|
||||
|
||||
|
||||
protected void checkPosition(BoatInRace boat) {
|
||||
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
||||
|
||||
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
|
||||
//boat has passed onto new leg
|
||||
if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) {
|
||||
//boat has finished
|
||||
boat.setTimeFinished(timeElapsed);
|
||||
finishingBoats.add(boat);
|
||||
} else {
|
||||
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
||||
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
|
||||
boat.setCurrentLeg(nextLeg);
|
||||
if (boat.getCurrentLeg().getLegNumber() > legs.length) {
|
||||
//boat has finished
|
||||
finishingBoats.add(boat);
|
||||
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package seng302;
|
||||
|
||||
import org.junit.Test;
|
||||
import seng302.Model.BoatInRace;
|
||||
import seng302.Model.ConstantVelocityRace;
|
||||
import seng302.Model.Leg;
|
||||
|
||||
/**
|
||||
* Created by esa46 on 15/03/17.
|
||||
*/
|
||||
public class RaceTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void singleBoatRaceRunsAndFinishes(){
|
||||
|
||||
BoatInRace boat = new BoatInRace("NZ", 240);
|
||||
BoatInRace[] boats = new BoatInRace[] {boat};
|
||||
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(3, 4), 0);
|
||||
Leg[] legs = new Leg[] {leg1};
|
||||
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
|
||||
race.run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user