-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ant.java
215 lines (198 loc) · 5.09 KB
/
Ant.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
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* An ant that collects food.
*
* @author Michael Kölling
* @version 1.1
*/
public class Ant extends Creature
{
/** */
public static final int PRICE = 5;
/** */
protected static final boolean IS_ALIVE = true;
/** */
protected static final boolean IS_DEAD = false;
/** How much food increases HP´s */
protected static final int CALORIESPERFOOD = 10;
/** How much energy is destroyed from walking */
protected static final int CALORIESBYSTEP = 1;
/**
* Every how many steps can we place a pheromone drop.
*/
private static final int MAX_PH_LEVEL = 18;
/**
* How long do we keep direction after finding pheromones.
*/
private static final int PH_TIME = 30;
/** */
private static final int MAXLP = 3000;
/** Indicate whether we have any food with us. */
private boolean carryingFood = false;
/** How much pheromone do we have right now. */
private int pheromoneLevel = MAX_PH_LEVEL;
/** How well do we remember the last pheromone - larger number: more recent */
private int foundLastPheromone = 0;
/** How vital are you? - larger number: */
private int LP = 350;
/**
* Create an ant with a given home hill. The initial speed is zero (not moving).
*/
public Ant(AntHill home) {
setHomeHill(home);
home.newAnt();
}
@Deprecated
public Ant() {
}
/**
* Do what an ant's gotta do.
*/
@Override
public void act()
{
if (checkLiveStat()) {
if (this.carryingFood) {
walkTowardsHome();
handlePheromoneDrop();
checkHome();
} else {
searchForFood();
}
} else {
super.getHomeHill().antIsDieing();
getWorld().removeObject(this);
}
}
/**
* Walk around in search of food.
*/
private void searchForFood()
{
if (this.foundLastPheromone > 0) { // if we can still remember...
this.foundLastPheromone--;
walkAwayFromHome();
}
else if (smellPheromone()) {
walkTowardsPheromone();
}
else {
randomWalk();
}
checkFood();
}
/**
* Are we home? Drop the food if we are, and start heading back out.
*/
private void checkHome()
{
if (atHome()) {
dropFood();
}
}
/**
* Are we home?
*/
private boolean atHome()
{
if (getHomeHill() != null) {
return (Math.abs(getX() - getHomeHill().getX()) < 4) && (Math.abs(getY() - getHomeHill().getY()) < 4);
}
else {
return false;
}
}
/**
* Is there any food here where we are? If so, take some!.
*/
public void checkFood()
{
Food food = (Food) getOneIntersectingObject(Food.class);
if (food != null) {
takeFood(food);
}
}
/**
* Take some food from a fool pile.
*/
private void takeFood(Food food)
{
this.carryingFood = true;
food.takeSome();
setImage("ant-with-food.gif");
}
/**
* Drop our food in the ant hill.
*/
private void dropFood()
{
this.carryingFood = false;
getHomeHill().countFood();
setImage("ant.gif");
}
/**
* Check whether we can drop some pheromone yet. If we can, do it.
*/
private void handlePheromoneDrop()
{
if (pheromoneLevel == MAX_PH_LEVEL) {
Pheromone ph = new Pheromone();
getWorld().addObject(ph, getX(), getY());
pheromoneLevel = 0;
}
else {
pheromoneLevel++;
}
}
/**
* Check whether we can smell pheromones. If we can, return true, otherwise return false.
*/
public boolean smellPheromone()
{
Actor ph = getOneIntersectingObject(Pheromone.class);
return (ph != null);
}
/**
* If we can smell some pheromone, walk towards it. If not, do nothing.
*/
public void walkTowardsPheromone()
{
Actor ph = getOneIntersectingObject(Pheromone.class);
if (ph != null) {
headTowards(ph);
walk();
if (ph.getX() == getX() && ph.getY() == getY()) {
foundLastPheromone = PH_TIME;
}
}
}
/**
* If we walk, we will lose health points (LP).
*/
private void looseLive() {
this.LP -= Ant.CALORIESBYSTEP;
}
/**
* If we eat we will gain live.
*/
private void gainLive() {
if (this.LP <= Ant.MAXLP) {
this.LP += Ant.CALORIESPERFOOD;
}
}
/**
* I do not have sufficient LP
* */
protected boolean checkLiveStat() {
if ((this.LP) > 0) {
if (this.carryingFood) {
this.gainLive();
} else {
this.looseLive();
}
return Ant.IS_ALIVE;
}else{
return Ant.IS_DEAD;
}
}
}