-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarnivore.java
More file actions
52 lines (47 loc) · 1.58 KB
/
Carnivore.java
File metadata and controls
52 lines (47 loc) · 1.58 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
public class Carnivore extends Animal{
public Carnivore(){
super(25, 15);
}
@Override
public String toString(){
return "C";
}
@Override
public int[] foodLoc(Lifeform[][] board, int i, int j){
if (i != 0 && board[i-1][j] instanceof Herbivore){
return new int[] {i-1,j};
}
else if (j != 0 && board[i][j-1] instanceof Herbivore){
return new int[] {i,j-1};
}
else if (i != board.length-1 && board[i+1][j] instanceof Herbivore){
return new int[] {i+1,j};
}
else{
return new int[] {i,j+1};
}
}
@Override
public boolean isFood(Lifeform[][] board, int i, int j){
boolean food = false;
if(i != 0 && board[i-1][j] != null && board[i-1][j] instanceof Herbivore){
food = true;
}
else if(j != 0 && board[i][j-1] != null && board[i][j-1] instanceof Herbivore){
food = true;
}
else if(i != board.length-1 && board[i+1][j] != null && board[i+1][j] instanceof Herbivore){
food = true;
}
else if(j != board[i].length-1 && board[i][j+1] != null && board[i][j+1] instanceof Herbivore){
food = true;
}
return food;
}
@Override
public void reproduce(Lifeform[][] newBoard, int i, int j){
/* Halves the energy of the current Animal and creates a new Carnivore and adds to newBoard.*/
setEnergy(getEnergy() / 2);
newBoard[i][j] = new Carnivore();
}
}