-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCritter.java
892 lines (795 loc) · 23.5 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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
package assignment5;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import application.CritterWorld;
import application.printCritter;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public abstract class Critter {
/* NEW FOR PROJECT 5 */
public enum CritterShape {
CIRCLE,
SQUARE,
TRIANGLE,
DIAMOND,
STAR,
TEKASHI
}
/* the default color is white, which I hope makes critters invisible by default
* If you change the background color of your View component, then update the default
* color to be the same as you background
*
* critters must override at least one of the following three methods, it is not
* proper for critters to remain invisible in the view
*
* If a critter only overrides the outline color, then it will look like a non-filled
* shape, at least, that's the intent. You can edit these default methods however you
* need to, but please preserve that intent as you implement them.
*/
public javafx.scene.paint.Color viewColor() {
return javafx.scene.paint.Color.WHITE;
}
public javafx.scene.paint.Color viewOutlineColor() { return viewColor(); }
public javafx.scene.paint.Color viewFillColor() { return viewColor(); }
public static HashMap <Key, List<Critter>> positions = new HashMap<Key, List<Critter> >();
public abstract CritterShape viewShape();
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];
}
public static String getPackage() {
return Critter.myPackage;
}
public String look(int direction, boolean steps) {
this.energy = this.energy - Params.look_energy_cost;
int x;
int y;
if(!steps) {
switch(direction) {
case 0:
x = step('x', 1, this.x_coord);
y = this.y_coord;
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 1:
x = step('x', 1, this.x_coord);
y = step('y', -1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 2:
x = this.x_coord;
y = step('y', -1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 3:
x = step('x', -1, this.x_coord);
y = step('y', -1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 4:
x = step('x', -1, this.x_coord);
y = this.y_coord;
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 5:
x = step('x', -1, this.x_coord);
y = step('y', 1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 6:
x = this.x_coord;
y = step('y', 1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 7:
x = step('x', 1, this.x_coord);
y = step('y', 1, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
}
}
if(steps) {
switch(direction) {
case 0:
x = step('x', 2, this.x_coord);
y = this.y_coord;
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 1:
x = step('x', 2, this.x_coord);
y = step('y', -2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 2:
x = this.x_coord;
y = step('y', -2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 3:
x = step('x', -2, this.x_coord);
y = step('y', -2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 4:
x = step('x', -2, this.x_coord);
y = this.y_coord;
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 5:
x = step('x', -2, this.x_coord);
y = step('y', 2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 6:
x = this.x_coord;
y = step('y', 2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
case 7:
x = step('x', 2, this.x_coord);
y = step('y', 2, this.y_coord);
if(hasCritterthere(x, y)) {
Key pos = new Key(x,y);
Critter crit = positions.get(pos).get(0);
return crit.toString();
}
else {
return null;
}
}
}
return null;
}
/* rest is unchanged from Project 4 */
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 boolean hasCritterthere(int x, int y) {
//checks if there is a critter in the position that the fighting critter wants to flee to
Key pos = new Key(x,y);
if(positions.containsKey(pos)) { //checks if position critter is at position
List<Critter> crit = positions.get(pos);
for(Critter c: crit ) { //checks if critter at that position is alive
if(!(c.energy <= 0)) {
return true;
}
}
}
return false;
}
private final int step(Character type, int steps, int initVal) {
if(type.equals('x')) { // updates x position
initVal = initVal + steps; //adds the number of steps the critter must take to the inital position of the critter
if(initVal == -1) { //wraps the critters around
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')) { //updates the critter's y position
initVal = initVal + steps; //adds the number of steps to the critter's inital position
if(initVal == -1) {
initVal = Params.world_height-1; //wraps critter world
}
if(initVal == -2) {
initVal = Params.world_height-2;
}
if(initVal == Params.world_height) {
initVal = 0;
}
if(initVal == Params.world_height+1) {
initVal = 1;
}
}
return initVal;
}
protected final void walk(int direction) {
int xpos;
int ypos;
this.energy = this.energy - Params.walk_energy_cost; //deducts walking energy cost
if(!this.moved) {
switch(direction) { //updates position based on direction
case 0:
xpos = step('x', 1, this.x_coord);
ypos = this.y_coord;
if(this.inFight) { //run method is critter is fighting
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; //deducts run energy cost
if(!this.moved) { //checks if critter has already moved before
switch(direction) { //updates position based on direction
case 0:
xpos = step('x', 2, this.x_coord);
ypos = this.y_coord;
if(this.inFight) { //run method if critter is in fight
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) {
if(!(this.energy > Params.min_reproduce_energy)) { //makes sure that parent critter has enough energy to reproduce
return;
}
offspring.energy = this.energy/2; //gives offspring half of parents energy
this.energy = (int)Math.ceil(this.energy/2); //reduces parents energy
switch(direction) { //updates offsprings position based on parent's position and 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); //adds offsprings to baby population
}
public abstract void doTimeStep();
public abstract boolean fight(String oponent);
public static void worldTimeStep() {
if(population == null || population.isEmpty()) { //returns if population is empty
return;
}
for(Critter c : population) {
c.moved = false;
c.doTimeStep();
}
updatePositions();
for(List<Critter> crit: positions.values()) { //gets array list of all critters at different positions in hash map
boolean aFight;
boolean bFight;
int aDice;
int bDice;
if(crit.size() > 1) { //if arraylist is bigger than 1, then multiple critters at one position, and must fight
int nextOp = 1; //keeps track of next opponents (if there's more than 1)
Critter a = crit.get(0);
Critter b = crit.get(nextOp);
while(nextOp < crit.size()){ //ends of no more opponents
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) { //deducts rest energy cost for all critters
c.energy = c.energy - Params.rest_energy_cost;
}
for(int i=0;i<population.size();i++) { //removes dead critters
if(population.get(i) != null)
{
if(population.get(i).energy <= 0) {
population.remove(i);
}
}
}
for(int i=0;i<Params.refresh_algae_count;i++) { //creates new algae
try {
Critter.makeCritter("Algae");
}
catch(InvalidCritterException e) {
e.printStackTrace();
}
}
population.addAll(babies); //adds all babies to population
babies.clear();
}
// public static void displayWorld(Object pane) {
//
// Stage primaryStage = new Stage();
// pane = primaryStage;
// GridPane gridPane = new GridPane();
// gridPane.setMinSize(Params.world_width, Params.world_height);
// gridPane.setPadding(new Insets(25,25,25,25));
// //gridPane.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// primaryStage.setScene(new Scene(gridPane,300,250));
// primaryStage.show();
// //viewColor
//
// }
//Alternate displayWorld, where you use Main.<pane> to reach into your
// display component.
public static void updateDisplay() {
printCritter.clearDisplay();
for(Critter c: population) {
printCritter.print(c.viewShape(), c.viewOutlineColor(), c.viewFillColor(), c.x_coord,c.y_coord);
}
}
public static void displayWorld() {
for(Critter c: population) {
printCritter.print(c.viewShape(), c.viewOutlineColor(), c.viewFillColor(), c.x_coord,c.y_coord);
}
Application.launch(CritterWorld.class);
}
/* create and initialize a Critter subclass
* critter_class_name must be the name of a concrete subclass of Critter, if not
* an InvalidCritterException must be thrown
*/
public static void makeCritter(String critter_class_name) throws InvalidCritterException {
Class<?> fefe = null;
try {
fefe = Class.forName(Critter.myPackage + "." + critter_class_name);
Constructor<?> construct = fefe.getConstructor();
Object newcrit = construct.newInstance();
if(!(newcrit instanceof Critter)) { //makes sure that new critter is a valid 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;
population.add((Critter) newcrit);
}
catch (Exception e) {
e.printStackTrace();
}
}
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;
}
public static String runStats(List<Critter> critters) {
String result = null;
result = "" + 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()) {
result = result + prefix + s + ":" + critter_count.get(s);
prefix = ", ";
}
return result;
}
/* 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 correctup update your grid/data structure.
*/
static abstract class TestCritter extends Critter {
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;
}
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;
}
}
public static void updatePositions() {
positions.clear();
for(Critter c: population) {
Key po = new Key(c.x_coord,c.y_coord); //creates key with critter's position
if(!(positions.containsKey(po))) { //adds key if it doesn't exist, and adds critter
List <Critter> clist = new ArrayList<Critter>();
clist.add(c);
positions.put(po, clist);
}else {
List <Critter> clist = positions.get(po); //adds critter to array list if key exists already
clist.add(c);
positions.put(po, clist);
}
}
}
/**
* Clear the world of all critters, dead and alive
*/
public static void clearWorld() {
population.clear();
updatePositions();
}
}