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)"
This commit is contained in:
@@ -2,11 +2,16 @@ package seng302;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
LinkedList<Integer> test = new LinkedList<>();
|
||||
test.add(5);
|
||||
test.add(1, 8);
|
||||
|
||||
Boat boat1 = new Boat("ORACLE TEAM USA");
|
||||
Boat boat2 = new Boat("Artemis Racing");
|
||||
Boat boat3 = new Boat("Emirates Team New Zealand");
|
||||
@@ -16,16 +21,8 @@ public class App
|
||||
Boat[] all_boats = {boat1, boat2, boat3, boat4, boat5, boat6};
|
||||
Boat[] boats = {all_boats[0], all_boats[1]};
|
||||
|
||||
ArrayList<Event> events = new ArrayList<Event>();
|
||||
events.add(new Event("Start", boats));
|
||||
events.add(new Event("Mark", boats));
|
||||
events.add(new Event("Leeward Gate", boats));
|
||||
events.add(new Event("Windward Gate", boats));
|
||||
events.add(new Event("Leeward Gate", boats));
|
||||
events.add(new Event("Finish", boats));
|
||||
Race race = new Race(boats);
|
||||
race.simulateRace();
|
||||
|
||||
for(Event event:events) {
|
||||
System.out.println(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package seng302;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 3/03/17.
|
||||
*/
|
||||
|
||||
@@ -9,24 +9,24 @@ import java.util.Collections;
|
||||
*/
|
||||
public class Event {
|
||||
private String name;
|
||||
private ArrayList<Boat> boats;
|
||||
private Boat boat;
|
||||
private int time;
|
||||
|
||||
public Event(String name, Boat boats[]){
|
||||
public Event(String name, Boat boat, int time){
|
||||
this.name = name;
|
||||
this.boats = new ArrayList<>(Arrays.asList(boats));
|
||||
this.boat = boat;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
if(this.name != "Start") {
|
||||
Collections.shuffle(this.boats);
|
||||
}
|
||||
public void setTime(int time){
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public int getTime(){
|
||||
return this.time;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String event = name + "\n\n";
|
||||
|
||||
for(int i = 0; i < boats.size(); i++) {
|
||||
event += i+1 + ". " + boats.get(i).getName()+"\n";
|
||||
}
|
||||
|
||||
return event;
|
||||
return boat.getName() + " passed " + name + " at " + time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user