-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntMaster.java
More file actions
547 lines (447 loc) · 15.6 KB
/
EntMaster.java
File metadata and controls
547 lines (447 loc) · 15.6 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
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
import java.lang.Math;
import java.util.Random;
/*
* EntMaster.java
* @version 1.0
* @since April 24, 2019
* Manages all entities and interactions
*/
class EntMaster {
// Create the random generator (object)
Random rand = new Random();
// 2D array of entities
private Entity[][] entMap;
// Turn number (0)
private static int turn = 0;
// Map size
private int mapSize;
// Display object
private DisplayGrid display;
// Given spawn rate of grass
private final double GRASSRATE;
// Given grass health
private final int GRASSHEALTH;
// Given sheep health
private final int SHEEPHEALTH;
// Given wolf health
private final int WOLFHEALTH;
// Current entity counts
private int grassAlive = 0;
private int sheepAlive = 0;
private int wolvesAlive = 0;
// Historical entity counts
private int masterSheepCount = 0;
private int masterGrassCount = 0;
private int masterWolfCount = 0;
// Best wolf health
private int wolfBreedCount = 0;
// Best sheep health
private int sheepBreedCount = 0;
EntMaster(int size, double grassRate, int grassHP, int initialSheep, int sheepHP, int initialWolves, int wolfHP) {
this.mapSize = size;
entMap = new Entity[size][size];
display = new DisplayGrid(entMap, turn);
this.GRASSRATE = grassRate;
this.GRASSHEALTH = grassHP;
this.SHEEPHEALTH = sheepHP;
this.WOLFHEALTH = wolfHP;
this.generateRandomSheep(initialSheep);
this.generateRandomWolf(initialWolves);
}
/**
* addGrass
* Initiates addEnt method with a grass object meant to be created
* @param y The Y position in the 2D array that holds the object
* @param x The X position in the 2D array that holds the object
* @return boolean A boolean to represent the success of creating and adding the grass
*/
boolean addGrass(int y, int x) {
return addEnt(new Grass(GRASSHEALTH, y, x));
}
/**
* addSheep
* Initiates addEnt method with a sheep object meant to be created
* @param y The Y position in the 2D array that holds the object
* @param x The X position in the 2D array that holds the object
* @return boolean A method (boolean return) to represent the success of creating and adding the sheep
*/
boolean addSheep(int y, int x) {
return addEnt(new Sheep(SHEEPHEALTH, y, x));
}
/**
* addWolf
* Initiates addEnt method with a wolf object meant to be created
* @param y The Y position in the 2D array that holds the object
* @param x The X position in the 2D array that holds the object
* @return boolean A method (boolean return) to represent the success of creating and adding the sheep
*/
boolean addWolf(int y, int x) {
return addEnt(new Wolf(WOLFHEALTH, y, x));
}
/**
* addEnt
* Private method that attempts to add an object to the entMap array, should be initiated by specific add entity methods
* @param entity The entity that is being added to the entMap array
* @return boolean A boolean to represent addition of the specified object
*/
private boolean addEnt(Entity entity) {
int testX = entity.getX();
int testY = entity.getY();
// Coordinates are reversed for the 2-D array - Y comes before X
if (entMap[testY][testX] == null) {
entMap[testY][testX] = entity;
return true;
} else {
return false;
}
}
/**
* updateInfo
* Adds a turn count, counts entities on the map, updates display with current information
* including the the sheep and wolf with highest fitness
*/
void updateInfo() {
turn += 1;
display.addTurn();
int currentSheepCount = 0;
int currentWolfCount = 0;
int currentGrassCount = 0;
for (int i = 0; i<entMap.length; i++) {
for (int j = 0; j<entMap[0].length; j++) {
if (entMap[i][j] instanceof Grass) {
currentGrassCount += 1;
} else if (entMap[i][j] instanceof Sheep) {
((Sheep)entMap[i][j]).addCoolDown();
currentSheepCount += 1;
if (((Sheep)entMap[i][j]).getFitness() > this.sheepBreedCount) {
this.sheepBreedCount = ((Sheep)entMap[i][j]).getFitness();
display.updateBestSheep(entMap[i][j]);
}
} else if (entMap[i][j] instanceof Wolf) {
((Wolf)entMap[i][j]).addCoolDown();
currentWolfCount +=1;
if (((Wolf)entMap[i][j]).getFitness() > this.wolfBreedCount) {
this.wolfBreedCount = ((Wolf)entMap[i][j]).getFitness();
display.updateBestWolf(entMap[i][j]);
}
}
}
}
this.grassAlive = currentGrassCount;
this.sheepAlive = currentSheepCount;
this.wolvesAlive = currentWolfCount;
display.updateAnimalCount(currentGrassCount, currentSheepCount, currentWolfCount);
}
/**
* getTurn
* Retrieves the turn number
* @return int, The turn number of the current turn
*/
int getTurn() {
return turn;
}
/**
* getMasterGrass
* Retrieves the historic total of grass
* @return int, The turn number of total grass spawned throughout the simulation
*/
int getMasterGrass() {
return masterGrassCount;
}
/**
* getMasterSheep
* Retrieves the historic total of sheep
* @return int, The turn number of total sheep spawned throughout the simulation
*/
int getMasterSheep() {
return masterSheepCount;
}
/**
* getMasterWolves
* Retrieves the historic total of wolves
* @return int, The turn number of total wolves spawned throughout the simulation
*/
int getMasterWolves() {
return masterWolfCount;
}
/**
* checkFull
* Checks if the entity array is full
* @return boolean, Condition of being full/not full
*/
boolean checkFull(){
boolean full = true;
for (int i = 0; i < entMap.length; i++) {
for (int j = 0; j < entMap[0].length; j++) {
if (entMap[i][j] == null) {
full = false;
}
}
}
return full;
}
/**
* printMap
* Displays a string-based map in the console
*/
void printMap() {
for (int i = 0; i < entMap.length; i++) {
for (int j = 0; j< entMap[0].length; j++) {
if (entMap[i][j] instanceof Grass) {
System.out.print("[G]");
} else if (entMap[i][j] instanceof Sheep) {
System.out.print("[S]");
} else if (entMap[i][j] instanceof Wolf) {
System.out.print("[W]");
} else {
System.out.print("[ ]");
}
System.out.print(".");
}
System.out.println("");
}
System.out.println("");
}
/**
* displayMap
* Refreshes the display grid
*/
void displayMap() {
display.refresh();
}
/**
* spawnGrass
* Spawns grass based on the given grass spawn rate
* Every null space has a given chance to spawn a grass if a random double generated is below the double GRASSRATE
*/
void spawnGrass() {
double spawnDouble;
for (int i = 0; i < entMap.length; i++) {
for (int j = 0; j < entMap.length; j++){
if ((entMap[i][j] == null) && (this.checkFull() == false)) {
spawnDouble = Math.random();
//System.out.println(i + " " + j + " " + spawnDouble);
if (spawnDouble < GRASSRATE) {
if (addGrass(i, j) == true) {
this.masterGrassCount += 1;
}
}
}
}
}
}
/**
* eliminateZero
* Nullifies all entities with zero health
*/
void eliminateZero() {
for (int i = 0; i<entMap.length; i++) {
for (int j = 0; j < entMap[0].length; j++)
if ((entMap[i][j] != null) && (entMap[i][j].getHealth() <= 0)) {
entMap[i][j] = null;
}
}
}
/**
* resetMoveConditions
* Resets all entity conditions to false
* Should be used at the start of each turn to allow all entities to be moved again
* Condition prevents an entity from being moved twice in a turn through 2D array iteration
*/
void resetMoveConditions() {
for (int i = 0; i<entMap.length; i++) {
for (int j = 0; j<entMap[0].length; j++) {
if (entMap[i][j] instanceof Sheep) {
((Sheep)entMap[i][j]).setMoveCondition(false);
} else if (entMap[i][j] instanceof Wolf) {
((Wolf)entMap[i][j]).setMoveCondition(false);
}
}
}
}
/**
* generateRandomSheep
* Spawns random sheep based on number of expected spawns
* @param instances, The number of sheep to initially spawn
*/
void generateRandomSheep(int instances) {
int generated = 0;
int randomX;
int randomY;
while ((generated < instances) && (this.checkFull() == false)) {
randomY = rand.nextInt(this.mapSize);
randomX = rand.nextInt(this.mapSize);
if (entMap[randomY][randomX] == null) {
if (addSheep(randomY, randomX) == true) {
generated +=1;
this.masterSheepCount += 1;
}
}
}
}
/**
* generateRandomWolf
* Spawns random wolves based on number of expected spawns
* @param instances, The number of wolves to initially spawn
*/
void generateRandomWolf(int instances) {
int generated = 0;
int randomX;
int randomY;
while ((generated < instances) && (this.checkFull() == false)) {
randomY = rand.nextInt(this.mapSize);
randomX = rand.nextInt(this.mapSize);
if (entMap[randomY][randomX] == null) {
if (addWolf(randomY, randomX) == true) {
generated +=1;
this.masterWolfCount += 1;
}
}
}
}
/**
* replace
* Replaces new array position with a new entity
* @param entity, The entity that is being copied
* @param newY, The new Y coordinate being copied in
* @param newX, The new X coordinate being copied in
*/
void replace(Entity entity, int newY, int newX) {
if ((entity instanceof Sheep)&&(entMap[newY][newX] instanceof Sheep)) {
System.out.println("ERROR: SHEEP REPLACING SHEEP");
}
int oldY = entity.getY();
int oldX = entity.getX();
entity.setY(newY);
entity.setX(newX);
entMap[newY][newX] = entity;
}
/**
* breedSheep
* Breeds two sheep, takes away health, resets cooldown count
* @param firstSheep, First sheep
* @param secondSheep, The new Y coordinate being copied in
*/
void breedSheep(Entity firstSheep, Entity secondSheep) {
((Sheep)firstSheep).takeDamage(10);
((Sheep)firstSheep).resetCoolDown();
((Sheep)firstSheep).addFitness();
((Sheep)secondSheep).takeDamage(10);
((Sheep)secondSheep).resetCoolDown();
((Sheep)secondSheep).addFitness();
generateRandomSheep(1);
}
/**
* breedWolf
* Breeds two wolves, takes away health, resets cooldown count
* @param firstWolf, First wolf
* @param secondWolf, Second wolf
*/
void breedWolf(Entity firstWolf, Entity secondWolf) {
((Wolf)firstWolf).takeDamage(10);
((Wolf)firstWolf).resetCoolDown();
((Wolf)firstWolf).addFitness();
((Wolf)secondWolf).takeDamage(10);
((Wolf)secondWolf).resetCoolDown();
((Wolf)secondWolf).addFitness();
generateRandomWolf(1);
}
/**
* moveEntities
* Moves all entities, facilitates all interactions
*/
void moveEntities() {
for (int i = 0; i < entMap.length; i++) {
for (int j = 0; j < entMap.length; j++) {
// SHEEP
if ((entMap[i][j] instanceof Sheep) && (((Sheep)entMap[i][j]).getMoveCondition() == false)) {
entMap[i][j].takeDamage(1);
// Get the next move, give the array to the sheep
int[] newCoords = ((Sheep)entMap[i][j]).getMove(entMap);
int newY = newCoords[0];
int newX = newCoords[1];
// Take away 1 health, guaranteed -1 per turn
entMap[i][j].takeDamage(1);
this.eliminateZero();
// If the sheep survives the turn
if (entMap[i][j] instanceof Sheep) {
// Don't do anything if it stays
if ((i == newY) && (j == newX)) {
((Sheep)entMap[i][j]).setMoveCondition(true);
// Interaction with Grass
} else if (entMap[newY][newX] instanceof Grass) {
entMap[i][j].gainHealth(entMap[newY][newX].getHealth());
entMap[newY][newX] = null;
this.replace(entMap[i][j], newY, newX);
((Sheep)entMap[newY][newX]).setMoveCondition(true);
entMap[i][j] = null;
// Interaction with Sheep
} else if (entMap[newY][newX] instanceof Sheep) {
this.breedSheep(entMap[i][j], entMap[newY][newX]);
//System.out.println(i + " " + " " + j + " Breeding with " + newY + " " + newX);
((Sheep)entMap[i][j]).setMoveCondition(true);
// Move to blank spot
} else {
this.replace(entMap[i][j], newY, newX);
((Sheep)entMap[newY][newX]).setMoveCondition(true);
entMap[i][j] = null;
}
}
// WOLF
} else if ((entMap[i][j] instanceof Wolf) && (((Wolf)entMap[i][j]).getMoveCondition() == false)) {
// Take away 1 health, guaranteed -1 per turn
entMap[i][j].takeDamage(1);
// Get the next move, give the array to the wolf
int[] newCoords = ((Wolf)entMap[i][j]).getMove(entMap);
//int[] newCoords = ((Sheep)entMap[i][j]).getMove(entMap);
int newY = newCoords[0];
int newX = newCoords[1];
// Eliminate entity if health is equal to 0
this.eliminateZero();
// If the wolf survives the turn
if (entMap[i][j] instanceof Wolf) {
// Don't do anything if it stays
if ((i == newY) && (j == newX)) {
((Wolf)entMap[i][j]).setMoveCondition(true);
// Interaction with Sheep
} else if (entMap[newY][newX] instanceof Sheep) {
entMap[i][j].gainHealth(entMap[newY][newX].getHealth()/4);
entMap[newY][newX] = null;
this.replace(entMap[i][j], newY, newX);
((Wolf)entMap[newY][newX]).setMoveCondition(true);
entMap[i][j] = null;
// Interaction with Wolf
} else if (entMap[newY][newX] instanceof Wolf) {
if (((Wolf)entMap[newY][newX]).getGender() == ((Wolf)entMap[i][j]).getGender()) {
//System.out.println("\nA " + ((Wolf)entMap[i][j]).getGender() + " is attacking a " + ((Wolf)entMap[newY][newX]).getGender());
//System.out.println("The first wolf sees a differential of " + ((Wolf)entMap[i][j]).compareTo(entMap[newY][newX]));
entMap[newY][newX].takeDamage(10);
} else {
this.breedWolf(entMap[i][j], entMap[newY][newX]);
}
//System.out.println(i + " " + " " + j + " Breeding with " + newY + " " + newX);
((Wolf)entMap[i][j]).setMoveCondition(true);
// Move into new spot if null or instance of grass (trample)
} else {
this.replace(entMap[i][j], newY, newX);
((Wolf)entMap[newY][newX]).setMoveCondition(true);
entMap[i][j] = null;
}
}
}
}
}
}
/**
* checkExtinction
* Checks for extinction of one animal
* @return boolean, Condition of extinction (either sheep or wolf)
*/
boolean checkExtinction() {
if ((this.sheepAlive <= 0) || (this.wolvesAlive <= 0)) {
return true;
} else {
return false;
}
}
}