-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
125 lines (93 loc) · 4.26 KB
/
Animal.java
File metadata and controls
125 lines (93 loc) · 4.26 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
import java.util.ArrayList;
public abstract class Animal {
protected final int BIRTHRANGE = 3;// The distance required for the formation of a new animal
protected int gender; // Gender --> 0= Male 1=Female
protected int STEPSIZE;//
public Animal(int gender, int STEPSIZE) {
this.gender = gender;
this.STEPSIZE = STEPSIZE;
}
//The method used to determine whether animals are of the same species
public abstract boolean isSameSpecies(Animal other);
//method that calculates in which directions the animals should move based on the number of steps
public void moveAnimal(Field field, int xi, int yj) {
int x = xi;
int y = yj;
if ((Main.HEIGHT <= x + 1 || !field.isEmpty(x + 1, y)) && (0 > x - 1 || !field.isEmpty(x - 1, y)) && (0 > y - 1 || !field.isEmpty(x, y - 1)) && (Main.WIDTH <= y + 1 || !field.isEmpty(x, y + 1))) {
return;
}
Main.stepNumber += this.STEPSIZE;
for (int i = 0; i < this.STEPSIZE; ) {
int number = Main.random.nextInt(4);
switch (number) {
case 0:
if (Main.WIDTH > x + 1 && field.isEmpty(x + 1, y)) {
field.field[x + 1][y] = field.field[x][y];
field.field[x][y] = null;
x++;
i++;
break;
} else {
break;
}
case 1:
if (0 <= x - 1 && field.isEmpty(x - 1, y)) {
field.field[x - 1][y] = field.field[x][y];
field.field[x][y] = null;
x--;
i++;
break;
} else {
break;
}
case 2:
if (Main.WIDTH > y + 1 && field.isEmpty(x, y + 1)) {
field.field[x][y + 1] = field.field[x][y];
field.field[x][y] = null;
y++;
i++;
break;
} else {
break;
}
case 3:
if (0 <= y - 1 && field.isEmpty(x, y - 1)) {
field.field[x][y - 1] = field.field[x][y];
field.field[x][y] = null;
y--;
i++;
break;
} else {
break;
}
}
}
}
//In this method, it tells us whether there is a male animal that has not previously mated with another female animal within 3 units of the female animal we have selected from the field.
// If there are, we remove these 2 animals from the male and female lists. And we can say that we approve the formation of new animals around the female in the field class.
public boolean isBirthable(Field field, int x, int y, Animal animal) {
ArrayList<Animal> husbandable = new ArrayList<>();
for (int i = (x - BIRTHRANGE); i <= (x + BIRTHRANGE); i++) {
for (int j = (y - BIRTHRANGE); j <= (y + BIRTHRANGE); j++) {
if (Main.WIDTH > i && Main.WIDTH > j && i >= 0 && j >= 0) {
if (field.field[i][j] != null) {
if (field.field[i][j].equals(animal)) {
continue;
}
if (isSameSpecies(field.field[i][j]) && field.field[i][j].gender == 0 && field.maleAnimal.contains(field.field[i][j])) {
husbandable.add(field.field[i][j]);
}
}
}
}
}
if (husbandable.size() > 0) {
Animal fatherAnimal = husbandable.get(Main.random.nextInt(husbandable.size()));
husbandable.remove(fatherAnimal);
field.maleAnimal.remove(fatherAnimal);
field.femaleAnimal.remove(animal);
return true;
} else
return false;
}
}