-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovementRule.pde
166 lines (149 loc) · 5.2 KB
/
MovementRule.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
import java.util.LinkedList;
import java.util.Collections;
interface MovementRule {
public Square move(LinkedList<Square> neighborhood, SugarGrid g, Square middle);
}
class SugarSeekingMovementRule implements MovementRule {
/* The default constructor. For now, does nothing.
*
*/
public SugarSeekingMovementRule() {
}
/* For now, returns the Square containing the most sugar.
* In case of a tie, use the Square that is closest to the middle according
* to g.euclidianDistance().
* Squares should be considered in a random order (use Collections.shuffle()).
*/
public Square move(LinkedList<Square> neighborhood, SugarGrid g, Square middle) {
Square retval = neighborhood.peek();
Collections.shuffle(neighborhood);
for (Square s : neighborhood) {
if (s.getSugar() > retval.getSugar() ||
(s.getSugar() == retval.getSugar() &&
g.euclideanDistance(s, middle) < g.euclideanDistance(retval, middle)
)
) {
retval = s;
}
}
return retval;
}
}
class PollutionMovementRule implements MovementRule {
/* The default constructor. For now, does nothing.
*
*/
public PollutionMovementRule() {
}
/* For now, returns the Square containing the most sugar.
* In case of a tie, use the Square that is closest to the middle according
* to g.euclidianDistance().
* Squares should be considered in a random order (use Collections.shuffle()).
*/
public Square move(LinkedList<Square> neighborhood, SugarGrid g, Square middle) {
Square retval = neighborhood.peek();
Collections.shuffle(neighborhood);
boolean bestSquareHasNoPollution = (retval.getPollution() == 0);
for (Square s : neighborhood) {
boolean newSquareCloser = (g.euclideanDistance(s, middle) < g.euclideanDistance(retval, middle));
if (s.getPollution() == 0) {
if (!bestSquareHasNoPollution || s.getSugar() > retval.getSugar() ||
(s.getSugar() == retval.getSugar() && newSquareCloser)
) {
retval = s;
}
}
else if (!bestSquareHasNoPollution) {
float newRatio = s.getSugar()*1.0/s.getPollution();
float curRatio = retval.getSugar()*1.0/retval.getPollution();
if (newRatio > curRatio || (newRatio == curRatio && newSquareCloser)) {
retval = s;
}
}
}
return retval;
}
}
class CombatMovementRule extends SugarSeekingMovementRule{
int alpha;
public CombatMovementRule(int alpha){
this.alpha = alpha;
}
public Square move(LinkedList<Square> neighbourhood,SugarGrid g, Square middle){
Square target = null;
Agent casualty;
LinkedList<Square> squareList = neighbourhood;
LinkedList<Square> updatedSquareList = new LinkedList<Square>();
for(Square s:squareList){
// rule 1. if of the same tribe remove
if(middle.getAgent().getTribe() == s.getAgent().getTribe()){
squareList.remove(s);
continue;
}
// rule 2. if s's agent has more sugar, remove
if(middle.getAgent().getSugarLevel() <= s.getAgent().getSugarLevel()){
squareList.remove(s);
continue;
}
}
for(Square s:squareList){
// rule 3
if(s.getAgent() != null){
// find the middle agent's vision of they moved to that square
LinkedList<Square> potentialVision = g.generateVision(s.getX(),s.getY(),middle.getAgent().getVision());
// for each square in potential vision, check if the the square's agent has more sugar than middle's agent and if its from a different tribe
for(Square ss: potentialVision){
// if the potential agent has more sugar than you and is from a different tribe remove that square
if(ss.getAgent().getSugarLevel() > middle.getAgent().getSugarLevel() && ss.getAgent().getTribe() != middle.getAgent().getTribe()) squareList.remove(s);
}
}
}
// rule 4 update sugar levels and max sugar levels to account for the agents sugar
for(Square s: squareList){
if(s.getAgent() != null){
int newSugar = s.getSugar() + alpha;
int newMaxSugar = s.getMaxSugar() + s.getAgent().getSugarLevel();
updatedSquareList.add(new Square(s.getX(),s.getY(),newSugar,newMaxSugar));
}
}
// rule 5
Square targetSquare = super.move(updatedSquareList,g,middle);
// find square corresponding to targetSquare
for(Square s: squareList){
if(targetSquare == null){
// if targetSquare null don't do anything
}
else if(s.getX() == targetSquare.getX() && s.getY() == targetSquare.getY()){
target = s;
}
}
// rule 6
if(target.getAgent() == null){
return target;
}
// rule 7
else{
casualty = target.getAgent();
// rule 8
target.setAgent(null);
// rule 9
middle.getAgent().sugarLevel += Math.min(alpha,casualty.getSugarLevel());
// rule 10
g.killAgent(casualty);
// rule 11
return target;
}
}
}
class SugarSeekingMovementRuleTester {
public void test() {
SugarSeekingMovementRule mr = new SugarSeekingMovementRule();
//stubbed
}
}
class PollutionMovementRuleTester {
public void test() {
PollutionMovementRule mr = new PollutionMovementRule();
//stubbed
}
}