Repository navigation
Expand file tree
/
Copy pathPrey.java
More file actions
280 lines (249 loc) · 7.72 KB
/
Copy pathPrey.java
File metadata and controls
280 lines (249 loc) · 7.72 KB
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
class Prey extends Creature
{
//make int[] to store population data, used in graph
protected static int []population = new int[0];
//variables for movement
private int moveCount = 25;
private int direction = 0;
//amount of hunger that is satisfied by eating 1 food
public static int foodEnergy = 4;
//will decrease every turn, die at zero, increase by eating
private static int hunger = 15;
private int hungerCounter = hunger;
protected static int foodRequired = 5;//reproduce if hunger > x
protected static int repCoolDown = 5;//takes x turns before it can reproduce again
//set up reproduction variables
protected int repCoolDownCounter = 5;//only reproduce every x turns
//constructor
public Prey(int row, int col)
{
super(row, col);
live = true;
}
//accessor methods
public int getFoodEnergy()
{
return foodEnergy;
}
public int getHunger()
{
return hunger;
}
public int getFoodRequired()
{
return foodRequired;
}
public int getRepCoolDown()
{
return repCoolDown;
}
//able to change energy levels of food
public static void setFoodEnergy(int num)
{
foodEnergy = num;
}
public static void setHunger(int num)
{
hunger = num;
}
public static void setFoodRequired(int num)
{
foodRequired = num;
}
public static void setRepCoolDown(int num)
{
repCoolDown = num;
}
//check if it can reproduce, update boolean accordingly
public void updateReproduce ()
{
if (hungerCounter>foodRequired && repCoolDownCounter <0)//can reproduce
{
reproduce = true;
repCoolDownCounter = repCoolDown;//set repCoolDownCounter
}
else//can't reproduce
{
reproduce = false;
repCoolDownCounter--;//decrease counter by 1
}
}
//check if it can still live
public void updateLive()
{
if(hungerCounter<=0)//dies when hunger falls below 0
live = false;
}
//accepts grid and changes row and col in creature object accordingly to move
//movement for prey
public void preyMove (Life[][]grid)
{
int moveX = 0;
int moveY = 0;
double random = Math.random();
//decides general direction for the prey to move for next 10 turns
//greater probability that prey will move in that direction for 10 turns
//allows prey to move beyond small enclosed range
if(moveCount == 0 || moveCount == 25)//enter only for 1st time or when moveCount is 0
{
//1 = right, 2 = up, 3 = left, 4 = down;
if(random>0.75)
direction = 1;
else if(random>0.5)
direction = 2;
else if(random>0.25)
direction = 3;
else
direction = 4;
//reset moveCount
moveCount = 24;
}
//only do if it is within moveCount
if(moveCount>0)
{
random = Math.random();
//40% chance of moving right, 20% of moving in other 3 directions
if(direction == 1)//right
{
if(random>0.6)
moveX = 1;
else if(random>0.4)
moveX = -1;
else if(random>0.2)
moveY = 1;
else
moveY = -1;
}
else if(direction == 2)//up
{
if(random>0.6)
moveY = -1;
else if(random>0.4)
moveX = -1;
else if(random>0.2)
moveX = 1;
else
moveY = 1;
}
else if(direction == 3)//left
{
if(random>0.6)
moveX = -1;
else if(random>0.4)
moveY = -1;
else if(random>0.2)
moveX = 1;
else
moveY = 1;
}
else if(direction == 4)//down
{
if(random>0.6)
moveY = 1;
else if(random>0.4)
moveY = -1;
else if(random>0.2)
moveX = 1;
else
moveX = -1;
}
moveCount--;
}
//check it is within size of grid
//update row and col variables
if(row+moveX < grid.length && row+moveX >= 0)
row = row+moveX;
if(col+moveY <grid[0].length && col+moveY >= 0)
col = col + moveY;
//always able to move, so always reduce hunger
hungerCounter--;
}
//accepts Life[][] and returns x and y values of food to be eaten
public int[] eat(Life[][] grid)
{
int eaten = 0;//counter for eat, to add to hunger
//set up int[] to store x and y
int[] position = new int[2];
position[0] = -1;
position[1] = -1;
//hard coded all cases and corresponding x and y values
if (grid[row][col] instanceof Food)
{
eaten ++;
position[0] = row;
position[1] = col;
}
else if (row-1 >= 0 && grid[row-1][col] instanceof Food)
{
eaten ++;
position[0] = row-1;
position[1] = col;
}
else if (row-1 >= 0 && col-1 >= 0 && grid[row-1][col-1] instanceof Food)
{
eaten++;
position[0] = row-1;
position[1] = col-1;
}
else if (row-1 >= 0 && col+1 < grid[0].length && grid[row-1][col+1] instanceof Food)
{
eaten++;
position[0] = row-1;
position[1] = col+1;
}
else if (row+1 < grid.length && grid[row+1][col] instanceof Food)
{
eaten++;
position[0] = row+1;
position[1] = col;
}
else if (row+1 < grid.length && col-1 >= 0 && grid[row+1][col-1] instanceof Food)
{
eaten++;
position[0] = row+1;
position[1] = col-1;
}
else if (row+1 < grid.length && col+1 < grid[0].length && grid[row+1][col+1] instanceof Food )
{
eaten++;
position[0] = row+1;
position[1] = col+1;
}
else if (col-1 >= 0 && grid[row][col-1] instanceof Food )
{
eaten++;
position[0] = row;
position[1] = col-1;
}
else if(col+1 < grid[0].length && grid[row][col] instanceof Food)
{
eaten++;
position[0] = row;
position[1] = col+1;
}
//eaten something
if(position[0] != -1 && position[1]!=-1)
{
hungerCounter = hungerCounter + eaten*foodEnergy;//update hunger
}
return position;
}
//accepts num (population value at that generation) and adds it to int[] population
public static void updatePopulation(int num)
{
add(num);
}
//add int to population
private static void add(int a)
{
int[] temp = new int[population.length+1];
for(int i = 0; i<population.length; i++)
temp[i] = population[i];
temp[temp.length-1] = a;
population = temp;
}
public static void reset()
{
population = new int[0];
}
}