-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
65 lines (53 loc) · 1.94 KB
/
Animal.java
File metadata and controls
65 lines (53 loc) · 1.94 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
public abstract class Animal extends Lifeform{
protected static final int maxEnergy = 100;
private int energy;
public Animal(int deathAge, int energy){
super(deathAge, 30);
this.energy = energy;
}
@Override
public boolean isDead(){
return getAge() == getDeathAge() || getEnergy() == 0;
}
public void setEnergy(int energy){
this.energy = energy;
}
public int getEnergy(){
return energy;
}
public void decreEnergy(){
/* Decreases energy of this Animal */
setEnergy(getEnergy() - 1);
}
public boolean canReproduce(){
return getEnergy() > maxEnergy / 2 && (getAge() > getDeathAge()/4 && getAge() < getDeathAge()*3/4);
}
public void eat(Lifeform food){
/* Adds the food's nutrients to this Animal's energy up to a maximum of maxEnergy. */
if(getEnergy() + food.getNutrient() > maxEnergy){
setEnergy(maxEnergy);
}
else{
setEnergy(getEnergy() + food.getNutrient());
}
}
public boolean isFull(){
/* Returns true if this Animal's energy exceeds a certain energy amount. */
return getEnergy() >= maxEnergy * 8/10;
}
public int[] move(Lifeform[][] board, int i, int j){
/* Returns a int array containing the position of the food where
index 0 represents the row and index 1 represents the column.
*/
int[] newLoc;
newLoc = foodLoc(board, i, j);
return newLoc;
}
public abstract boolean isFood(Lifeform[][] board, int i, int j);
/* Returns true if there is food and false otherwise */
public abstract int[] foodLoc(Lifeform[][] board, int i, int j);
/* Precondition: isFood returns true.
Finds the location of the food.
*/
public abstract void reproduce(Lifeform[][] newBoard, int i, int j);
}