-
Notifications
You must be signed in to change notification settings - Fork 0
/
Critter.java
530 lines (459 loc) · 13.3 KB
/
Critter.java
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package assignment4;
/* CRITTERS Critter.java
* EE422C Project 4 submission by
* Replace <...> with your actual data.
* Jonathan Downing
* jjd2547
* <Student1 5-digit Unique No.>
* <Student2 Name>
* <Student2 EID>
* <Student2 5-digit Unique No.>
* Slip days used: <0>
* Spring 2018
*/
import java.util.Iterator;
import java.lang.reflect.Constructor;
import java.util.List;
/* see the PDF for descriptions of the methods and fields in this class
* you may add fields, methods or inner classes to Critter ONLY if you make your additions private
* no new public, protected or default-package code or data can be added to Critter
*/
public abstract class Critter {
private static String myPackage;
private static List<Critter> population = new java.util.ArrayList<Critter>();
private static List<Critter> babies = new java.util.ArrayList<Critter>();
// Gets the package name. This assumes that Critter and its subclasses are all in the same package.
static {
myPackage = Critter.class.getPackage().toString().split(" ")[1];
}
private static java.util.Random rand = new java.util.Random();
public static int getRandomInt(int max) {
if(max <= 0)
return 0;
else
return rand.nextInt(max);
}
public static void setSeed(long new_seed) {
rand = new java.util.Random(new_seed);
}
/* a one-character long string that visually depicts your critter in the ASCII interface */
public String toString() { return ""; }
private int energy = 0;
protected int getEnergy() { return energy; }
private static boolean Encounter;
private int x_coord;
private int y_coord;
private boolean isAlive;
private boolean moveDone;
private boolean isBaby;
/**
* Walk method, only moves 1 space in 8 different directions.
* @param direction
*/
protected final void walk(int direction) {
if(energy <= 0)
return;
if(moveDone) {
energy = energy - Params.walk_energy_cost;
if(energy <= 0)
isAlive = false;
return;
}
int prevX = x_coord;
int prevY = y_coord;
switch(direction) {
case 0: x_coord = moveX(1); //East
break;
case 1: y_coord = moveY(-1); //NE
x_coord = moveX(1);
break;
case 2: y_coord = moveY(-1); //North
break;
case 3: y_coord = moveY(-1); //NW
x_coord = moveX(-1);
break;
case 4: x_coord = moveX(-1); //West
break;
case 5: x_coord = moveX(-1); //SW
y_coord = moveY(1);
break;
case 6: y_coord = moveY(1); //South
break;
case 7: y_coord = moveY(1); //SE
x_coord = moveX(1);
break;
default: break;
}
if(isBaby)
return;
boolean canMove = true;
if(Critter.Encounter) {
for(Critter y : population) {
if(this.x_coord == y.x_coord && this.y_coord == y.y_coord)
canMove = false;
}
}
if(!canMove) {
x_coord = prevX;
y_coord = prevY;
return;
}
moveDone = true;
energy = energy - Params.walk_energy_cost;
if(energy <= 0)
isAlive = false;
}
/**
* Run method. Moves 2 spaces in 8 different directions.
* @param direction
*/
protected final void run(int direction) {
if(energy <= 0 && !isBaby)
return;
if(moveDone) {
energy = energy - Params.run_energy_cost;
if(energy <= 0)
isAlive = false;
return;
}
int prevX = x_coord;
int prevY = y_coord;
switch(direction) {
case 0: x_coord = moveX(2);
break;
case 1: y_coord = moveY(-2);
x_coord = moveX(2);
break;
case 2: y_coord = moveY(-2);
break;
case 3: y_coord = moveY(-2);
x_coord = moveX(-2);
break;
case 4: x_coord = moveX(-2);
break;
case 5: x_coord = moveX(-2);
y_coord = moveY(2);
break;
case 6: y_coord = moveY(2);
break;
case 7: y_coord = moveY(2);
x_coord = moveX(2);
break;
default: break;
}
boolean canMove = true;
if(Critter.Encounter) {
for(Critter y : population) {
if(this.x_coord == y.x_coord && this.y_coord == y.y_coord)
canMove = false;
}
}
if(!canMove) {
x_coord = prevX;
y_coord = prevY;
return;
}
moveDone = true;
energy = energy - Params.run_energy_cost;
if(energy <= 0)
isAlive = false;
}
/**
* Wraps the x coordinate to ensure that a Critter doesn't move off the map (as the map wraps around).
* Needs some debugging.
* @param distance
* @return int
*/
private int moveX(int distance) {
int moved = 0;
if((distance + x_coord) < 0) {
moved = Params.world_width - distance;
}
else if((Params.world_width - 1) < (x_coord + distance)) {
moved = distance - 1;
}
else
moved = x_coord + distance;
return moved;
}
/**
* Wraps the y coordinate to ensure that a Critter doesn't move off the map (as the map wraps around).
* Needs some debugging.
* @param distance
* @return int
*/
private int moveY(int distance) {
int moved = 0;
if((distance + y_coord) < 0) {
moved = Params.world_height - distance;
}
else if((Params.world_height - 1) < (y_coord + distance)) {
moved = distance - 1;
}
else
moved = y_coord + distance;
return moved;
}
/**
* Uses a newly created offspring Critter object to give it energy and coordinates
* Adds it to babies List
* @param offspring
* @param direction
*/
protected final void reproduce(Critter offspring, int direction) {
if(this.energy < Params.min_reproduce_energy || this.isAlive == false)
return;
offspring.energy = this.energy / 2;
this.energy = offspring.energy;
offspring.isBaby = true;
offspring.x_coord = this.x_coord;
offspring.y_coord = this.y_coord;
offspring.walk(direction);
babies.add(offspring);
}
public abstract void doTimeStep();
public abstract boolean fight(String oponent);
/**
* create and initialize a Critter subclass.
* critter_class_name must be the unqualified name of a concrete subclass of Critter, if not,
* an InvalidCritterException must be thrown.
* (Java weirdness: Exception throwing does not work properly if the parameter has lower-case instead of
* upper. For example, if craig is supplied instead of Craig, an error is thrown instead of
* an Exception.)
* @param critter_class_name
* @throws InvalidCritterException
*/
public static void makeCritter(String critter_class_name) throws InvalidCritterException {
try {
Class<?> critter = Class.forName("assignment4."+ critter_class_name);
Constructor constructor = critter.getConstructor();
Critter newCritter = (Critter) constructor.newInstance();
newCritter.x_coord = Critter.getRandomInt(Params.world_width);
newCritter.y_coord = Critter.getRandomInt(Params.world_height);
newCritter.energy = Params.start_energy;
newCritter.isAlive = true;
population.add(newCritter);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new InvalidCritterException(critter_class_name);
}
}
/**
* Gets a list of critters of a specific type.
* @param critter_class_name What kind of Critter is to be listed. Unqualified class name.
* @return List of Critters.
* @throws InvalidCritterException
*/
public static List<Critter> getInstances(String critter_class_name) throws InvalidCritterException {
List<Critter> result = new java.util.ArrayList<Critter>();
try {
Class<?> c = Class.forName("assignment4." + critter_class_name);
for(Critter x : population) {
if(c.isInstance(x))
result.add(x);
}
} catch (Exception e) {
throw new InvalidCritterException(critter_class_name);
}
return result;
}
/**
* Prints out how many Critters of each type there are on the board.
* @param critters List of Critters.
*/
public static void runStats(List<Critter> critters) {
System.out.print("" + critters.size() + " critters as follows -- ");
java.util.Map<String, Integer> critter_count = new java.util.HashMap<String, Integer>();
for (Critter crit : critters) {
String crit_string = crit.toString();
Integer old_count = critter_count.get(crit_string);
if (old_count == null) {
critter_count.put(crit_string, 1);
} else {
critter_count.put(crit_string, old_count.intValue() + 1);
}
}
String prefix = "";
for (String s : critter_count.keySet()) {
System.out.print(prefix + s + ":" + critter_count.get(s));
prefix = ", ";
}
System.out.println();
}
/* the TestCritter class allows some critters to "cheat". If you want to
* create tests of your Critter model, you can create subclasses of this class
* and then use the setter functions contained here.
*
* NOTE: you must make sure that the setter functions work with your implementation
* of Critter. That means, if you're recording the positions of your critters
* using some sort of external grid or some other data structure in addition
* to the x_coord and y_coord functions, then you MUST update these setter functions
* so that they correctly update your grid/data structure.
*/
static abstract class TestCritter extends Critter {
protected void setEnergy(int new_energy_value) {
super.energy = new_energy_value;
if(new_energy_value <= 0)
super.isAlive = false;
}
protected void setX_coord(int new_x_coord) {
super.x_coord = new_x_coord;
}
protected void setY_coord(int new_y_coord) {
super.y_coord = new_y_coord;
}
protected int getX_coord() {
return super.x_coord;
}
protected int getY_coord() {
return super.y_coord;
}
/*
* This method getPopulation has to be modified by you if you are not using the population
* ArrayList that has been provided in the starter code. In any case, it has to be
* implemented for grading tests to work.
*/
protected static List<Critter> getPopulation() {
return population;
}
/*
* This method getBabies has to be modified by you if you are not using the babies
* ArrayList that has been provided in the starter code. In any case, it has to be
* implemented for grading tests to work. Babies should be added to the general population
* at either the beginning OR the end of every timestep.
*/
protected static List<Critter> getBabies() {
return babies;
}
}
/**
* Clear the world of all critters, dead and alive
*/
public static void clearWorld() {
population.clear();
babies.clear();
}
/**
* Performs the timestep, in the order given below:
* 1. increment timestep; timestep++;
2. doTimeSteps();
3. Do the fights. doEncounters();
4. updateRestEnergy();
5. Generate Algae genAlgae();
6. Move babies to general population. population.addAll(babies); babies.clear();
Note that these methods do not exist, they are simply a description of what the below method does.
*/
public static void worldTimeStep() {
// Complete this method.
Critter.Encounter = false;
for(Critter x : population) {
x.moveDone = false;
x.doTimeStep();
if(x.energy > 0)
x.isAlive = true;
else
x.isAlive = false;
}
for(Critter x: population) {
for(Critter y: population) {
if(x.x_coord == y.x_coord && x.y_coord == y.y_coord && x.isAlive && y.isAlive && !x.equals(y)) {
Critter.Encounter = true;
boolean xfight = x.fight(y.toString());
boolean yfight = y.fight(x.toString());
if(x.x_coord == y.x_coord && x.y_coord == y.y_coord && y.isAlive && x.isAlive) {
//they're gonna fight
int rollx, rolly;
if(xfight) {
rollx = Critter.getRandomInt(x.energy);
}
else
rollx = 0;
if(yfight) {
rolly = Critter.getRandomInt(y.energy);
}
else
rolly = 0;
if(rollx > rolly) {
y.isAlive = false;
x.energy += y.energy/2;
}
if(rolly > rollx) {
x.isAlive = false;
y.energy += x.energy/2;
}
if(rollx == rolly) {
if(Critter.getRandomInt(2) == 1) {
y.isAlive = false;
x.energy += y.energy/2;
}
else {
x.isAlive = false;
y.energy += x.energy/2;
}
}
}
}
}
}
for(Critter c : population) {
c.energy -= Params.rest_energy_cost;
if(c.energy <= 0)
c.isAlive = false;
}
for(int i = 0; i < Params.refresh_algae_count; i++) {
try {
Critter.makeCritter("Algae");
} catch (InvalidCritterException e) {
// TODO Auto-generated catch block
e.toString();
}
}
for(Critter b : babies) {
population.add(b);
b.isBaby = false;
b.isAlive = true;
}
babies.clear();
for(Iterator<Critter> iterator = population.iterator(); iterator.hasNext(); ) {
Critter c = iterator.next();
if(c.isAlive = false || c.energy <= 0)
iterator.remove();
}
}
public static void displayWorld() {
// Complete this method.
System.out.print("+");
for(int i = 0; i < Params.world_width; i++)
System.out.print("-");
System.out.print("+");
System.out.println();
char[][] array = new char[Params.world_height][Params.world_width+2];
for(int i = 0; i < Params.world_height; i++) {
for(int j = 0; j < Params.world_width; j++) {
array[i][j] = ' ';
}
}
for(Critter c : population) {
try {
array[c.x_coord][c.y_coord] = c.toString().charAt(0);
}
catch(Exception e) {
continue;
}
}
for(int i = 0; i < Params.world_height; i++) {
for(int j = 0; j < Params.world_width + 2; j++) {
if(j == 0 || j == Params.world_width + 1)
System.out.print('|');
else
System.out.print(array[i][j]);
}
System.out.println();
}
System.out.print("+");
for(int i = 0; i < Params.world_width; i++)
System.out.print("-");
System.out.print("+");
System.out.println();
}
}