-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animal.java
105 lines (96 loc) · 2.42 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
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
import java.util.List;
/**
* A class representing shared characteristics of animals.
*
* @author Daniel Koch and Jakub Grzelak
* @version 2020.02.23 (2)
*/
public abstract class Animal
{
// Whether the animal is alive or not.
private boolean alive;
// The animal's field.
private Field field;
// The animal's position in the field.
private Location location;
// Is the animal infected.
protected boolean isInfected;
/**
* Create a new animal at location in field.
*
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Animal(Field field, Location location)
{
alive = true;
this.field = field;
setLocation(location);
isInfected = false;
}
/**
* Make this animal act - that is: make it do
* whatever it wants/needs to do.
* @param newAnimals A list to receive newly born animals.
* @param time time - day or night.
* @param weather Current weather condition.
*/
abstract public void act(List<Animal> newAnimals, boolean time, String weather);
/**
* Check whether the animal is alive or not.
* @return true if the animal is still alive.
*/
protected boolean isAlive()
{
return alive;
}
/**
* Indicate that the animal is no longer alive.
* It is removed from the field.
*/
protected void setDead()
{
alive = false;
if(location != null) {
field.clear(location);
location = null;
field = null;
}
}
/**
* Return the animal's location.
* @return The animal's location.
*/
protected Location getLocation()
{
return location;
}
/**
* Place the animal at the new location in the given field.
* @param newLocation The animal's new location.
*/
protected void setLocation(Location newLocation)
{
if(location != null) {
field.clear(location);
}
location = newLocation;
field.place(this, newLocation);
}
/**
* Return the animal's field.
* @return The animal's field.
*/
protected Field getField()
{
return field;
}
/**
* Return the animal's infection state.
* @return If the animal is infected.
*/
public boolean getInfection()
{
return isInfected;
}
}