-
Notifications
You must be signed in to change notification settings - Fork 0
/
Critter.java
710 lines (619 loc) · 17.8 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package assignment4;
/* CRITTERS Critter.java
* EE422C Project 4 submission by
* Kayla Tran
* knt627
* 16345
* Arvin Bhatti
* ab62733
* 16345
* Slip days used: <2>
* Fall 2018
*/
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/* 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>();
public static HashMap <Key, List<Critter>> positions = new HashMap<Key, List<Critter> >();
//public static HashSet <String> c = new HashSet<String>();
// 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) {
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 int x_coord;
private int y_coord;
private boolean moved;
private boolean inFight;
private final int step(Character type, int steps, int initVal) {
if(type.equals('x')) {
//System.out.println("steps: x " + "step size: " + steps+ "initval" + initVal);
initVal = initVal + steps;
// if(initVal >= Params.world_width || initVal < 0) {
// initVal = Math.abs(initVal % Params.world_width);
// }
if(initVal == -1) {
initVal = Params.world_width-1;
}
if(initVal == -2) {
initVal = Params.world_width-2;
}
if(initVal == Params.world_width) {
initVal = 0;
}
if(initVal == Params.world_width+1) {
initVal = 1;
}
}
if(type.equals('y')) {
//System.out.println("steps: y " + "step size: " + steps+ " initval " + initVal);
initVal = initVal + steps;
// if(initVal >= Params.world_height || initVal < 0) {
// initVal = Math.abs(initVal % Params.world_height);
// }
if(initVal == -1) {
initVal = Params.world_width-1;
}
if(initVal == -2) {
initVal = Params.world_width-2;
}
if(initVal == Params.world_width) {
initVal = 0;
}
if(initVal == Params.world_width+1) {
initVal = 1;
}
}
//System.out.println("new pos" + initVal);
return initVal;
}
private final boolean hasCritterthere(int x, int y) {
//System.out.println("hascritterthere");
Key pos = new Key(x,y);
if(positions.containsKey(pos)) {
List<Critter> crit = positions.get(pos);
for(Critter c: crit ) {
if(!(c.energy <= 0)) {
return true;
}
}
}
return false;
}
protected final void walk(int direction) {//TEST
int xpos;
int ypos;
this.energy = this.energy - Params.walk_energy_cost;
if(!this.moved) {
switch(direction) {
case 0:
xpos = step('x', 1, this.x_coord);
ypos = this.y_coord;
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
}
}else {
this.x_coord = xpos;
}
break;
case 1:
xpos = step('x', 1, this.x_coord);
ypos = step('y', -1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 2:
xpos = this.x_coord;
ypos = step('y', -1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 3:
xpos = step('x', -1, this.x_coord);
ypos = step('y', -1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 4:
xpos = step('x', -1, this.x_coord);
ypos = this.y_coord;
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 5:
xpos = step('x', -1, this.x_coord);
ypos = step('y', 1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 6:
xpos = this.x_coord;
ypos = step('y', 1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 7:
xpos = step('x', 1, this.x_coord);
ypos = step('y', 1, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
}
this.moved = true;
}
}
protected final void run(int direction) {
int xpos;
int ypos;
this.energy = this.energy - Params.run_energy_cost;
if(!this.moved) {
switch(direction) {
case 0:
xpos = step('x', 2, this.x_coord);
ypos = this.y_coord;
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 1:
xpos = step('x', 2, this.x_coord);
ypos = step('y', -2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 2:
xpos = this.x_coord;
ypos = step('y', -2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 3:
xpos = step('x', -2, this.x_coord);
ypos = step('y', -2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 4:
xpos = step('x', -2, this.x_coord);
ypos = this.y_coord;
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 5:
xpos = step('x', -2, this.x_coord);
ypos = step('y', 2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 6:
xpos = this.x_coord;
ypos = step('y', 2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
case 7:
xpos = step('x', 2, this.x_coord);
ypos = step('y', 2, this.y_coord);
if(this.inFight) {
if(!hasCritterthere(xpos,ypos)) {
this.x_coord = xpos;
this.y_coord = ypos;
}
}else {
this.x_coord = xpos;
this.y_coord = ypos;
}
break;
}
this.moved = true;
}
}
protected final void reproduce(Critter offspring, int direction) {
//System.out.println("reproducing");
if(!(this.energy > Params.min_reproduce_energy)) {
return;
}
offspring.energy = this.energy/2;
this.energy = (int)Math.ceil(this.energy/2);
switch(direction) {
case 0:
offspring.x_coord = step('x', 1, this.x_coord);
offspring.y_coord = this.y_coord;
break;
case 1:
offspring.y_coord = step('y', -1, this.y_coord);
offspring.x_coord = step('x', 1, this.x_coord);
break;
case 2:
offspring.y_coord = step('y', -1, this.y_coord);
offspring.x_coord = this.x_coord;
break;
case 3:
offspring.y_coord = step('y', -1, this.y_coord);
offspring.x_coord = step('x', -1, this.x_coord);
break;
case 4:
offspring.x_coord = step('x', -1, this.x_coord);
offspring.y_coord = this.y_coord;
break;
case 5:
offspring.y_coord = step('y', 1, this.y_coord);
offspring.x_coord = step('x', -1, this.x_coord);
break;
case 6:
offspring.y_coord = step('y', 1, this.y_coord);
offspring.x_coord = this.x_coord;
break;
case 7:
offspring.y_coord = step('y', 1, this.y_coord);
offspring.x_coord = step('x', 1, this.x_coord);
break;
}
babies.add(offspring);
}
public abstract void doTimeStep();
public abstract boolean fight(String opponent);
/**
* 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 {
//error no capital letter
Class<?> fefe = null;
try {
fefe = Class.forName(Critter.myPackage + "." + critter_class_name);
Constructor<?> construct = fefe.getConstructor();
Object newcrit = construct.newInstance();
if(!(newcrit instanceof Critter)) {
throw new InvalidCritterException(critter_class_name);
}
((Critter) newcrit).x_coord = getRandomInt(Params.world_width);
((Critter) newcrit).y_coord = getRandomInt(Params.world_height);
((Critter) newcrit).energy = Params.start_energy;
((Critter) newcrit).moved = false;
//System.out.println("new crit x coord:" + ((Critter)newcrit).x_coord);
//System.out.println("new crit y coord:" + ((Critter)newcrit).y_coord);
population.add((Critter) newcrit);
//updatePositions();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 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 {
Class<?> crit = null;
List<Critter> instances = new java.util.ArrayList<Critter>();
try {
crit = Class.forName(Critter.myPackage +"." + critter_class_name);
for (Critter a : population) {
if (crit.isInstance(a)) {
instances.add(a);
}
}
} catch (Exception e) {
e.printStackTrace();
//return instances;
}
return instances;
}
/**
* 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 {///?? do we need to update
protected void setEnergy(int new_energy_value) {
super.energy = new_energy_value;
}
protected void setX_coord(int new_x_coord) {
super.x_coord = new_x_coord;
updatePositions();
}
protected void setY_coord(int new_y_coord) {
super.y_coord = new_y_coord;
updatePositions();
}
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();
updatePositions();
}
public static void updatePositions() {
positions.clear();
for(Critter c: population) {
Key po = new Key(c.x_coord,c.y_coord);
if(!(positions.containsKey(po))) {
List <Critter> clist = new ArrayList<Critter>();
clist.add(c);
positions.put(po, clist);
}else {
List <Critter> clist = positions.get(po);
clist.add(c);
positions.put(po, clist);
}
}
}
public static void worldTimeStep() {
if(population == null || population.isEmpty()) {
return;
}
for(Critter c : population) {
c.moved = false;
c.doTimeStep();
}
updatePositions();
for(List<Critter> crit: positions.values()) {
boolean aFight;
boolean bFight;
int aDice;
int bDice;
if(crit.size() > 1) {
int nextOp = 1;
Critter a = crit.get(0);
Critter b = crit.get(nextOp);
while(nextOp < crit.size()){
b = crit.get(nextOp);
a.inFight = true;
b.inFight = true;
aFight = a.fight(b.toString());
bFight = b.fight(a.toString());
if((a.energy > 0) && (b.energy > 0) && (a.x_coord == b.x_coord) && (a.y_coord == b.y_coord)) {
if(!aFight) {
aDice = 0;
}else {
aDice = Critter.getRandomInt(a.energy);
}
if(!bFight) {
bDice = 0;
}else {
bDice = Critter.getRandomInt(b.energy);
}
if(aDice >= bDice) { //remove loser?
a.energy = a.energy + (b.energy/2);
population.remove(b);
}
if(bDice > aDice) {
b.energy = b.energy + (a.energy/2);
population.remove(a);
a = b;
}
}
nextOp ++;
}
}
}
for(Critter c: population) {
c.energy = c.energy - Params.rest_energy_cost;
}
for(int i=0;i<population.size();i++) {
if(population.get(i) != null)
{
if(population.get(i).energy <= 0) {
population.remove(i);
}
}
}
for(int i=0;i<Params.refresh_algae_count;i++) {
try {
Critter.makeCritter("Algae");
}
catch(InvalidCritterException e) {
e.printStackTrace();
}
}
population.addAll(babies);
babies.clear();
}
public static void displayWorld() { //multiple on one position
System.out.print("+");
for(int i = 0; i<Params.world_width;i++) {
System.out.print("-");
}
System.out.println("+");
for(int row=0;row<Params.world_height;row++) {
System.out.print("|");
for(int col=0; col < Params.world_width; col++) {
Key pos = new Key(col,row);
if(positions.containsKey(pos)) {
List <Critter> c = positions.get(pos);
Critter cr = c.get(0);
System.out.print(cr.toString());
}else {
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.print("+");
for(int i = 0; i<Params.world_width;i++) {
System.out.print("-");
}
System.out.println("+");
}
}