-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimal.java
More file actions
94 lines (86 loc) · 2.82 KB
/
Animal.java
File metadata and controls
94 lines (86 loc) · 2.82 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
import java.util.Random;
/**
* Represents an animal.
*
* @author David Matuszek
* @version October 12, 2001
*/
public class Animal {
// instance variables -- every animal has a location
private Model model;
/** The row of the field containing this animal (do not modify!) */
int row;
/** The column of the field containing this animal (do not modify!) */
int column;
static Random randomNumberGenerator = new Random();
/**
* Constructor for objects of class Animal.
*
* @param model the model (needed to use Model.look and Model.move methods
* @param row the row of the field in which to place this animal
* @param column the column of the field in which to place this animal
*/
public Animal(Model model, int row, int column) {
this.model = model;
this.row = row;
this.column = column;
}
/**
* Utility method to choose a random integer from min
* to max, inclusive.
*
* @param min the minimum value to be returned
* @param max the maximum value to be returned
* @return a random integer N, where min <= N <= max
*/
static int random(int min, int max) {
return Model.random(min, max);
}
/**
* Finds the first visible thing in the given direction, starting
* from this animal's current position.
*
* @param direction the direction in which to look
* @return the object seen
*/
int look(int direction) {
return model.look(row, column, direction);
}
/**
* Finds the distance to the first visible thing in the given
* direction, starting from this animal's current position.
*
* @param direction the direction in which to look
* @return the distance to the object seen
*/
int distance(int direction) {
return model.distance(row, column, direction);
}
/**
* Determines whether this animal can move one step in the
* given direction. An animal cannot move off the edge of
* the array or onto a bush, but it <i>can</i> move onto
* another animal.<p>
* If we extend this program to allow multiple rabbits or
* multiple foxes, we should disallow an animal moving onto
* another animal of the same type.
*
* @param direction the direction in which to consider moving
* @return true if the move is possible
*/
boolean canMove(int direction) {
if (direction == Model.STAY) return true;
if (distance(direction) > 1) return true;
int object = look(direction);
if (object == Model.EDGE || object == Model.BUSH) return false;
return true;
}
/**
* Decides what direction to move next.
*
* @return the desired direction
*/
int decideMove() {
return Model.STAY;
}
}