-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.java
More file actions
515 lines (394 loc) · 18.6 KB
/
Field.java
File metadata and controls
515 lines (394 loc) · 18.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
import java.util.ArrayList;
public class Field {
Animal[][] field;
/**
* When choosing mother and father, use the 2 ArrayList below to avoid choosing them over and over again.
*/
ArrayList<Animal> femaleAnimal = new ArrayList<>();
ArrayList<Animal> maleAnimal = new ArrayList<>();
/**
* Most of the lists below are used to avoid repeating an action, such as walking, hunting, etc.
*/
ArrayList<Lion> listOfLion = new ArrayList<>();
ArrayList<Wolf> listOfWolf = new ArrayList<>();
ArrayList<Chicken> listOfChicken = new ArrayList<>();
ArrayList<Cow> listOfCow = new ArrayList<>();
ArrayList<Sheep> listOfSheep = new ArrayList<>();
ArrayList<Huntsman> listOfHuntsman = new ArrayList<>();
public Field(int height, int width) {
field = new Animal[height][width];
}
//----------------------------------------------------------------------------------------------------------------------------------------
/**
* Field operations are operations used to test code while writing it. It is easier to observe the constant variables in the main file by making them smaller.
*/
public void printArea(String[][] area1) {
for (String[] row : area1) {
for (String element : row) {
System.out.print(String.format("%-" + Main.WIDTH + "s", element));
}
System.out.println();
}
}
public void clearArea(String[][] area1) {
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
area1[i][j] = "-";
}
}
}
public void setArea(String[][] area1) {
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (field[i][j] != null) {
if (field[i][j].isSameSpecies(new Lion(0))) {
if (field[i][j].gender == 0) {
area1[i][j] = "ML";
} else {
area1[i][j] = "FL";
}
} else if (field[i][j].isSameSpecies(new Wolf(0))) {
if (field[i][j].gender == 0) {
area1[i][j] = "MW";
} else {
area1[i][j] = "FW";
}
} else if (field[i][j].isSameSpecies(new Huntsman())) {
if (field[i][j].gender == 0) {
area1[i][j] = "HU";
} else {
area1[i][j] = "HU";
}
} else if (field[i][j].isSameSpecies(new Sheep(0))) {
if (field[i][j].gender == 0) {
area1[i][j] = "MS";
} else {
area1[i][j] = "FS";
}
} else if (field[i][j].isSameSpecies(new Cow(0))) {
if (field[i][j].gender == 0) {
area1[i][j] = "MI";
} else {
area1[i][j] = "FI";
}
} else if (field[i][j].isSameSpecies(new Chicken(0))) {
if (field[i][j].gender == 0) {
area1[i][j] = "MC";
} else {
area1[i][j] = "FC";
}
}
}
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------------
/**
* Methods for the lists we use to avoid repetition when choosing parents
*/
public void setMaleFemale() {
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (!isEmpty(i, j) && field[i][j].gender == 1) {
femaleAnimal.add(field[i][j]);
} else if (!isEmpty(i, j) && field[i][j].gender == 0)
maleAnimal.add(field[i][j]);
}
}
}
public void clearMaleFemale() {
maleAnimal.clear();
femaleAnimal.clear();
}
//----------------------------------------------------------------------------------------------------------------------------------------
/**
* Methods for lists that we use to avoid repetition when performing procedures on animals
*/
public void setList() {
for (int i = 0; i < Main.HEIGHT; i++) {
for (int j = 0; j < Main.WIDTH; j++) {
if (field[i][j] != null) {
if (this.field[i][j].isSameSpecies(new Lion(0))) {
listOfLion.add((Lion) field[i][j]);
} else if (this.field[i][j].isSameSpecies(new Wolf(0))) {
listOfWolf.add((Wolf) field[i][j]);
} else if (this.field[i][j].isSameSpecies(new Huntsman())) {
listOfHuntsman.add((Huntsman) field[i][j]);
} else if (this.field[i][j].isSameSpecies(new Sheep(0))) {
listOfSheep.add((Sheep) field[i][j]);
} else if (this.field[i][j].isSameSpecies(new Cow(0))) {
listOfCow.add((Cow) field[i][j]);
} else if (this.field[i][j].isSameSpecies(new Chicken(0))) {
listOfChicken.add((Chicken) field[i][j]);
}
}
}
}
}
public void clearList() {
listOfLion.clear();
listOfWolf.clear();
listOfHuntsman.clear();
listOfCow.clear();
listOfChicken.clear();
listOfSheep.clear();
}
public void printList() {
System.out.println("\n");
System.out.println("Lion " + listOfLion.size() + "\n" +
"Wolf " + listOfWolf.size() + "\n" +
"Hunter " + listOfHuntsman.size() + "\n" +
"Cow " + listOfCow.size() + "\n" +
"Chicken " + listOfChicken.size() + "\n" +
"Sheep " + listOfSheep.size() + "\n");
System.out.println("\n");
}
//----------------------------------------------------------------------------------------------------------------------------------------
/**
* Method that prints how many animals we have in the final
*/
public void printNumberOfAnimal() {
int numberLion = 0;
int numberWolf = 0;
int numberHuntsman = 0;
int numberSheep = 0;
int numberCow = 0;
int numberChicken = 0;
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (field[i][j] != null) {
if (field[i][j].isSameSpecies(new Lion(0))) {
numberLion++;
} else if (field[i][j].isSameSpecies(new Wolf(0))) {
numberWolf++;
} else if (field[i][j].isSameSpecies(new Huntsman())) {
numberHuntsman++;
} else if (field[i][j].isSameSpecies(new Sheep(0))) {
numberSheep++;
} else if (field[i][j].isSameSpecies(new Cow(0))) {
numberCow++;
} else if (field[i][j].isSameSpecies(new Chicken(0))) {
numberChicken++;
}
}
}
}
System.out.println("\n");
System.out.println("Final Number of Animal ");
System.out.println("Lion number of : " + numberLion + "\n" +
"Wolf number of : " + numberWolf + "\n" +
"Hunter number of : " + numberHuntsman + "\n" +
"Cow number of : " + numberCow + "\n" +
"Chicken number of : " + numberChicken + "\n" +
"Sheep number of : " + numberSheep + "\n");
System.out.println("\n");
}
//----------------------------------------------------------------------------------------------------------------------------------------
/**
* The following methods are written for the operations of the animal within its field.
*/
// The method by which we find out whether the point in the field is empty or not
public boolean isEmpty(int x, int y) {
return field[x][y] == null;
}
//method of placement in the field according to the given points
public boolean addAnimal(int x, int y, Animal animal) {
if (isEmpty(x, y)) {
field[x][y] = animal;
return true;
} else
return false;
}
//Method that deletes the animal at a given location. We use this method for animals that are hunted and killed.
public void removeAnimal(int x, int y) {
field[x][y] = null;
}
//We use this method to place all of the starting animals in random locations in the field
public void SetAnimal(int numberOfAnimal, String animal) {
int gender = 0;
for (int i = 0; i < numberOfAnimal; i++) {
int tempx = Main.random.nextInt(Main.HEIGHT);
int tempy = Main.random.nextInt(Main.WIDTH);
if (isEmpty(tempx, tempy)) {
switch (animal) {
case "Lion":
addAnimal(tempx, tempy, new Lion(gender));
listOfLion.add((Lion) field[tempx][tempy]);
break;
case "Wolf":
addAnimal(tempx, tempy, new Wolf(gender));
listOfWolf.add((Wolf) field[tempx][tempy]);
break;
case "Huntsman":
addAnimal(tempx, tempy, new Huntsman());
listOfHuntsman.add((Huntsman) field[tempx][tempy]);
break;
case "Sheep":
addAnimal(tempx, tempy, new Sheep(gender));
listOfSheep.add((Sheep) field[tempx][tempy]);
break;
case "Chicken":
addAnimal(tempx, tempy, new Chicken(gender));
listOfChicken.add((Chicken) field[tempx][tempy]);
break;
case "Cow":
addAnimal(tempx, tempy, new Cow(gender));
listOfCow.add((Cow) field[tempx][tempy]);
break;
}
} else {
i--;
continue;
}
if (i == (numberOfAnimal / 2) - 1) {
gender++;
}
}
}
//In this method, it is checked what type of prey the animal selected from the field is and whether it has been hunted before, and if not, the hunting method is called and it is hunted.
public void deadAnimal(Lion lion, Wolf wolf, Huntsman huntsman) {
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (field[i][j] != null) {
if (field[i][j].isSameSpecies(new Lion(0)) && !listOfLion.contains(field[i][j])) {
listOfLion.add((Lion) field[i][j]);
lion.hunt(field[i][j], lion.huntRange, this, i, j);
} else if (field[i][j].isSameSpecies(new Wolf(0)) && !listOfWolf.contains(field[i][j])) {
listOfWolf.add((Wolf) field[i][j]);
wolf.hunt(field[i][j], wolf.huntRange, this, i, j);
} else if (field[i][j].isSameSpecies(new Huntsman()) && !listOfHuntsman.contains(field[i][j])) {
listOfHuntsman.add((Huntsman) field[i][j]);
huntsman.hunt(field[i][j], huntsman.huntRange, this, i, j);
}
}
}
}
}
//The method where the animal is placed in the area where it is calculated where the animal should go in the moveAnimal method in the animal class.
public void move() {
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (field[i][j] != null) {
if (field[i][j].isSameSpecies(new Lion(0)) && listOfLion.contains(field[i][j])) {
listOfLion.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
} else if (field[i][j].isSameSpecies(new Wolf(0)) && listOfWolf.contains(field[i][j])) {
listOfWolf.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
} else if (field[i][j].isSameSpecies(new Huntsman()) && listOfHuntsman.contains(field[i][j])) {
listOfHuntsman.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
} else if (field[i][j].isSameSpecies(new Sheep(0)) && listOfSheep.contains(field[i][j])) {
listOfSheep.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
} else if (field[i][j].isSameSpecies(new Cow(0)) && listOfCow.contains(field[i][j])) {
listOfCow.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
} else if (field[i][j].isSameSpecies(new Chicken(0)) && listOfChicken.contains(field[i][j])) {
listOfChicken.remove(field[i][j]);
field[i][j].moveAnimal(this, i, j);
}
}
}
}
}
//In this method, we find the female animal in the area, and if this female animal has not previously mated, we create a new animal.
// We check if there is an empty space around the female animal within 1 unit, if not, we check within 2 units, and if there is still no room, we continue checking in increments of 1 unit.
// Prioritizing the nearest surroundings, we randomly create the new animal in one of the empty spaces.
public void birth() {
setMaleFemale();
int k = 1;
int count = 0;
ArrayList<Integer> cordinateX = new ArrayList<>();
ArrayList<Integer> cordinateY = new ArrayList<>();
for (int i = 0; i < Main.WIDTH; i++) {
for (int j = 0; j < Main.HEIGHT; j++) {
if (field[i][j] != null && field[i][j].gender == 1) {
Animal animal = field[i][j];
if (femaleAnimal.contains(animal) && animal.isBirthable(this, i, j, field[i][j])) {
for (int m = (i - k); m <= (i + k); m++) {
for (int l = (j - k); l <= (j + k); l++) {
if (Main.WIDTH > m && Main.WIDTH > l && m >= 0 && l >= 0) {
if (field[m][l] != null && field[m][l].equals(animal)) {
continue;
}
if (isEmpty(m, l)) {
cordinateX.add(m);
cordinateY.add(l);
count++;
}
}
}
}
}
if (count > 0) {
int rand = Main.random.nextInt(count);
int newAnimalX = cordinateX.get(rand);
int newAnimalY = cordinateY.get(rand);
int gender = Main.random.nextInt(2);
if (this.field[i][j].isSameSpecies(new Lion(0))) {
addAnimal(newAnimalX, newAnimalY, new Lion(gender));
} else if (this.field[i][j].isSameSpecies(new Wolf(0))) {
addAnimal(newAnimalX, newAnimalY, new Wolf(gender));
} else if (this.field[i][j].isSameSpecies(new Sheep(0))) {
addAnimal(newAnimalX, newAnimalY, new Sheep(gender));
} else if (this.field[i][j].isSameSpecies(new Cow(0))) {
addAnimal(newAnimalX, newAnimalY, new Cow(gender));
} else if (this.field[i][j].isSameSpecies(new Chicken(0))) {
addAnimal(newAnimalX, newAnimalY, new Chicken(gender));
}
// addAnimal(newAnimalX, newAnimalY, new Lion(gender));
cordinateX.clear();
cordinateY.clear();
count = 0;
}
}
}
//
}
// public void newBirthkonumu(int x, int y, Animal femaleAnimal) {
// int k = 1;
// int x=femaleAnimal.
// ArrayList<Animal> emptyField = new ArrayList<>();
// ArrayList<Integer> xLocation = new ArrayList<>();
// ArrayList<Integer> yLocation = new ArrayList<>();
//
// for (int i = (x - k); i <= (x + k); i++) {
// for (int j = (y - k); j <= (y + k); j++) {
//
// if (Main.WIDTH > i && Main.WIDTH > j && i >= 0 && j >= 0) {
// if (femaleAnimal.isFemaleAnimal(field[i][j])){
//
// }
// if (field[i][j].equals(femaleAnimal)) {
// continue;
// }
// if (isEmpty(i, j)&& ) {
// xLocation.add(i);
// yLocation.add(j);
// emptyField.add(field[i][j]);
// addAnimal(i, j, femaleAnimal);
// }
// }
// if (i == (x + k) && xLocation.isEmpty()) {
// k++;
// }
// }
// }
// int realLocation = Main.random.nextInt(xLocation.size());
// int newX = xLocation.get(realLocation);
// int newY = yLocation.get(realLocation);
// addAnimal(newX, newX, femaleAnimal);
//
//// Animal hunt=emptyField.get(niceLocation);
//
//
//
//
//
//
//
//}
//
}
}