Added simulation of a race with randomised time events. This is reported in time order and shown in the console which is displayed as "Boat passed Mark at 60 (seconds)"
parent
2148c1e06d
commit
4f6ab7b4bc
@ -0,0 +1,46 @@
|
||||
package seng302;
|
||||
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 3/03/17.
|
||||
*/
|
||||
public class Race {
|
||||
Boat[] boats;
|
||||
LinkedList<Event> events = new LinkedList<Event>();
|
||||
|
||||
public Race(Boat[] boats){
|
||||
this.boats = boats;
|
||||
}
|
||||
|
||||
public void simulateRace(){
|
||||
String[] marks = {"Mark", "Leeward Gate", "Windward Gate", "Leeward Gate", "Finish"};
|
||||
randomiseRace(marks);
|
||||
//printRace();
|
||||
for (Event event: events) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask(){
|
||||
@Override
|
||||
public void run(){
|
||||
System.out.println(event);
|
||||
}
|
||||
}, event.getTime() * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
public void randomiseRace(String[] eventNames){
|
||||
Random rand = new Random();
|
||||
for (Boat boat: this.boats){
|
||||
events.add(new Event("Start", boat, 0));
|
||||
int prevTime = 0;
|
||||
for (String markName: eventNames){
|
||||
int time = rand.nextInt(12) + 6;
|
||||
events.add(new Event(markName, boat, prevTime + time));
|
||||
prevTime += time;
|
||||
}
|
||||
}
|
||||
Collections.sort(events, (o1, o2) -> o1.getTime() - o2.getTime());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue