-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
128 lines (118 loc) · 4.54 KB
/
Simulator.java
File metadata and controls
128 lines (118 loc) · 4.54 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/** A simple predator-prey simulator, based on a rectangular field containing humans */
public class Simulator implements ActionListener
{
// Constants representing configuration information for the simulation.
// The default width for the grid.
private static final int DEFAULT_WIDTH = 120;
// The default depth of the grid.
private static final int DEFAULT_DEPTH = 80;
// The probability that a human will be created in any given position.
private static final double HUMAN_CREATION_PROBABILITY = 0.055;
// List of humans in the field.
public final List<Human> allHumans;
// The current state of the field.
private final Field field;
// The current step of the simulation.
private int step;
// A graphical view of the simulation.
private final SimulatorView view;
/** Construct a simulation field with default size */
public Simulator() {
allHumans = new ArrayList<>();
field = new Field(DEFAULT_DEPTH, DEFAULT_WIDTH);
// Create a view of the state of each location in the field.
view = new SimulatorView(DEFAULT_DEPTH, DEFAULT_WIDTH);
// Setup a valid starting point.
reset();
// the Action Listeners for the buttons
view.button1.addActionListener(this);
view.button2.addActionListener(this);
view.button3.addActionListener(this);
}
/** Run the simulation from its current state for a reasonably long period (4000 steps) */
public void runLongSimulation() {
simulate(200);
}
/** Run the simulation for the given number of steps.
* Stop before the given number of steps if it ceases to be viable.
* @param numSteps The number of steps to run for */
public void simulate(int numSteps) {
for(int step=1; step <= numSteps; step++) {
simulateOneStep();
//delay(700); // run more slowly
}
}
/** Run the simulation from its current state for a single step. Iterate
* over the whole field updating the state of each fox and rabbit */
public void simulateOneStep() {
step++;
// Provide space for newborn humans.
List<Human> newBorn = new ArrayList<>();
// Let all humans act.
for(Iterator<Human> it = allHumans.iterator(); it.hasNext(); ) {
Human person = it.next();
person.move(newBorn);
if(! person.isAlive()) {
it.remove();
}
}
// Add the newly born humans to the main list.
allHumans.addAll(newBorn);
view.showStatus(step, field);
}
/** Reset the simulation to a starting position. */
public void reset() {
step = 0;
allHumans.clear();
populate();
// Show the starting state in the view.
view.showStatus(step, field);
}
/** Randomly populate the field with humans */
private void populate() {
Random rand = Randomizer.getRandom();
field.clear();
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
if(rand.nextDouble() <= HUMAN_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Human human = new Human(true,true, field, location);
human.setHumanColor();
view.setColor(human);
allHumans.add(human);
}
// else leave the location empty.
}
}
}
/** Pause for a given time.
* @param millisec The time to pause for, in milliseconds */
private void delay(int millisec) {
try {
Thread.sleep(millisec);
}
catch (InterruptedException ie) {
// wake up
}
}
/** Recognise the source of the Action Listener and run the appropriate code for each button
* @param e the action event that indicates which button has been pressed */
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == view.button1){
simulateOneStep();
}
else if(e.getSource() == view.button2){
runLongSimulation();
}
else if(e.getSource() == view.button3){
reset();
}
}
}