-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.java
More file actions
executable file
·245 lines (220 loc) · 8.92 KB
/
Simulator.java
File metadata and controls
executable file
·245 lines (220 loc) · 8.92 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.Color;
/**
* A simple predator-prey simulator, based on a rectangular field
* containing various mythological animals.
*
* @author Saathveekan Satheshkumar, Manik Aggarwal, David J. Barnes and Michael Kölling
* @version 2022.03.02 (2)
*/
public class Simulator {
// 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 werewolf will be created in any given grid position.
private static final double WEREWOLF_CREATION_PROBABILITY = 0.03;
// The probability that a jackalope will be created in any given grid position.
private static final double JACKALOPE_CREATION_PROBABILITY = 0.2;
// The probability that a griffon will be created in any given grid position.
private static final double GRIFFON_CREATION_PROBABILITY = 0.03;
// The probability that a pegasus will be created in any given grid position.
private static final double PEGASUS_CREATION_PROBABILITY = 0.08;
// The probability that a unicorn will be created in any given grid position.
private static final double UNICORN_CREATION_PROBABILITY = 0.05;
// The probability that a cyclops will be created in any given grid position.
private static final double CYCLOPS_CREATION_PROBABILITY = 0.03;
// The probability that a mandrake will be created in any given grid position.
private static final double MANDRAKE_CREATION_PROBABILITY = 0.3;
// The probability that a hyacinth will be created in any given grid position.
private static final double HYACINTH_CREATION_PROBABILITY = 0.3;
// List of animals in the field.
private List<Animal> animals;
// The current state of the field.
private Field field;
// The current time of day.
private boolean isDay;
//The current weather event.
private String currentWeather;
// The current step of the simulation.
private int step;
// A graphical view of the simulation.
private SimulatorView view;
// Simulates the weather.
private Weather weather = new Weather();
/**
* Construct a simulation field with default size.
*/
public Simulator() {
this(DEFAULT_DEPTH, DEFAULT_WIDTH);
}
/**
* Create a simulation field with the given size.
*
* @param depth Depth of the field. Must be greater than zero.
* @param width Width of the field. Must be greater than zero.
*/
public Simulator(int depth, int width) {
if (width <= 0 || depth <= 0) {
System.out.println("The dimensions must be greater than zero.");
System.out.println("Using default values.");
depth = DEFAULT_DEPTH;
width = DEFAULT_WIDTH;
}
animals = new ArrayList<>();
field = new Field(depth, width);
// Create a view of the state of each location in the field.
view = new SimulatorView(depth, width);
view.setColor(Hyacinth.class, new Color(0, 150, 2));
view.setColor(Mandrake.class, new Color(0, 73, 1));
view.setColor(Jackalope.class, new Color(79, 73, 204));
view.setColor(Werewolf.class, new Color(94, 94, 94, 255));
view.setColor(Griffon.class, new Color(255, 204, 0));
view.setColor(Unicorn.class, new Color(167, 4, 248));
view.setColor(Pegasus.class, new Color(0, 0, 0));
view.setColor(Cyclops.class, new Color(253, 0, 0));
// Setup a valid starting point.
reset();
}
/**
* Run the simulation from its current state for a reasonably long period,
* (4000 steps).
*/
public void runLongSimulation() {
simulate(4000);
}
/**
* Run the simulation from its current state 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 tally = 1; tally <= numSteps && view.isViable(field); tally++) {
simulateOneStep();
delay(100); // uncomment this to run m
}
}
/**
* Run the simulation from its current state for a single step.
* Iterate over the whole field updating the state of each
* animal.
*/
public void simulateOneStep() {
step++;
// Changes the time of day.
changesDay();
// Updates the time of day for the weather class.
weather.timeOfDay(isDay);
// Sets a new weather for each step.
currentWeather = weather.setWeather();
// Provide space for newborn animals.
List<Animal> newAnimals = new ArrayList<>();
// Let all animals act.
for (Iterator<Animal> it = animals.iterator(); it.hasNext(); ) {
Animal animal = it.next();
animal.act(newAnimals, isDay, currentWeather);
if (!animal.isAlive()) {
it.remove();
}
}
// Add the newly born animals to the main lists.
animals.addAll(newAnimals);
view.showStatus(currentWeather, step, field, isDayOrNight());
}
/**
* Reset the simulation to a starting position.
*/
public void reset() {
step = 0;
isDay = true;
animals.clear();
populate();
// Show the starting state in the view.
view.showStatus(currentWeather, step, field, isDayOrNight());
}
/**
* Randomly populate the field with foxes and rabbits.
*/
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() <= WEREWOLF_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Werewolf werewolf = new Werewolf(true, field, location, "Werewolf");
animals.add(werewolf);
} else if (rand.nextDouble() <= JACKALOPE_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Jackalope Jackalope = new Jackalope(true, field, location, "Jackalope");
animals.add(Jackalope);
} else if (rand.nextDouble() <= GRIFFON_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Griffon griffon = new Griffon(true, field, location, "Griffon");
animals.add(griffon);
} else if (rand.nextDouble() <= PEGASUS_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Pegasus pegasus = new Pegasus(true, field, location, "Pegasus");
animals.add(pegasus);
} else if (rand.nextDouble() <= CYCLOPS_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Cyclops cyclops = new Cyclops(true, field, location, "Cyclops");
animals.add(cyclops);
} else if (rand.nextDouble() <= UNICORN_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Unicorn unicorn = new Unicorn(true, field, location, "Unicorn");
animals.add(unicorn);
} else if (rand.nextDouble() <= MANDRAKE_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Mandrake mandrake = new Mandrake(true, field, location, "Mandrake");
animals.add(mandrake);
} else if (rand.nextDouble() <= HYACINTH_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Hyacinth hyacinth = new Hyacinth(true, field, location, "Hyacinth");
animals.add(hyacinth);
}
// 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
}
}
/**
* Changes the time of day.
*/
protected void changesDay() {
isDay = !isDay;
}
/**
* For the GUI, returns the corresponding time of day as a
* String type.
*
* @return the time of day as a String type.
*/
private String isDayOrNight() {
if (isDay) {
return "Day";
} else {
return "Night";
}
}
public static void main(String[] args) {
Simulator sim = new Simulator();
sim.runLongSimulation();
}
}