-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
126 lines (122 loc) · 2.63 KB
/
Animal.java
File metadata and controls
126 lines (122 loc) · 2.63 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Random;
import java.util.ArrayList;
public class Animal extends Entity {
protected int energy, maxEnergy;
protected boolean moving;
protected Random rand;
/**
* Constructor for Animal Class
* @param x X coordinate for Animal object
* @param y Y Coordinate for Animal object
*/
public Animal(int x, int y) {
this.x = x;
this.y = y;
maxAge = 15;
maxEnergy = 10;
age = 0;
energy = 5;
moving = false;
//for random movement
rand = new Random();
}
public void eat(Entity food) {
}
/**
* Retrieve energy of Animal
* @return energy variable
*/
public int getEnergy() {
return energy;
}
/**
* Set energy of Animal
* @param energy amount of energy added
*/
public void setEnergy(int energy) {
this.energy = energy;
}
/**
* Check if Animal is hungry
* @return whether energy is not at capacity
*/
public boolean isHungry() {
return (energy < maxEnergy);
}
/**
* Set x coordinate of Animal
* @param x X coordinate
*/
public void setX(int x) {
this.x = x;
}
/**
* Set y coordinate of Animal
* @param y Y coordinate
*/
public void setY(int y) {
this.y = y;
}
/**
* Move Animal
* @param grid grid that holds all entities
*/
public void move(Entity[][] grid) {
moveRandomly(grid);
}
/**
* Set animal to "finished moving"
*/
public void startMoving() {
moving = true;
}
/**
* Check if Animal has moved already
* @param x X coordinate for Animal object
* @return whether animal moved or not already
*/
public boolean isMoving() {
return moving;
}
/**
* Reset animal to "has not moved"
*/
public void stopMoving() {
moving = false;
}
/**
* Moves animal randomly if there are no priority moves like eating food
* @param grid grid that holds all Entities
*/
public void moveRandomly(Entity[][] grid) {
//moves:
//0 1 2
//3 4 5
//6 7 8
//move[4] is the current location and will be false
//since the location is taken by this animal
int i = 0;
ArrayList<Integer> moves = new ArrayList<Integer>();
//append all possible moves to arraylist
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
try {
if (grid[x+dx][y+dy] == null) {
moves.add(i);
}
} catch(ArrayIndexOutOfBoundsException e) {
//off the grid
}
i++;
}
}
//choose random move and follow it
if (moves.size() != 0) {
int chosenMove = moves.get(rand.nextInt(moves.size()));
grid[x][y] = null;
grid[x+(chosenMove/3)-1][y+(chosenMove%3)-1] = this;
x = x+(chosenMove/3)-1;
y = y+(chosenMove%3)-1;
}
}
}