-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
84 lines (67 loc) · 1.71 KB
/
Copy pathSimulation.java
File metadata and controls
84 lines (67 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package controller;
import java.util.LinkedList;
import java.util.List;
import model.Actor;
import model.City;
import model.PassengerSource;
import model.TaxiCompany;
import view.CityGUI;
/**
* Run the simulation by asking a collection of actors to act.
*
* @author Yujie ZHENG, Su YU
* @version 2017.03.15
*/
public class Simulation
{
private static List<Actor> actors;
/**
* Create the initial set of actors for the simulation.
*/
public Simulation()
{
actors = new LinkedList<>();
City city = new City(CityGUI.getCityWidth(),CityGUI.getCityHeight());
TaxiCompany company = new TaxiCompany(city);
PassengerSource source = new PassengerSource(city, company);
actors.addAll(company.getVehicles());
actors.add(source);
actors.add(new CityGUI(city));
}
/**
* Run the simulation for a fixed number of steps.
* Pause after each step to allow the GUI to keep up.
*/
public static void run()
{
for(int i = 0; i< 300; i++) {
step();
wait(100);
}
}
/**
* Take a single step of the simulation.
*/
public static void step()
{
for(Actor actor : actors) {
actor.act();
}
}
/**
* Wait for a specified number of milliseconds before finishing.
* This provides an easy way to cause a small delay.
* @param milliseconds The number of milliseconds to wait.
*/
private static void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
// ignore the exception
}
}
}