-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSheep.java
More file actions
342 lines (298 loc) · 8.78 KB
/
Sheep.java
File metadata and controls
342 lines (298 loc) · 8.78 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import java.util.Random;
/*
* Entity.java
* @version 1.0
* @since April 24, 2019
* Base class for all other entities in simulation
*/
class Sheep extends Entity{
// Random object for random number generation
Random rand = new Random();
// Moved condition (ensure one move per turn)
private boolean moved;
// Gender integer - 0 = male, 1 = female
private final int gender;
// Breeding cooldown counter
private int breedingCoolDown = 0;
// Constant minimum cooldown before breeding
private final int MINIMUMCOOLDOWN = 3;
// Number of offspring produced (known as fitness)
private int fitness = 0;
Sheep(int health, int y, int x) {
super(health, y, x);
this.moved = true;
this.gender = rand.nextInt(2);
}
/**
* getGender
* Retrieves the gender of the sheep
* @return int, Gender of the Sheep
*/
int getGender() {
return this.gender;
}
/**
* getMoveCondition
* Retrieves boolean condition for being moved
* @return boolean, True if moved this turn, false if not
*/
boolean getMoveCondition() {
return moved;
}
/**
* setMoveCondition
* Sets new condition for being moved this turn
* @param condition, New boolean condition for being moved
*/
void setMoveCondition(boolean condition) {
this.moved = condition;
}
/**
* getBreedingCoolDown
* Retrieves the current breeding cooldown count
* @return int, Turns since last breeding occured
*/
int getBreedingCoolDown() {
return this.breedingCoolDown;
}
/**
* addCoolDown()
* Adds one to breeding cooldown counter
*/
void addCoolDown() {
this.breedingCoolDown +=1;
}
/**
* resetCoolDown()
* Resets breeding cooldown to zero
*/
void resetCoolDown() {
this.breedingCoolDown = 0;
}
/**
* addFitness()
* Adds one fitness count, used when new offspring produced
*/
void addFitness() {
this.fitness +=1;
}
/**
* getFitness()
* Retrieves current fitness count
* @return int, Current fitness count
*/
int getFitness() {
return this.fitness;
}
/**
* checkBreedable()
* Checks if the entity being tested is breedable to the current sheep
* @param testSheep, The potential mate
* @return boolean, True if sheep is breedable, false if not
*/
private boolean checkBreedable(Entity testSheep) {
boolean breedable = true;
// Health
if ((this.getHealth() < 25) || (((Sheep)testSheep).getHealth() < 25)) {
breedable = false;
}
// Gender
if ((this.gender + (((Sheep)testSheep).getGender())) != 1) {
breedable = false;
}
// Cooldown
if ((this.breedingCoolDown < MINIMUMCOOLDOWN) || (((Sheep)testSheep).getBreedingCoolDown() < MINIMUMCOOLDOWN)) {
breedable = false;
}
return breedable;
}
/**
* checkEatable()
* Checks if the space being tested contains an eatable grass
* @param entityArray, the current map
* @param testY, Test Y coordinate
* @param testX, Test X coordinate
* @return boolean, True if spot contains a grass, false if not
*/
private boolean checkEatable(Entity[][] entityArray, int testY, int testX) {
if (entityArray[testY][testX] instanceof Grass) {
return true;
} else {
return false;
}
}
/**
* checkValidMove()
* Checks if the proposed move is valid, used for random generation
* @param entityArray, the current map
* @param testY, Test Y coordinate
* @param testX, Test X coordinate
* @return boolean, True if spot is a valid move, false if not
*/
private boolean checkValidMove(Entity[][] entityArray, int testY, int testX) {
if (entityArray[testY][testX] instanceof Sheep) {
Entity refEntity = entityArray[testY][testX];
if (checkBreedable(refEntity) == true) {
return true;
} else {
return false;
}
} else if (entityArray[testY][testX] instanceof Wolf){
return false;
} else if (entityArray[testY][testX] == null) {
return true;
} else {
return true;
}
}
/**
* generateSmartMove()
* Determines move based on current health conditions
* @param entityArray, the current map
* @param coords, Array of current coordinates
* @param maxIndex, Limit index of map
* @return String, The letter representing a move of either up, down, left, right, or stay (if random generation is reached)
*/
String generateSmartMove(Entity[][] entityArray, int[] coords, int maxIndex) {
int direction;
int currentY = coords[0];
int currentX = coords[1];
String possibleMoves;
// Prioritize breeding;
if (this.getHealth() >= 25) {
if (currentY > 0) {
if (entityArray[currentY-1][currentX] instanceof Sheep) {
if (checkBreedable(entityArray[currentY-1][currentX]) == true) {
return "u";
}
}
}
if (currentY < maxIndex) {
if (entityArray[currentY+1][currentX] instanceof Sheep) {
if (checkBreedable(entityArray[currentY+1][currentX]) == true) {
return "d";
}
}
}
if (currentX > 0) {
if (entityArray[currentY][currentX-1] instanceof Sheep) {
if (checkBreedable(entityArray[currentY][currentX-1]) == true) {
return "l";
}
}
}
if (currentX < maxIndex) {
if (entityArray[currentY][currentX+1] instanceof Sheep) {
if (checkBreedable(entityArray[currentY][currentX+1]) == true) {
return "r";
}
}
}
// Health less than 20 - prioritize eating
} else if (this.getHealth() < 25) {
if (currentY > 0) {
if (checkEatable(entityArray, currentY-1, currentX) == true) {
return "u";
}
}
if (currentY < maxIndex) {
if (checkEatable(entityArray, currentY+1, currentX) == true) {
return "d";
}
}
if (currentX > 0) {
if (checkEatable(entityArray, currentY, currentX-1) == true) {
return "l";
}
}
if (currentX < maxIndex) {
if (checkEatable(entityArray, currentY, currentX+1) == true) {
return "r";
}
}
}
return generateRandomMove(entityArray, coords, maxIndex);
}
/**
* generateRandomMove()
* Generates a random move based on current coordinates, checks if move is valid before sending,
* only called when a smarter move cannot be determined (nothing useful around)
* @param entityArray, the current map
* @param coords, Array of current coordinates
* @param maxIndex, Maximum index of the map
* @return String, A String representing the move
*/
String generateRandomMove(Entity[][] entityArray, int[] coords, int maxIndex) {
int direction;
int currentY = coords[0];
int currentX = coords[1];
boolean generateMove = true;
String move = "";
while (generateMove == true) {
direction = rand.nextInt(5);
// Up
if (direction == 0) {
if (currentY > 0) {
if (checkValidMove(entityArray, currentY-1, currentX) == true) {
move = "u";
generateMove = false;
}
}
// Down
} else if (direction == 1) {
if (currentY < maxIndex) {
if (checkValidMove(entityArray, currentY+1, currentX) == true) {
move = "d";
generateMove = false;
}
}
// Left
} else if (direction == 2) {
if (currentX > 0) {
if (checkValidMove(entityArray, currentY, currentX-1) == true) {
move = "l";
generateMove = false;
}
}
// Right
} else if (direction == 3) {
if (currentX < maxIndex) {
if (checkValidMove(entityArray, currentY, currentX+1) == true) {
move = "r";
generateMove = false;
}
}
} else if (direction == 4) {
move = "s";
generateMove = false;
}
}
return move;
}
/**
* getMove()
* Gets a move with other methods, translates into an array of new coordinates
* @param entityArray, the current map
* @return coords, Array of new Coordinates
*/
int[] getMove(Entity[][] entityArray) {
int[] coords = {this.getY(), this.getX()};
int maxIndex = entityArray.length-1;
String move = generateSmartMove(entityArray, coords, maxIndex);
// 0 = up
// 1 = down
// 2 = left
// 3 = right
// 4 = stay
if (move.equals("u")) {
coords[0] -= 1;
} else if (move.equals("d")) {
coords[0] += 1;
} else if (move.equals("l")) {
coords[1] -= 1;
} else if (move.equals("r")) {
coords[1] += 1;
}
return coords;
}
}