-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.pde
280 lines (241 loc) · 6.31 KB
/
Agent.pde
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
import java.lang.Math;
import java.util.Random;
class Agent {
public static final int NOLIFESPAN = -999;
private int metabolism;
private int vision;
private int sugarLevel;
private MovementRule movementRule;
private int age;
private int lifespan;
private Square square;
private char sex;
private boolean[] culture;
Random r = new Random();
/* initializes a new Agent with the specified values for its
* metabolism, vision, stored sugar, and movement rule.
*
*/
public Agent(int metabolism, int vision, int initialSugar, MovementRule m) {
if(r.nextBoolean()){
this.sex = 'Y';
}
else{
this.sex = 'X';
}
this.metabolism = metabolism;
this.vision = vision;
this.sugarLevel = initialSugar;
this.movementRule = m;
this.culture = new boolean[11];
for(int i = 0; i < 11;i++){
culture[i] = r.nextBoolean();
}
age = 0;
lifespan = NOLIFESPAN;
square = null;
}
public Agent(int metabolism, int vision, int initialSugar, MovementRule m,char sex) {
assert(sex == 'X' || sex == 'Y');
this.sex = sex;
this.metabolism = metabolism;
this.vision = vision;
this.sugarLevel = initialSugar;
this.movementRule = m;
this.culture = new boolean[11];
age = 0;
lifespan = NOLIFESPAN;
square = null;
}
/* returns the amount of food the agent needs to eat each turn to survive.
*
*/
public int getMetabolism() {
return metabolism;
}
public char getSex(){
return sex;
}
/* returns the agent's vision radius.
*
*/
public int getVision() {
return vision;
}
/* returns the amount of stored sugar the agent has right now.
*
*/
public int getSugarLevel() {
return sugarLevel;
}
/* returns the Agent's movement rule.
*
*/
public MovementRule getMovementRule() {
return movementRule;
}
/* returns the Agent's age.
*
*/
public int getAge() {
return age;
}
/* sets the Agent's age.
*
*/
public void setAge(int howOld) {
assert(howOld >= 0);
this.age = howOld;
}
/* returns the Agent's lifespan.
*
*/
public int getLifespan() {
return lifespan;
}
/* sets the Agent's lifespan.
*
*/
public void setLifespan(int span) {
assert(span >= 0);
this.lifespan = span;
}
/* returns the Square occupied by the Agent.
*
*/
public Square getSquare() {
return square;
}
/* sets the the Square occupied by the Agent.
*
*/
public void setSquare(Square s) {
this.square = s;
}
/* Moves the agent from source to destination.
* If the destination is already occupied, the program should crash with an assertion error
* instead, unless the destination is the same as the source.
*
*/
public void move(Square source, Square destination) {
// make sure this agent occupies the source
assert(this == source.getAgent());
if (!destination.equals(source)) {
assert(destination.getAgent() == null);
source.setAgent(null);
destination.setAgent(this);
}
}
/* Reduces the agent's stored sugar level by its metabolic rate, to a minimum value of 0.
*
*/
public void step() {
sugarLevel = Math.max(0, sugarLevel - metabolism);
age += 1;
}
/* returns true if the agent's stored sugar level is greater than 0, false otherwise.
*
*/
public boolean isAlive() {
return (sugarLevel > 0);
}
/* The agent eats all the sugar at Square s.
* The agent's sugar level is increased by that amount, and
* the amount of sugar on the square is set to 0.
*
*/
public void eat(Square s) {
sugarLevel += s.getSugar();
s.setSugar(0);
}
public void gift(Agent other,int amount){
assert(this.getSugarLevel() >= amount);
other.sugarLevel += amount;
this.sugarLevel -= amount;
}
public void influence(Agent other){
int rand = r.nextInt(11);
if(this.culture[rand] != other.culture[rand]) other.culture[rand] = this.culture[rand];
}
public void nurture(Agent parent1,Agent parent2){
for(int i = 0; i < 11; i++){
if(r.nextBoolean()) this.culture[i] = parent1.culture[i];
else this.culture[i] = parent2.culture[i];
}
}
public boolean getTribe(){
int trueVals = 0;
int falseVals = 0;
for(int i = 0; i < 11; i++){
if(this.culture[i]) trueVals++;
else falseVals++;
}
if(trueVals > falseVals) return true;
else return false;
}
/* Two agents are equal only if they're the same agent,
* not just if they have the same properties.
*/
public boolean equals(Agent other) {
return this == other;
}
public void display(int x, int y, int scale) {
fill(0);
ellipse(x, y, 3.0*scale/4, 3.0*scale/4);
}
public void display(int x, int y, int scale,boolean culture,boolean fertility,FertilityRule f) {
if(culture){
if(getTribe()) fill(41,24,237);
else fill(237,89,92);
}
else if(fertility){
if(f.isFertile(this)) fill(252,5,212);
else fill(5,252,81);
}
else fill(0);
stroke(0);
ellipse(x, y, 3.0*scale/4, 3.0*scale/4);
fill(0);
}
}
class AgentTester {
public void test() {
// test constructor, accessors
int metabolism = 3;
int vision = 2;
int initialSugar = 4;
MovementRule m = null;
Agent a = new Agent(metabolism, vision, initialSugar, m);
Agent a1 = new Agent(metabolism,vision,initialSugar,m,'X');
Agent a2 = new Agent(metabolism,vision,initialSugar,m,'Y');
assert(a1.getSex() == 'X');
assert(a2.getSex() == 'Y');
assert(a.getSex() == 'X' || a.getSex() == 'Y');
a1.gift(a2,1);
assert(a1.getSugarLevel() == 3);
assert(a2.getSugarLevel() == 5);
assert(a.isAlive());
assert(a.getMetabolism() == 3);
assert(a.getVision() == 2);
assert(a.getSugarLevel() == 4);
assert(a.getMovementRule() == null);
// movement
Square s1 = new Square(5, 9, 10, 10);
Square s2 = new Square(5, 9, 12, 12);
s1.setAgent(a);
a.move(s1, s2);
assert(s2.getAgent().equals(a));
// eat
a.eat(s2);
assert(a.getSugarLevel() == 9);
// test get/set MovementRule
// step
a.step();
assert(a.getSugarLevel() == 6);
a.step();
a.step();
a.step();
assert(a.getSugarLevel() == 0);
assert(!a.isAlive());
}
}