Added single test to debug race process
This commit is contained in:
@@ -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
|
||||
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
|
||||
boat.setCurrentLeg(nextLeg);
|
||||
if (boat.getCurrentLeg().getLegNumber() > legs.length) {
|
||||
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);
|
||||
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,6 @@ public class RaceMap {
|
||||
* @see GraphCoordinate
|
||||
*/
|
||||
public GraphCoordinate convertGPS(double lat, double lon) {
|
||||
return new GraphCoordinate((int) (width * (lat - x1) / (x2 - x1)), (int) (height - (height * (lon - y1) / (y2 - y1))));
|
||||
return new GraphCoordinate((int) (width * (lat - x1) / (x2 - x1)), (int) (height - (height * (lon - y1) / (y2 - y1))));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user