Added function to generate events for race

- added generateEvents() for creating event into a priority queue
- added iterateEvents() to pull out events when time passing
- changed default velocity to 70 just for testing. Pls change it back after testing

#story[5] #implement
This commit is contained in:
Haoming Yin
2017-03-07 21:30:48 +13:00
parent 17fbb1212d
commit 78dea7daac
2 changed files with 77 additions and 23 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ public class Boat
{ {
// The name of the team, this is also the name of the boat // The name of the team, this is also the name of the boat
private String teamName = null; private String teamName = null;
private float velocity = 0; private float velocity = 70; // please set this one to a reasonable num!!!!!, i set it just for testing ;)
public Boat(String teamName) { public Boat(String teamName) {
this.teamName = teamName; this.teamName = teamName;
+76 -22
View File
@@ -1,28 +1,41 @@
package seng302; package seng302;
import java.util.ArrayList; import java.util.*;
import java.util.Random;
import java.util.Collections;
import java.util.List;
public class Race { public class Race {
private ArrayList<Boat> boats; private ArrayList<Boat> boats;
private ArrayList<Leg> legs; private ArrayList<Leg> legs;
private PriorityQueue<Event> events;
private int numberOfBoats = 0; private int numberOfBoats = 0;
private long startTime = 0; private long startTime = 0;
private int timeScale = 1; private int timeScale = 1;
public Race(){ public Race() {
boats = new ArrayList<Boat>(); this.boats = new ArrayList<Boat>();
legs = new ArrayList<Leg>(); this.legs = new ArrayList<Leg>();
// create a priority queue within custom Comparator to order events
this.events = new PriorityQueue<Event>(new Comparator<Event>() {
@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) {
return time1.compareTo(time2);
} else {
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){ public void addBoat(Boat boat) {
boats.add(boat); boats.add(boat);
numberOfBoats += 1; numberOfBoats += 1;
} }
@@ -32,7 +45,7 @@ public class Race {
@returns a list of boats @returns a list of boats
*/ */
public Boat[] getShuffledBoats(){ public Boat[] getShuffledBoats() {
// Shuffle the list of boats // Shuffle the list of boats
long seed = System.nanoTime(); long seed = System.nanoTime();
Collections.shuffle(this.boats, new Random(seed)); Collections.shuffle(this.boats, new Random(seed));
@@ -46,7 +59,7 @@ public class Race {
@returns a list of boats @returns a list of boats
*/ */
public Boat[] getFinishedBoats(){ public Boat[] getFinishedBoats() {
return getShuffledBoats(); return getShuffledBoats();
} }
@@ -55,7 +68,7 @@ public class Race {
@returns the number of boats in the race @returns the number of boats in the race
*/ */
public int getNumberOfBoats(){ public int getNumberOfBoats() {
return numberOfBoats; return numberOfBoats;
} }
@@ -64,28 +77,28 @@ public class Race {
@returns a list of the boats competing in the race @returns a list of the boats competing in the race
*/ */
public Boat[] getBoats(){ public Boat[] getBoats() {
return boats.toArray(new Boat[boats.size()]); return boats.toArray(new Boat[boats.size()]);
} }
/* /*
Prints the order in which the boats finished Prints the order in which the boats finished
*/ */
public void displayFinishingOrder(){ public void displayFinishingOrder() {
int numberOfBoats = this.getNumberOfBoats(); int numberOfBoats = this.getNumberOfBoats();
Boat[] boats = this.getFinishedBoats(); Boat[] boats = this.getFinishedBoats();
System.out.println("--- Finishing Order ---"); System.out.println("--- Finishing Order ---");
for (int i = 0; i < numberOfBoats; i++) { for (int i = 0; i < numberOfBoats; i++) {
System.out.println("#" + Integer.toString(i+1) + " - " + boats[i].getTeamName()); System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
} }
} }
/* /*
Prints the list of boats competing in the race Prints the list of boats competing in the race
*/ */
private void displayStartingBoats(){ private void displayStartingBoats() {
int numberOfBoats = this.getNumberOfBoats(); int numberOfBoats = this.getNumberOfBoats();
Boat[] boats = this.getBoats(); Boat[] boats = this.getBoats();
@@ -101,12 +114,13 @@ public class Race {
@param leg, the leg to add to the race @param leg, the leg to add to the race
*/ */
public void addLeg(Leg leg){ public void addLeg(Leg leg) {
this.legs.add(leg); this.legs.add(leg);
} }
/** /**
* Gets legs array * Gets legs array
*
* @return an array of legs * @return an array of legs
*/ */
public ArrayList<Leg> getLegs() { public ArrayList<Leg> getLegs() {
@@ -114,7 +128,26 @@ public class Race {
} }
/** /**
* Temporary method used to generated all the events.
*/
private void generateEvents() {
//calculate the time for every boat passes each leg, and create an event
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);
}
}
}
/**
* Note: this function is useless so far
* Calculates how far a boat has travelled in meter * Calculates how far a boat has travelled in meter
*
* @param velocity the velocity of boat * @param velocity the velocity of boat
* @return a float number of distance the boat has been travelled * @return a float number of distance the boat has been travelled
*/ */
@@ -124,25 +157,46 @@ public class Race {
return timeElapse * velocity; return timeElapse * velocity;
} }
/**
* Micheal, here is a demo function shows you how to iterate all events
*/
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());
}
}
}
/* /*
Start the race and print each marker with the order Start the race and print each marker with the order
in which the boats passed that marker in which the boats passed that marker
*/ */
public void startRace(){ public void startRace() {
// record start time. // record start time.
generateEvents();
this.startTime = System.currentTimeMillis(); this.startTime = System.currentTimeMillis();
iterateEvents();
for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ for (Leg leg : this.legs) {
Boat[] boats = this.getShuffledBoats(); Boat[] boats = this.getShuffledBoats();
System.out.println("--- " + leg.getMarkerLabel() + " ---"); System.out.println("--- " + leg.getMarkerLabel() + " ---");
// Print the order in which the boats passed the marker // Print the order in which the boats passed the marker
for (int i = 0; i < this.getNumberOfBoats(); i++) { for (int i = 0; i < this.getNumberOfBoats(); i++) {
System.out.println("#" + Integer.toString(i+1) + " - " + boats[i].getTeamName()); System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
} }
System.out.println(""); System.out.println("");
} }
} }
} }