-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrganism.java
325 lines (308 loc) · 9.32 KB
/
Organism.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
package PopSim;
import java.util.Random;
import javax.swing.*;
public abstract class Organism {
private boolean isAlive;
private Species speciesName;
private int organismID;
private int xCoordinate;
private int yCoordinate;
private JLabel label;
private ImageIcon image;
private int nutrients;
private double energy;
private int speed;
private int camouflage;
private int perception;
private int strength;
private int numberOfOffspring;
private double energyToReproduce;
private static double metabolismRate = 0.25;
private static double mutationRate = 0.25;
//Accessor Functions
public Species getSpeciesName() {
return this.speciesName;
}
public int getOrganismID() {
return this.organismID;
}
public int getX() {
return this.xCoordinate;
}
public int getY() {
return this.yCoordinate;
}
public ImageIcon getImage() {
return this.image;
}
public JLabel getLabel() {
return this.label;
}
public boolean getIsAlive() {
return this.isAlive;
}
public double getEnergy() {
return this.energy;
}
public int getNutrients() {
return this.nutrients;
}
public int getSpeed() {
return this.speed;
}
public int getPerception() {
return this.perception;
}
public int getCamouflage() {
return this.camouflage;
}
public int getStrength() {
return this.strength;
}
public double getEnergyToReproduce() {
return this.energyToReproduce;
}
public int getNumOffspring() {
return this.numberOfOffspring;
}
public double getMutationRate() {
return this.mutationRate;
}
public double getMetabolismRate() {
return this.metabolismRate;
}
//Mutator Functions
public void setSpeciesName(Species species) {
this.speciesName = species;
}
public void setIsAlive(boolean b) {
this.isAlive = b;
}
public void setOrganismID() {
this.organismID = this.speciesName.getPopulation().size();
}
public void setX(int x) {
if (x >= 0 && x < this.getSpeciesName().getMap().getXBound()) {
this.xCoordinate = x;
}
}
public void setY(int y) {
if (y >= 0 && y < this.getSpeciesName().getMap().getYBound()) {
this.yCoordinate = y;
}
}
public void setImage(ImageIcon image) {
this.image = image;
}
public void setLabel(JLabel label) {
this.label = label;
}
public void setEnergy(double energy) {
if (energy >= 0 && energy <= 1) {
this.energy = energy;
}
if (this.getEnergy() == 0) {
this.die();
}
}
public void setNutrients(int nutrients) {
if (nutrients >= 0) {
this.nutrients = nutrients;
}
}
public void setSpeed(int tiles) {
if (tiles >= 0) {
this.speed = tiles;
}
}
public void setCamouflage(int tiles) {
if (tiles >= 0) {
this.camouflage = tiles;
}
}
public void setPerception(int tiles) {
if (tiles >= 0) {
this.perception = tiles;
}
}
public void setStrength(int strength) {
if (strength >= 0) {
this.strength = strength;
}
}
public void setNumberOfOffspring(int offspring) {
if (offspring > 0) {
this.numberOfOffspring = offspring;
}
}
public void setEnergyToReproduce(double minimum) {
if (minimum > 0 && minimum < 100) {
this.energyToReproduce = minimum;
}
}
public void drawOrganism() {
//Setting up the label for row
label = new JLabel("");
label.setSize(40, 40);
label.setLocation(this.getY()*50, this.getX()*50);
this.getSpeciesName().getMap().getFrame().add(label);
//Drawing the picture
label.setIcon(this.getImage());
this.getSpeciesName().getMap().getAcre(this.getX(), this.getY()).getLabel().add(label);
this.getSpeciesName().getMap().getFrame().revalidate();
this.getSpeciesName().getMap().getFrame().repaint();
}
//Random movement:
public void move() {
if (this.getIsAlive() == true) {
Random random = new Random();
int direction;
for (int moves = 0; moves < this.speed; moves++) {
this.getSpeciesName().getMap().remove(this);
direction = random.nextInt(4)+1;
if (direction == 1) {
if (this.getX() < this.getSpeciesName().getMap().getYBound()-1) {
this.setX(this.xCoordinate + 1);
//System.out.println("You moved right");
} else {
//System.out.println("You can't move right");
this.setX(this.xCoordinate - 1);
//System.out.println("You moved left");
}
} else if (direction == 2) {
if (this.getX() > 0) {
this.setX(this.xCoordinate - 1);
//System.out.println("You moved left");
} else {
//System.out.println("You can't move left");
this.setX(this.xCoordinate + 1);
//System.out.println("You moved right");
}
} else if (direction == 3) {
if (this.getY() < this.getSpeciesName().getMap().getXBound()-1) {
this.setY(this.yCoordinate + 1);
//System.out.println("You moved down");
} else {
//System.out.println("You can't move down");
this.setY(this.yCoordinate - 1);
//System.out.println("You moved up");
}
} else {
if (this.getY() > 0) {
this.setY(this.yCoordinate - 1);
//System.out.println("You moved up");
} else {
//System.out.println("You can't move up");
this.setY(this.yCoordinate + 1);
//System.out.println("You moved down");
}
}
this.getSpeciesName().getMap().locate(this);
}
}
}
public void eat(int food) {
if (this.getIsAlive() == true) {
this.setNutrients(this.getNutrients()+food);
}
}
public void eat() {
if (this.getIsAlive() == true) {
this.setNutrients(this.getNutrients());
}
}
public void metabolize() {
if (this.getIsAlive() == true) {
if (this.getEnergy() < (1 - (this.getNutrients()*this.getMetabolismRate())/100)) {
this.setEnergy(this.getEnergy()+(this.getNutrients()*this.getMetabolismRate())/100);
this.setNutrients((int)(this.getNutrients()*(1-this.getMetabolismRate())));
} else {
double oldEnergy = this.getEnergy();
this.setEnergy(1);
this.setNutrients((int)(this.getNutrients()*oldEnergy));
}
}
}
public void reproduce() {
if (this.getIsAlive() == true) {
if (this.getEnergy() > this.getEnergyToReproduce()) {
for (int i = 0; i < this.getNumOffspring(); i++) {
Random random = new Random();
int mutation;
Organism child = new Plant(this.getSpeciesName());
//child.getSpeciesName().getMap().remove(child);
mutation = random.nextInt(3) - 1;
if (this.getX() + mutation >= 0 && this.getX() + mutation < this.getSpeciesName().getMap().getXBound()) {
child.setX(this.getX() + mutation);
} else {
child.setX(this.getX());
}
mutation = random.nextInt(3) - 1;
if (this.getY() + mutation >= 0 && this.getX() + mutation < this.getSpeciesName().getMap().getYBound()) {
child.setY(this.getY() + mutation);
} else {
child.setY(this.getY());
}
child.setImage(this.getImage());
child.getSpeciesName().getMap().locate(child);
child.setEnergy(this.getEnergy()/(this.getNumOffspring() + 1));
double speedMutation = random.nextDouble();
if (speedMutation > this.getMutationRate()) {
mutation = random.nextInt(3) - 1;
if (this.getSpeed() + mutation >= 0) {
child.setSpeed(this.getSpeed() + mutation);
}
} else {
child.setSpeed(this.getSpeed());
}
double perceptionMutation = random.nextDouble();
if (perceptionMutation > this.getMutationRate()) {
mutation = random.nextInt(3) - 1;
if (this.getPerception() + mutation >= 0) {
child.setPerception(this.getPerception() + mutation);
}
} else {
child.setPerception(this.getPerception());
}
double camouflageMutation = random.nextDouble();
if (camouflageMutation > this.getMutationRate()) {
mutation = random.nextInt(3) - 1;
if (this.getCamouflage() + mutation >= 0) {
child.setCamouflage(this.getCamouflage() + mutation);
}
} else {
child.setCamouflage(this.getCamouflage());
}
double strengthMutation = random.nextDouble();
if (strengthMutation > this.getMutationRate()) {
mutation = random.nextInt(3) - 1;
if (this.getStrength() + mutation >= 0) {
child.setStrength(this.getStrength() + mutation);
}
} else {
child.setStrength(this.getStrength());
}
this.getSpeciesName().setAvgSpe();
this.getSpeciesName().setAvgPer();
this.getSpeciesName().setAvgCam();
this.getSpeciesName().setAvgStr();
child.setNumberOfOffspring(this.getNumOffspring());
child.setEnergyToReproduce(this.getEnergyToReproduce());
}
this.setEnergy(this.getEnergy()/(this.getNumOffspring() + 1));
}
}
}
public void die() {
this.setIsAlive(false);
this.getSpeciesName().getMap().getFrame().remove(this.getLabel());
this.getLabel().setIcon(null);
this.setLabel(null);
this.getSpeciesName().setSize(this.getSpeciesName().getSize() - 1);
}
public String toString() {
String s = "";
s += "\n"+this.speciesName.getSpeciesName()+this.organismID+" ("+this.xCoordinate+","+this.yCoordinate+") Energy: "+this.energy+" Nutrients: "+this.nutrients+" Strength: "+this.strength+" Speed: "+this.speed+" Perception: "+this.perception+" camouflage: "+this.camouflage;
return s;
}
}