Repository navigation
Expand file tree
/
Copy pathSimulator.java
More file actions
executable file
·227 lines (202 loc) · 7.21 KB
/
Copy pathSimulator.java
File metadata and controls
executable file
·227 lines (202 loc) · 7.21 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
import java.util.*;
/**
* A simple predator-prey simulator, based on a rectangular field containing
* rabbits and foxes.
*
* @author David J. Barnes and Michael Kölling
* @version 7.1
*/
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 wolf will be created
private static final double WOLF_CREATION_PROBABILITY = 0.04;
// The probability that a capybara will be created
private static final double CAPYBARA_CREATION_PROBABILITY = 0.1;
// probability that tiger will be created
private static final double TIGER_CREATION_PROBABILITY = 0.06;
//probability that anaconda will be created
private static final double ANACONDA_CREATION_PROBABILITY = 0.03;
//probability that heron will be created
private static final double HERON_CREATION_PROBABILITY = 0.05;
//probability that grass will be created
private static final double GRASS_CREATION_PROBABILITY = 0.2;
private static int hour;
private boolean isNight;
// The current state of the field.
private 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()
{
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 >= zero.");
System.out.println("Using default values.");
depth = DEFAULT_DEPTH;
width = DEFAULT_WIDTH;
}
field = new Field(depth, width);
view = new SimulatorView(depth, width);
reset();
}
/**
* Run the simulation from its current state for a reasonably long
* period (4000 steps).
*/
public void runLongSimulation()
{
simulate(700);
}
/**
* 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)
{
reportStats();
for(int n = 1; n <= numSteps && field.isViable(); n++) {
simulateOneStep();
delay(50); // adjust this to change execution speed
}
}
/**
* 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++;
// Use a separate Field to store the starting state of
// the next step.
Field nextFieldState = new Field(field.getDepth(), field.getWidth());
List<Animal> animals = field.getAnimals();
for (Animal anAnimal : animals) {
anAnimal.act(field, nextFieldState);
}
hour = (hour + 1) % 24;
if (hour>= 18 || hour < 6) {
view.setNight(true);
isNight = true;
}
else {
view.setNight(false);
isNight = false;
}
// Replace the old state with the new one.
field = nextFieldState;
reportStats();
view.showStatus(step, field);
delay(1);
}
public static boolean isNight() {
return hour >= 18 || hour < 6;
}
/**
* Reset the simulation to a starting position.
*/
public void reset()
{
step = 0;
populate();
view.showStatus(step, field);
}
/**
* 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++)
/**
* method was adjusted in a way so that
* some acting species can only spawn
* in even cells and other only in odd cells
*/
{
if (col%2==0) {
// wolfs/tigers/grass spawn in even cells
if (rand.nextDouble() <= WOLF_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Wolf wolf = new Wolf(true, location);
field.placeAnimal(wolf, location);
} else if (rand.nextDouble() <= TIGER_CREATION_PROBABILITY) {
Location location = new Location(row, col);
int random = rand.nextInt(2);
Tiger tiger = new Tiger(true,location);
field.placeAnimal(tiger, location);
}
else if (rand.nextDouble() <= GRASS_CREATION_PROBABILITY) {
Location location = new Location(row, col);
int random = rand.nextInt(2);
Grass grass = new Grass(location);
field.placeAnimal(grass, location);
}
}
else{
//spawns capybaras/anacondas/herons in odd cells
if (rand.nextDouble() <= CAPYBARA_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Capybara capybara = new Capybara(true, location);
field.placeAnimal(capybara, location);
}
else if(rand.nextDouble() <= ANACONDA_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Anaconda anaconda = new Anaconda(true, location);
field.placeAnimal(anaconda, location);
}
else if(rand.nextDouble() <= HERON_CREATION_PROBABILITY) {
Location location = new Location(row, col);
Heron heron = new Heron(true, location);
field.placeAnimal(heron, location);
if (rand.nextDouble() < 0.05) {
heron.infect();
}
}
}
// else leave the location empty.
}
}
}
/**
* Report on the number of each type of animal in the field.
*/
public void reportStats()
{
//System.out.print("Step: " + step + " ");
field.fieldStats();
}
/**
* Pause for a given time.
* @param milliseconds The time to pause for, in milliseconds
*/
private void delay(int milliseconds)
{
try {
Thread.sleep(milliseconds);
}
catch(InterruptedException e) {
// ignore
}
}
}