-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimal.java
62 lines (47 loc) · 1.33 KB
/
Animal.java
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
public abstract class Animal
{
private Cell cell;
private int totalSurvivalSteps=0;
public abstract boolean isRabbit();
public abstract void becomeEaten();
public abstract boolean timeToBreed();
public abstract Animal giveBirth();
public abstract void move();
public Cell getCell(){
return this.cell;
}
public int getTotalSurvivalSteps(){
return this.totalSurvivalSteps;
}
public void setCell(Cell c){
this.cell=c;
}
public void survived(){
this.totalSurvivalSteps++;
}
public void randomMove(){
Cell randomNeighborCell = getCell().getRandomNeighbor();
if (randomNeighborCell!=null){
Cell previousCell = getCell();
if(randomNeighborCell.isEmpty()){
getCell().getAnimal().setCell(randomNeighborCell);
randomNeighborCell.setAnimal(previousCell.getAnimal());
randomNeighborCell.getAnimal().survived();
previousCell.setAnimal(null);
}
}
}
public Animal breed(){
Animal newAnimal;
Cell randomNeighborCell=getCell().getRandomNeighbor();
if (randomNeighborCell!=null && randomNeighborCell.isEmpty()) {
newAnimal = getCell().getAnimal().giveBirth();
randomNeighborCell.setAnimal(newAnimal);
randomNeighborCell.getAnimal().setCell(randomNeighborCell);
return newAnimal;
}
return null;
}
}
//ONOMA: MARINA PAPAGEORGIOU
//AM: 4757