-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWerewolf.java
More file actions
executable file
·82 lines (72 loc) · 2.79 KB
/
Werewolf.java
File metadata and controls
executable file
·82 lines (72 loc) · 2.79 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
import java.util.List;
/**
* A simple model of a werewolf.
* Werewolves age, move, and die.
*
* @author Saathveekan Satheeshkumar, Manik Aggarwal, David J. Barnes and Michael Kölling
* @version 2022.03.02 (2)
*/
public class Werewolf extends Animal {
// The name of the animal.
private static String ANIMAL_NAME;
// The food values of the werewolf's prey.
private final int JACKALOPE_FOOD_VALUE = data.getFoodValue("Jackalope");
private final int UNICORN_FOOD_VALUE = data.getFoodValue("Unicorn");
/**
* Create a werewolf. A werewolf can be created as a newborn (age zero
* and not hungry) or with a random age and food level.
*
* @param randomAge If true, the werewolf will have random age and hunger level.
* @param field The field currently occupied.
* @param location The location within the field.*
* @param animalName The name of the animal
*/
public Werewolf(boolean randomAge, Field field, Location location, String animalName) {
super(field, location, animalName);
ANIMAL_NAME = animalName;
setAge(randomAge);
}
/**
* Look for werewolves adjacent to the current location.
* Only the first live prey is eaten.
*
* @return Where food was found, or null if it wasn't.
*/
protected Location findFood() {
if (belowMaxHealth()) {
Field field = getField();
List<Location> adjacent = field.adjacentLocations(getLocation());
for (Location where : adjacent) {
Object animal = field.getObjectAt(where);
if (animal instanceof Jackalope jackalope && jackalope.isAlive()) {
jackalope.setDead();
health += JACKALOPE_FOOD_VALUE;
return where;
} else if (animal instanceof Unicorn unicorn && unicorn.isAlive()) {
unicorn.setDead();
health += UNICORN_FOOD_VALUE;
return where;
}
}
}
return null;
}
/**
* Check whether this werewolf is to give birth at this step.
* New births will be made into free adjacent locations.
*
* @param newAnimals A list to return newly born werewolves.
*/
protected void giveBirth(List<Animal> newAnimals) {
// New werewolves are born into adjacent locations.
// Get a list of adjacent free locations.
Field field = getField();
List<Location> free = field.getFreeAdjacentLocations(getLocation());
int births = breed();
for (int b = 0; b < births && !free.isEmpty(); b++) {
Location loc = free.remove(0);
Werewolf young = new Werewolf(false, field, loc, ANIMAL_NAME);
newAnimals.add(young);
}
}
}