Implemented list of boats in order of last feature passed.

- Update controller every time boats pass a mark
- Cleared info table to force update
#story [15]
main
cbt24 9 years ago
parent 3751ee2488
commit b25389f212

@ -50,7 +50,8 @@ public class RaceController extends Controller{
private RaceMap map; private RaceMap map;
public void updateInfoTable(Race race) { public void updateInfoTable(Race race) {
boatInfoTable.setItems(FXCollections.observableArrayList(race.getFinishingBoats())); boatInfoTable.getItems().clear();
boatInfoTable.setItems(FXCollections.observableArrayList(race.getStartingBoats()));
boatTeamColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace,String>("Name")); boatTeamColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace,String>("Name"));
boatMarkColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace, String>("CurrentLeg")); boatMarkColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace, String>("CurrentLeg"));
@ -70,10 +71,11 @@ public class RaceController extends Controller{
raceMap.draw(); raceMap.draw();
canvasBase.getChildren().add(raceMap); canvasBase.getChildren().add(raceMap);
BoatInRace boat = new BoatInRace("NZ", 10000); BoatInRace boat = new BoatInRace("NZ", 1000);
BoatInRace[] boats = new BoatInRace[] {boat}; BoatInRace[] boats = new BoatInRace[] {boat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(1, 1), 0); ArrayList<Leg> legs = new ArrayList<>();
Leg[] legs = new Leg[] {leg1}; legs.add(new Leg("Start", 1, new GPSCoordinate(0,0), new GPSCoordinate(1,1), 0));
legs.add(new Leg("Mark", 1, new GPSCoordinate(0,0), new GPSCoordinate(1,1), 1));
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs, this); ConstantVelocityRace race = new ConstantVelocityRace(boats, legs, this);

@ -4,6 +4,7 @@ import seng302.Controllers.RaceController;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
import seng302.GraphCoordinate; import seng302.GraphCoordinate;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -18,11 +19,11 @@ public class ConstantVelocityRace extends Race {
* @see Leg * @see Leg
*/ */
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks, RaceController controller) { public ConstantVelocityRace(BoatInRace[] startingBoats, ArrayList<Leg> marks, RaceController controller) {
super(startingBoats, marks, controller); super(startingBoats, marks, controller);
} }
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks) { public ConstantVelocityRace(BoatInRace[] startingBoats, ArrayList<Leg> marks) {
super(startingBoats, marks); super(startingBoats, marks);
} }

@ -12,9 +12,9 @@ import java.util.*;
*/ */
public abstract class Race implements Runnable { public abstract class Race implements Runnable {
protected BoatInRace[] startingBoats; protected BoatInRace[] startingBoats;
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>(); protected ArrayList<Leg> legs;
protected Leg[] legs;
protected RaceController controller; protected RaceController controller;
protected int boatsFinished = 0;
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
@ -23,22 +23,21 @@ public abstract class Race implements Runnable {
* @param boats Takes in an array of boats that are participating in the 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 marks Number of marks in order that the boats pass in order to complete the race.
*/ */
public Race(BoatInRace[] boats, Leg[] marks, RaceController controller) { public Race(BoatInRace[] boats, ArrayList<Leg> marks, RaceController controller) {
this.startingBoats = boats; this.startingBoats = boats;
this.legs = marks; this.legs = marks;
this.legs.add(new Leg("Finish"));
this.controller = controller; this.controller = controller;
} }
public Race(BoatInRace[] boats, Leg[] marks) { public Race(BoatInRace[] boats, ArrayList<Leg> marks) {
this.startingBoats = boats; this(boats, marks, null);
this.legs = marks;
} }
public void run() { public void run() {
long time = System.currentTimeMillis(); updateController();
preRace(); preRace();
simulateRace(); simulateRace();
if(controller != null) controller.updateInfoTable(this);
} }
private void preRace() { private void preRace() {
@ -47,7 +46,7 @@ public abstract class Race implements Runnable {
System.out.println("===================="); System.out.println("====================");
for (int i = 0; i < startingBoats.length; i++) { 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"); System.out.println(i + 1 + ". " + startingBoats[i].getName() + ", Speed: " + Math.round(startingBoats[i].getVelocity() * 1.94384) + "kn");
startingBoats[i].setCurrentLeg(legs[0]); startingBoats[i].setCurrentLeg(legs.get(0));
} }
} }
@ -64,7 +63,7 @@ public abstract class Race implements Runnable {
long timeLoopStarted; long timeLoopStarted;
long timeLoopEnded; long timeLoopEnded;
while (finishingBoats.size() < startingBoats.length) { while (boatsFinished < startingBoats.length) {
timeLoopStarted = System.currentTimeMillis(); timeLoopStarted = System.currentTimeMillis();
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
for (BoatInRace boat : startingBoats) { for (BoatInRace boat : startingBoats) {
@ -83,27 +82,29 @@ public abstract class Race implements Runnable {
protected void checkPosition(BoatInRace boat, long timeElapsed) { protected void checkPosition(BoatInRace boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){ if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
updateController();
//boat has passed onto new leg //boat has passed onto new leg
if (boat.getCurrentLeg().getLegNumber() == legs.length - 1) { if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished //boat has finished
boat.setTimeFinished(timeElapsed); boat.setTimeFinished(timeElapsed);
boat.setCurrentLeg(new Leg("Finish")); boatsFinished++;
finishingBoats.add(boat);
} else { } else {
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance()); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1]; Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg); boat.setCurrentLeg(nextLeg);
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg()); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
} }
} }
}
protected void updateController() {
if(controller != null) controller.updateInfoTable(this);
} }
public ArrayList<BoatInRace> getFinishingBoats() { public BoatInRace[] getStartingBoats() {
return finishingBoats; return startingBoats;
} }
/** /**

@ -5,6 +5,8 @@ import seng302.Model.BoatInRace;
import seng302.Model.ConstantVelocityRace; import seng302.Model.ConstantVelocityRace;
import seng302.Model.Leg; import seng302.Model.Leg;
import java.util.ArrayList;
/** /**
* Created by esa46 on 15/03/17. * Created by esa46 on 15/03/17.
*/ */
@ -16,8 +18,8 @@ public class RaceTest {
BoatInRace boat = new BoatInRace("NZ", 240); BoatInRace boat = new BoatInRace("NZ", 240);
BoatInRace[] boats = new BoatInRace[] {boat}; BoatInRace[] boats = new BoatInRace[] {boat};
Leg leg1 = new Leg("first leg", 1, new GPSCoordinate(0, 0), new GPSCoordinate(3, 4), 0); ArrayList<Leg> legs = new ArrayList<>();
Leg[] legs = new Leg[] {leg1}; legs.add(new Leg("Start", 1, new GPSCoordinate(0,0), new GPSCoordinate(1,1), 0));
ConstantVelocityRace race = new ConstantVelocityRace(boats, legs); ConstantVelocityRace race = new ConstantVelocityRace(boats, legs);
race.run(); race.run();
} }

Loading…
Cancel
Save