-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiver.java
More file actions
313 lines (258 loc) · 9.42 KB
/
River.java
File metadata and controls
313 lines (258 loc) · 9.42 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import java.util.Random;
import java.util.ArrayList;
/**
* Models the overall river ecosystem
* in the simulation.
*
* @author Jackson Eshbaugh
* @version 01/28/2024
*/
public class River {
public Animal[] river;
private AnimalArrayList doNotUpdate = new AnimalArrayList();
/**
* Generates a random river ecosystem of
* specified length.
*
* @param length The length of the river.
*/
public River(int length) {
this.river = new Animal[length];
this.fillRiver();
}
/**
* Randomly fills the river with fish,
* bears, and empty (null) spots. To be used
* in generating a new ecosystem.
*/
private void fillRiver() {
// Iterate through each slot in the river array
for (int i = 0; i < river.length; ++i) {
// Randomly decide what to place there
Random r = new Random();
int choice = r.nextInt(3);
switch (choice) {
case 0:
river[i] = null;
break;
case 1:
river[i] = new Bear();
break;
default: // (i.e., choice == 2)
river[i] = new Fish();
break;
}
}
}
/**
* Gets the length of the river.
*
* @return The length of the river
* being modeled.
*/
public int getLength() {
return river.length;
}
/**
* Finds the number of empty cells in the river.
*
* @return The count of cells in the river
* who are set to null
*/
public int numEmpty() {
int count = 0;
for(Animal a : river) {
if(a == null)
count++;
}
return count;
}
/**
* Attempts to add a new animal of age 0
* and of random gender, who is the same type as
* the specified animal to a random empty cell.
*
* Helper method for {@link #processMove(int, int)} to fulfil
* rule 4 in section 1.1 of the project specification.
*
* @return True if the operation is successful, or false if
* there aren't any empty cells.
*/
public boolean addRandom(Animal animal) {
if(numEmpty() == 0)
return false;
// Select the random cell to add the animal to
Random r = new Random();
int index = r.nextInt(numEmpty());
// The index selected above takes into account only
// cells who are currently null. In order to find the
// referenced cell, iterate through the array and subtract
// from the index whenever a cell is null, until index == 0.
for (int i = 0; i < river.length; ++i) {
if(river[i] == null) {
if(index == 0) {
// This is the cell that was selected.
// Now, find the type of the animal and
// create a new instance of one in this cell.
if(animal instanceof Fish) {
river[i] = new Fish(0,
Animal.generateRandomGender());
} else { // (i.e., animal instanceof Bear)
river[i] = new Bear(0,
Animal.generateRandomGender());
}
// Don't process a move for this animal until
// the next cycle.
doNotUpdate.add(river[i]);
break;
} else index--;
}
}
return true;
}
/**
* Updates one specific cell of the river.
* If the cell {@code river[i]} is null, no action is taken.
* Otherwise:
* <ol>
* <li>Checks if the animal should be updated (i.e., if the animal already moved this cycle). If not, no further action is taken.</li>
* <li>Checks the animal's age. If it's at the end of its lifespan, it dies.</li>
* <li>If the animal is still alive, decides if the animal will move—and if so, in which direction.</li>
* <li>Calls {@link #processMove(int, int)} to process the move taken by the animal and any consequeces of it.</li>
* </ol>
*
* @param i The index of the cell in the {@code river} array to be updated.
*/
public void updateCell(int i) {
// Ensure there is an animal in this cell.
if(river[i] == null) return;
// Make sure this animal hasn't already been updated.
if(doNotUpdate.find(river[i]) != -1) return;
// Don't let the animal be updated again.
doNotUpdate.add(river[i]);
// Increment the animal's age.
if(!river[i].incrAge()) {
// The animal has reached its lifespan and has died.
river[i] = null;
return;
}
// Choose a move:
Random r = new Random();
int choice = r.nextInt(3);
switch(choice) {
case 0:
// Don't move.
return;
case 1:
// Move left.
// Find the target index, taking care to
// allow for wrap-around in the array.
int target = i - 1;
if(target < 0)
target = river.length - 1;
processMove(i, target);
return;
default:
// (i.e., 2)
// Move right.
// Find the target index, taking care to
// allow for wrap-around in the array.
target = i + 1;
if(target > river.length - 1)
target = 0;
processMove(i, target);
return;
}
}
/**
* Helper method for {@link #updateCell(int)}.
* Moves the Animal in question, processing any
* consequences of these moves as well.
*
* @param i The index of the animal to move (the same parameter as in {@link #updateCell(int)}).
* @param targetIndex The index where the animal wants to move (an index one away from i,
* unless i is at the extrema of the array).
*/
public void processMove(int i, int targetIndex) {
if(river[targetIndex] == null) {
// The cell is empty, move the animal here.
river[targetIndex] = river[i];
river[i] = null;
return;
}
if((river[targetIndex] instanceof Fish) && (river[i] instanceof Bear)) {
// The bear kills the fish, regardless of gender.
river[targetIndex] = river[i];
river[i] = null;
return;
}
if((river[i] instanceof Fish) && (river[targetIndex] instanceof Bear)) {
// The bear once again kills the fish. Effectively, the fish is simply
// removed from the river array.
river[i] = null;
return;
}
// The animals are of the same species.
if(river[i].getGender() != river[targetIndex].getGender()) {
// Two animals of opposite genders have collided. They remain in their spaces
// but a new animal of their type is added to the array (if possible).
this.addRandom(river[i]);
return;
}
// The animals are the same gender.
if((river[i] instanceof Fish) && (river[targetIndex] instanceof Fish)) {
// Fish of the same gender who collide don't do anything.
return;
}
Bear cell = (Bear) river[i];
Bear target = (Bear) river[targetIndex];
if(cell.getStrength() > target.getStrength()) {
// This bear kills the target bear.
river[targetIndex] = river[i];
river[i] = null;
return;
}
if(cell.getStrength() < target.getStrength()) {
// The target bear kills this bear.
river[i] = null;
return;
}
// Bears of the same gender and strength
// simply remain in their spaces, so that case
// isn't handled here.
}
/**
* Performs one cycle of the simulation
* by calling {@link #updateCell(int)}
* on every cell in the {@code river} array.
*/
public void updateRiver() {
for(int i = 0; i < river.length; ++i) {
updateCell(i);
}
// Clear the doNotUpdate ArrayList, prepare
// for the next cycle.
doNotUpdate.clear();
}
/**
* Produces a string representation
* of the river, using "---" to represent
* empty cells and {@link Animal#toString()} for
* animals.
*
* @return The string representation of the river.
*/
@Override
public String toString() {
String s = "";
for(Animal a : river) {
if(a == null)
s += "--- ";
else
s += a.toString() + " ";
}
// Get rid of the trailing space
s = s.substring(0, s.length() - 1);
return s;
}
}