-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredator.java
More file actions
125 lines (115 loc) · 4.1 KB
/
Predator.java
File metadata and controls
125 lines (115 loc) · 4.1 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
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.LinkedList;
import javafx.scene.paint.Color;
/**
* A simple model of a predator.
* Predators age, move, eat preys, and die.
*
* @author Yingtao Zheng(Kevin)
* @time 2025.02.21
* @version 1.0
*/
public class Predator extends Animal {
private static final int MAX_FOOD_LEVEL = 40;
private static final int FLAMINGO_FOOD_VALUE = 5;
private static final int SHEEP_FOOD_VALUE = 7;
private static final Random rand = Randomizer.getRandom();
private int age;
private double foodLevel;
// record animals have already bred in this round
private static List<Animal> alreadyBred = new ArrayList<>();
/**
* Create a predator. A predator can be created as a new born (age zero
* and not hungry) or with a random age and food level.
*
* @param isFirstGeneration If true, the predator will have random age and hunger level.
* @param field The field currently occupied.
* @param location The location within the field.
* @param col The color of the predator.
* @param type The type of the predator.
* @param isFirstGeneration If true, the predator is the first generation.
*/
public Predator(Field field, Location location, Color col, Type type, boolean isFirstGeneration) {
super(field, location, col, type, isFirstGeneration);
if(isFirstGeneration) {
setAge(rand.nextInt(getMaxAge()));
foodLevel = rand.nextInt(MAX_FOOD_LEVEL);
}
else {
setAge(0);
foodLevel = MAX_FOOD_LEVEL;
}
}
/**
* This is what the predator does most of the time: it hunts for
* preys. In the process, it might breed, die of hunger,
* or die of old age.
* @param newPredators A list to return newly born predators.
*/
public void act(List<Animal> newPredators) {
super.incrementAge();
metabolism();
checkSick();
if(isAlive()) {
Breeding.giveBirth(this, newPredators);
// Move towards a source of food if found.
Location newLocation = findFood();
if(newLocation == null) {
// No food found - try to move to a free location.
newLocation = getField().getFreeAdjacentLocation(getLocation());
}
// See if it was possible to move.
if(newLocation != null) {
setLocation(newLocation);
}
else {
// Overcrowding.
setDead(DeathReason.OUT_OF_BOUNDS);
}
}
}
/**
* Natural metabolism. This could result in the predator's death.
*/
private void metabolism() {
foodLevel -= getMetabolism();
if(foodLevel <= 0) {
setDead(DeathReason.HUNGER);
}
}
/**
* Look for preys adjacent to the current location.
* Only the first live prey is eaten.
* @return Where food was found, or null if it wasn't.
*/
private 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 Prey) {
Prey prey = (Prey) animal;
if(prey.isAlive()) {
prey.setDead(DeathReason.EATEN);
if (prey.getType() == Animal.Type.SHEEP){
foodLevel += SHEEP_FOOD_VALUE;
}
else if (prey.getType() == Animal.Type.FLAMINGO){
foodLevel += FLAMINGO_FOOD_VALUE;
}
if (foodLevel > MAX_FOOD_LEVEL) foodLevel = MAX_FOOD_LEVEL;
return where;
}
}
}
return null;
}
public double getFoodLevel() {
return this.foodLevel;
}
}