-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
225 lines (190 loc) · 7.44 KB
/
Animal.java
File metadata and controls
225 lines (190 loc) · 7.44 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
import java.util.List;
import java.util.Random;
import javafx.scene.paint.Color;
import java.util.HashMap;
/**
* A class representing shared characteristics of animals.
* @author Yingtao Zheng(Kevin) and Kexin Wang
* @time 2025.02.21
* @version 2.0
*/
public abstract class Animal {
protected enum Gender { FEMALE, MALE }
protected enum DeathReason { HUNGER, OLD, DISEASE, EATEN, OUT_OF_BOUNDS, OTHER }
protected enum Type { FLAMINGO, SHEEP, LION, TIGER, LEOPARD } // defined in ascending order (for acting)
public static final List<Color> AnimalColors = List.of(
Color.rgb(254,193,220),
Color.rgb(166,129,239),
Color.rgb(241,102,21),
Color.rgb(250,159,11),
Color.rgb(243,228,55)); // corresponding colors to the animal above
public static HashMap<Animal.Type, Color> colors = new HashMap<>();
private boolean alive = true;
private boolean sick = false;
private Field field;
private Location location;
private Color color = Color.BLACK;
private Type type; // type of the animal (e.g. FLAMINGO)
private Gender gender;
private String gene; // 14-digit genetic code
private static final Random rand = new Random();
private int countdown = 5; // when sick, remain alive for 2 steps
private int age;
/** new added for monitor */
private DeathReason deathReason;
private double foodLevel;
public Animal(Field field, Location location, Color col, Type type, boolean isFirstGeneration) {
alive = true;
sick = false;
this.field = field;
setLocation(location);
setColor(col);
this.type = type;
this.gender = rand.nextBoolean() ? Gender.FEMALE : Gender.MALE;
this.gene = isFirstGeneration ? generateRandomGene() : "05040120400000"; // a default value avoid null
}
/** generate 2 or 3 gene format */
private String formatGeneValue(int min, int max, int length) {
return String.format("%0" + length + "d", rand.nextInt(max - min + 1) + min);
}
/** generate valid random gene */
private String generateRandomGene() {
String breedingAge = formatGeneValue(12, 90, 2); // breeding age (12-90) inclusive
int minMaxAge = Integer.parseInt(breedingAge) + 1; // make sure the maxAge is at least one age larger than the breeding age
String maxAge = formatGeneValue(minMaxAge, 120, 3);
return breedingAge +
maxAge + // max age (10-120) inclusive (actually it's (12-120))
formatGeneValue(1, 49, 2) + // bredding probability (0-50) -> 0.00-0.50
formatGeneValue(1, 12, 2) + // litter size (1-12) inclusive
formatGeneValue(1, 49, 2) + // disease probability (0-50) -> 0.00-0.50
formatGeneValue(25, 100, 3); // metabolism (0.25-1.00) inclusive
}
/** split gene, get random attribute */
public int getBreedingAge() { return Integer.parseInt(gene.substring(0, 2)); }
public int getMaxAge() { return Integer.parseInt(gene.substring(2, 5)); }
public double getBreedingProbability() { return Integer.parseInt(gene.substring(5, 7)) / 100.0; }
public int getLitterSize() { return Integer.parseInt(gene.substring(7, 9)); }
public double getDiseaseProbability() { return Integer.parseInt(gene.substring(9, 11)) / 100.0; }
public double getMetabolism() { return Integer.parseInt(gene.substring(11, 14)) / 100.0; }
public String getGene() {
return gene;
}
public void setGene(String newGene) {
if (newGene == null || newGene.length() != 14) {
throw new IllegalArgumentException("Invalid gene length: " + (newGene == null ? "null" : newGene.length()));
}
this.gene = newGene;
}
/** subclass must have act */
public abstract void act(List<Animal> newAnimals);
/** age */
public void setAge(int newAge) {this.age = newAge;}
public int getAge() {return age;}
/**
* Increase the age. This could result in the animal's death.
*/
public void incrementAge() {
age++;
if(age > getMaxAge()) {
setDead(DeathReason.OLD);
}
}
/** food level */
public abstract double getFoodLevel();
/** gender */
public Gender getGender() { return gender; }
/** animal type */
public Type getType() { return type; }
/** alive */
protected boolean isAlive() { return alive; }
/** sick */
protected boolean isSick() { return sick; }
/** set sick */
protected void setSick() {sick = true; }
/** countdown to death */
protected void countdown() {
if (countdown > 0){
countdown--;
}
else{
setDead(DeathReason.DISEASE);
}
}
/** try to get sick */
protected void trySick() {
double diseaseProbability = getDiseaseProbability();
double randDouble = rand.nextDouble();
if (randDouble <= diseaseProbability){
setSick();
}
}
/** if sick, countdown to death. If not sick, try to get sick */
protected void checkSick() {
if (sick) {
spreadDisease();
countdown();
}
else {
trySick();
}
}
/** spread disease */
protected void spreadDisease() {
List nearbyLocationList = getField().adjacentLocations(getLocation());
for (int i = 0; i < nearbyLocationList.size(); i++) {
Location theLocation = (Location)nearbyLocationList.get(i);
Animal theAnimal = getField().getObjectAt(theLocation);
if (theAnimal != null){
if (theAnimal.getType() == type){
// let it trySick again (raise the infection chance by raising times)
theAnimal.trySick();
}
}
}
}
/** dead, if dead naturally, grow plant */
protected void setDead(DeathReason reason) {
alive = false;
/** new added for monitor */
deathReason = reason;
if (field != null && location != null) {
field.clear(location);
if (reason == DeathReason.DISEASE || reason == DeathReason.HUNGER
|| reason == DeathReason.OLD) {
field.placePlant(new Plant(field, location), location);
}
}
}
/** location */
protected Location getLocation() { return location; }
protected void setLocation(Location newLocation) {
if (location != null) field.clear(location);
location = newLocation;
field.place(this, newLocation);
}
/** field */
protected Field getField() { return field; }
/** color */
public void setColor(Color col) { color = col; }
public Color getColor() { return color; }
/**
* Get information about a target animal at a specific location.
* @param location The location to check.
* @return The target animal if found, otherwise null.
*/
public Animal getTarget(Location location) {
Field field = getField();
if (field == null) return null; // Ensure field is not null
Animal target = field.getObjectAt(location);
if (target != null && target.isAlive()) {
return target;
}
return null;
}
/** new added for monitor
* @return if animal die, return reason; or return "Still Alive"
*/
public String getDeathReason() {
return isAlive() ? "Still Alive" : deathReason.toString();
}
}