-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplacementRule.pde
98 lines (89 loc) · 3.54 KB
/
ReplacementRule.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
import java.util.Collections;
import java.util.Random;
import java.util.List;
import java.util.HashMap; // added
class ReplacementRule {
private int minAge;
private int maxAge;
HashMap<Agent,Integer> maxAgeMap; // added
AgentFactory fac;
/* initializes a replacement rule with the specified values for minimum and maximum lifespans.
* fac will be used to generate any new random agents.
*/
public ReplacementRule(int minAge, int maxAge, AgentFactory fac) {
this.minAge = minAge;
this.maxAge = maxAge;
this.fac = fac;
maxAgeMap = new HashMap<Agent,Integer>(); // added
}
/* This method accepts an Agent a, and determines whether the agent should be replaced yet.
* a should be replaced if it is no longer alive.
*
* Further, if this is the first time ReplacementRule has been asked about Agent a,
* then generate a random integer between minAge and maxAge to represent the lifespan of a.
*
* The assignment has this to say:
* Store this integer in a List as well as storing a reference to Agent a
* (you may want to make an additional class to facilitate this, or use two lists).
*
* Instead, we get and store the lifespan of a in a.lifespan using a.getLifespan() and a.setLifespan().
*
* When replaceThisOne(a) is subsequently called, it checks whether the age of a exceeds the randomly generated lifespan.
* If it does, then return true regardless of whether a is alive or not and set the age of a to maxAge + 1.
* The last step ensures that function will return a consistent value after an agent has gotten too old.
*
* If a is null, returns false (the assignment says, "you may return any value").
*/
public boolean replaceThisOne(Agent a) {
if (a.getLifespan() == Agent.NOLIFESPAN) {
Random r = new Random();
int lifeSpan = minAge + r.nextInt(maxAge+1 - minAge); // added
maxAgeMap.put(a,lifeSpan); // added
a.setLifespan(lifeSpan); // slightly changed
}
else if(a == null){
maxAgeMap.remove(a);
}
else {
if (a.getAge() > a.getLifespan()) {
a.setAge(maxAge+1);
return true;
}
}
return false;
}
/* Returns a new Agent that is a replacement for Agent a.
* For now, both a and others should be ignored, though in future we might design rules that will use them.
* By including the parameters now, we can ensure that tests we write today will still work in the future.}
*/
public Agent replace(Agent a, List<Agent> others) {
return fac.makeAgent();
}
}
class ReplacementRuleTester {
public void test() {
int minMetabolism = 3;
int maxMetabolism = 6;
int minVision = 2;
int maxVision = 4;
int minInitialSugar = 5;
int maxInitialSugar = 10;
MovementRule mr = new PollutionMovementRule();
AgentFactory af = new AgentFactory(minMetabolism, maxMetabolism, minVision, maxVision,
minInitialSugar, maxInitialSugar, mr);
int minAge = 40;
int maxAge = 80;
ReplacementRule rr = new ReplacementRule(minAge, maxAge, af);
Agent a = af.makeAgent();
assert(rr.replaceThisOne(a) == false);
int ls = a.getLifespan();
assert(ls <= maxAge && ls >= minAge);
assert(rr.maxAgeMap.get(a) <= maxAge || rr.maxAgeMap.get(a) >= minAge);
a.step();
a.setLifespan(a.getAge() - 1);
assert(rr.replaceThisOne(a) == true);
List<Agent> others = null;
a = rr.replace(a, others);
assert(rr.replaceThisOne(a) == false);
}
}