-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiger.java
More file actions
240 lines (227 loc) · 7.71 KB
/
Tiger.java
File metadata and controls
240 lines (227 loc) · 7.71 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
import java.util.List;
import java.util.Iterator;
import java.util.Random;
/**
* A simple model of a Tiger.
* Tigers age, move, eat consumers, and die.
*
* @author Reibjok Othow and Kwan Yui Chiu
* @version 27/02/2022
*/
public class Tiger extends ApexPredator
{
// Characteristics shared by all Tigers (class variables).
// The age at which a Tiger can start to breed.
private static final int BREEDING_AGE = 40;
// The age to which a Tiger can live.
private static final int MAX_AGE = 150;
// The likelihood of a Tiger breeding.
private static final double BREEDING_PROBABILITY = 0.051;
// The maximum number of births.
private static final int MAX_LITTER_SIZE = 5;
// The food value of a single rabbit. In effect, this is the
// number of steps a Tiger can go before it has to eat again.
private static final int RABBIT_FOOD_VALUE = 100;
// number of steps a Tiger can go before it has to eat again.
private static final int MOUSE_FOOD_VALUE = 100;
// number of steps a Tiger can go before it has to eat again.
private static final int FOX_FOOD_VALUE = 100;
// number of steps a Tiger can go before it has to eat again.
private static final int GIRAFFE_FOOD_VALUE = 100;
// A shared random number generator to control breeding.
private static final Random rand = Randomizer.getRandom();
// Individual characteristics (instance fields).
// The Tiger's age.
private int age;
// The Tiger's food level, which is increased by eating rabbits.
private int foodLevel;
/**
* Create a Tiger. A Tiger can be created as a new born (age zero
* and not hungry) or with a random age and food level.
*
* @param randomAge If true, the Tiger will have random age and hunger level.
* @param female whether or not the tiger is female
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Tiger(boolean randomAge,boolean female, Field field, Location location)
{
super(female, field, location);
if(randomAge) {
age = rand.nextInt(MAX_AGE);
foodLevel = rand.nextInt(RABBIT_FOOD_VALUE);
}
else {
age = 0;
foodLevel = RABBIT_FOOD_VALUE;
}
}
/**
* This is what the Tiger does most of the time: it hunts for
* consumers. In the process, it might breed, die of hunger,
* or die of old age.
* The tigers only hunt for food in the night
* Only female tiger are able to breed
* @param newTigers A list to return newly born Tigers.
*/
public void act(List<Entity> newTigers)
{
incrementAge();
incrementHunger();
if(isAlive()) {
if(isFemale()){
giveBirth(newTigers);
}
// Move towards a source of food if found.
Location newLocation = null;
if (!getField().isDay()){
newLocation = findFood();
}
if(newLocation == null) {
// No food found - try to move to a free location.
newLocation = getField().freeAdjacentLocation(getLocation());
}
// See if it was possible to move.
if(newLocation != null && !getField().isDay()) {
setLocation(newLocation);
}
else if(newLocation == null){
// Overcrowding.
setDead();
}
}
}
/**
* Increase the age. This could result in the Tiger's death.
*/
private void incrementAge()
{
age++;
if(age > MAX_AGE) {
setDead();
}
}
/**
* Make this Tiger more hungry. This could result in the Tiger's death.
*/
private void incrementHunger()
{
foodLevel--;
if(foodLevel <= 0) {
setDead();
}
}
/**
* Look for consumers adjacent to the current location.
* Only the first live consumer is eaten.
* @return Where food was found, or null if it wasn't.
*/
protected Location findFood()
{
Field field = getField();
List<Location> adjacent = field.adjacentLocations(getLocation());
Iterator<Location> it = adjacent.iterator();
while(it.hasNext()) {
Location where = it.next();
Object animal = field.getObjectAt(where);
if(animal instanceof Rabbit) {
Rabbit rabbit = (Rabbit) animal;
if(rabbit.isAlive()) {
rabbit.setDead();
foodLevel = RABBIT_FOOD_VALUE;
return where;
}
}
else if(animal instanceof Mouse) {
Mouse mouse = (Mouse) animal;
if(mouse.isAlive()) {
mouse.setDead();
foodLevel = MOUSE_FOOD_VALUE;
return where;
}
}
else if(animal instanceof Fox) {
Fox fox = (Fox) animal;
if(fox.isAlive()) {
fox.setDead();
foodLevel = FOX_FOOD_VALUE;
return where;
}
}
else if(animal instanceof Giraffe) {
Giraffe giraffe = (Giraffe) animal;
if(giraffe.isAlive()) {
giraffe.setDead();
foodLevel = GIRAFFE_FOOD_VALUE;
return where;
}
}
}
return null;
}
/**
* Check whether or not this Tiger is to give birth at this step.
* Tigers give birth when a male and female tiger meet and mate
* New births will be made into free adjacent locations.
* @param newTigers A list to return newly born Tigers.
*/
private void giveBirth(List<Entity> newTigers)
{
// New Tigers are born into adjacent locations.
// Get a list of adjacent free locations.
Field field = getField();
List<Location> free = field.getFreeAdjacentLocations(getLocation(), 2);
int births = breed();
for(int b = 0; b < births && free.size() > 0; b++) {
Location loc = free.remove(0);
boolean gender = rand.nextBoolean();
Tiger young = new Tiger(false,gender,field, loc);
newTigers.add(young);
}
}
/**
* This method checks if there is any male mouse nearby so
* @return boolean there is a male nearby
*/
private boolean canFindMaleTiger(int distance){
Field field = getField();
List<Location> adjacent = field.adjacentLocations(getLocation(),distance);
Iterator<Location> it = adjacent.iterator();
while(it.hasNext()) {
Location where = it.next();
Object animal = field.getObjectAt(where);
if(animal instanceof Tiger) {
Tiger tiger = (Tiger) animal;
if(!tiger.isFemale()) {
return true;
}
}
}
return false;
}
/**
* Generate a number representing the number of births,
* if it can breed.
* @return The number of births (may be zero).
*/
private int breed()
{
int births = 0;
if(canBreed() && rand.nextDouble() <= BREEDING_PROBABILITY) {
births = rand.nextInt(MAX_LITTER_SIZE) + 1;
}
if (canFindMaleTiger(2)){
return births;
}
else{
return 0;
}
}
/**
* A Tiger can breed if it has reached the breeding age.
*/
private boolean canBreed()
{
return age >= BREEDING_AGE;
}
}