-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFox.java
More file actions
84 lines (76 loc) · 2.77 KB
/
Fox.java
File metadata and controls
84 lines (76 loc) · 2.77 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
/**
* Represents a fox.
*
* @author David Matuszek
* @version October 12, 2001
*/
public class Fox extends Animal {
// instance variables
private boolean haveSeenRabbit = false;
private boolean canSeeRabbitNow = false;
private int distanceToRabbit;
private int directionToRabbit;
private int currentDirection;
/**
* Constructs a Fox in the given model, at the given position
* in the field.
*
* @param model the model that controls this fox.
* @param row the row of the field containing this fox.
* @param column the column of the field containing this fox.
*/
public Fox(Model model, int row, int column) {
super(model, row, column);
currentDirection = Model.random(Model.MIN_DIRECTION,
Model.MAX_DIRECTION);
}
/**
* Controls the movement of the fox.
*
* @return the direction in which the fox wishes to move.
*/
int decideMove() {
// look all around for rabbit
canSeeRabbitNow = false;
for (int i = Model.MIN_DIRECTION; i <= Model.MAX_DIRECTION; i++) {
if (look(i) == Model.RABBIT) {
canSeeRabbitNow = haveSeenRabbit = true;
directionToRabbit = i;
distanceToRabbit = distance(i);
}
}
// if rabbit has been seen recently (not necessarily this time),
// move toward its last known position
if (haveSeenRabbit) {
if (distanceToRabbit > 0) {
distanceToRabbit--;
return directionToRabbit;
}
else { // rabbit was here--where did it go?
haveSeenRabbit = false;
currentDirection = Model.random(Model.MIN_DIRECTION,
Model.MAX_DIRECTION);
}
}
// either haven't seen rabbit, or lost track of rabbit
// continue with current direction, maybe dodging bushes
if (canMove(currentDirection))
return currentDirection;
else if (canMove(Model.turn(currentDirection, 1)))
return Model.turn(currentDirection, 1);
else if (canMove(Model.turn(currentDirection, -1)))
return Model.turn(currentDirection, -1);
else {
currentDirection = Model.random(Model.MIN_DIRECTION,
Model.MAX_DIRECTION);
for (int i = 0; i < 8; i++) {
if (canMove(currentDirection))
return currentDirection;
else
currentDirection = Model.turn(currentDirection, 1);
}
}
// stuck! cannot move
return Model.STAY;
}
}