diff options
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/maze/physical/Animal.java | 24 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/GhostPredator.java | 2 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 16 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Panda.java | 2 |
4 files changed, 27 insertions, 17 deletions
diff --git a/src/ch/epfl/maze/physical/Animal.java b/src/ch/epfl/maze/physical/Animal.java index 3697c32..34a1136 100644 --- a/src/ch/epfl/maze/physical/Animal.java +++ b/src/ch/epfl/maze/physical/Animal.java | |||
@@ -13,6 +13,18 @@ import ch.epfl.maze.util.Vector2D; | |||
13 | abstract public class Animal { | 13 | abstract public class Animal { |
14 | 14 | ||
15 | private Vector2D position; | 15 | private Vector2D position; |
16 | private Direction direction; | ||
17 | |||
18 | /** | ||
19 | * Constructs an animal with a specified position and direction. | ||
20 | * | ||
21 | * @param position Position of the animal in the labyrinth | ||
22 | * @param direction Direction the animal is facing | ||
23 | */ | ||
24 | public Animal(Vector2D position, Direction direction) { | ||
25 | this.position = position; | ||
26 | this.direction = direction; | ||
27 | } | ||
16 | 28 | ||
17 | /** | 29 | /** |
18 | * Constructs an animal with a specified position. | 30 | * Constructs an animal with a specified position. |
@@ -20,7 +32,7 @@ abstract public class Animal { | |||
20 | * @param position Position of the animal in the labyrinth | 32 | * @param position Position of the animal in the labyrinth |
21 | */ | 33 | */ |
22 | public Animal(Vector2D position) { | 34 | public Animal(Vector2D position) { |
23 | this.position = position; | 35 | this(position, Direction.NONE); |
24 | } | 36 | } |
25 | 37 | ||
26 | /** | 38 | /** |
@@ -44,6 +56,7 @@ abstract public class Animal { | |||
44 | */ | 56 | */ |
45 | public final void update(Direction dir) { | 57 | public final void update(Direction dir) { |
46 | this.position = this.position.addDirectionTo(dir); | 58 | this.position = this.position.addDirectionTo(dir); |
59 | this.direction = dir; | ||
47 | } | 60 | } |
48 | 61 | ||
49 | /** | 62 | /** |
@@ -67,6 +80,15 @@ abstract public class Animal { | |||
67 | return this.position; | 80 | return this.position; |
68 | } | 81 | } |
69 | 82 | ||
83 | /** | ||
84 | * Returns the direction of the Animal. | ||
85 | * | ||
86 | * @return The current direction of the Animal | ||
87 | */ | ||
88 | public final Direction getDirection() { | ||
89 | return this.direction; | ||
90 | } | ||
91 | |||
70 | abstract public Animal copy(); | 92 | abstract public Animal copy(); |
71 | 93 | ||
72 | } | 94 | } |
diff --git a/src/ch/epfl/maze/physical/GhostPredator.java b/src/ch/epfl/maze/physical/GhostPredator.java index 5408bf6..e4a64b3 100644 --- a/src/ch/epfl/maze/physical/GhostPredator.java +++ b/src/ch/epfl/maze/physical/GhostPredator.java | |||
@@ -116,7 +116,7 @@ abstract public class GhostPredator extends Predator { | |||
116 | Prey prey = GhostPredator.getPrey(daedalus); | 116 | Prey prey = GhostPredator.getPrey(daedalus); |
117 | 117 | ||
118 | if (prey == null) return Direction.NONE; | 118 | if (prey == null) return Direction.NONE; |
119 | return prey.getCurrentDirection(); | 119 | return prey.getDirection(); |
120 | } | 120 | } |
121 | 121 | ||
122 | /** | 122 | /** |
diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java index ed26d23..650cecf 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java | |||
@@ -17,8 +17,6 @@ abstract public class ProbabilisticAnimal extends Animal { | |||
17 | 17 | ||
18 | public static final Random RANDOM_SOURCE = new Random(); | 18 | public static final Random RANDOM_SOURCE = new Random(); |
19 | 19 | ||
20 | private Direction currentDirection; | ||
21 | |||
22 | /** | 20 | /** |
23 | * Constructs a probabilistic animal with a starting position | 21 | * Constructs a probabilistic animal with a starting position |
24 | * | 22 | * |
@@ -26,15 +24,6 @@ abstract public class ProbabilisticAnimal extends Animal { | |||
26 | */ | 24 | */ |
27 | public ProbabilisticAnimal(Vector2D position) { | 25 | public ProbabilisticAnimal(Vector2D position) { |
28 | super(position); // no pun intended | 26 | super(position); // no pun intended |
29 | this.currentDirection = Direction.NONE; | ||
30 | } | ||
31 | |||
32 | protected Direction getCurrentDirection() { | ||
33 | return this.currentDirection; | ||
34 | } | ||
35 | |||
36 | protected void setCurrentDirection(Direction direction) { | ||
37 | this.currentDirection = direction; | ||
38 | } | 27 | } |
39 | 28 | ||
40 | /** | 29 | /** |
@@ -59,7 +48,7 @@ abstract public class ProbabilisticAnimal extends Animal { | |||
59 | * @return An array of smart choices | 48 | * @return An array of smart choices |
60 | */ | 49 | */ |
61 | protected Direction[] excludeOrigin(Direction[] choices) { | 50 | protected Direction[] excludeOrigin(Direction[] choices) { |
62 | return this.excludeDirection(choices, this.currentDirection.reverse()); | 51 | return this.excludeDirection(choices, this.getDirection().reverse()); |
63 | } | 52 | } |
64 | 53 | ||
65 | /** | 54 | /** |
@@ -81,8 +70,7 @@ abstract public class ProbabilisticAnimal extends Animal { | |||
81 | if (choices.length == 0) return Direction.NONE; | 70 | if (choices.length == 0) return Direction.NONE; |
82 | 71 | ||
83 | Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; | 72 | Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; |
84 | this.currentDirection = this.getRandomDirection(smartChoices); | 73 | return this.getRandomDirection(smartChoices); |
85 | return this.currentDirection; | ||
86 | } | 74 | } |
87 | 75 | ||
88 | } | 76 | } |
diff --git a/src/ch/epfl/maze/physical/zoo/Panda.java b/src/ch/epfl/maze/physical/zoo/Panda.java index b794d10..ce38fda 100644 --- a/src/ch/epfl/maze/physical/zoo/Panda.java +++ b/src/ch/epfl/maze/physical/zoo/Panda.java | |||
@@ -88,7 +88,7 @@ public class Panda extends ProbabilisticAnimal { | |||
88 | private Direction[] selectBestDirections(Direction[] choices) { | 88 | private Direction[] selectBestDirections(Direction[] choices) { |
89 | // special case | 89 | // special case |
90 | if (this.isIntersection(choices) && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) | 90 | if (this.isIntersection(choices) && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) |
91 | return new Direction[]{this.getCurrentDirection().reverse()}; | 91 | return new Direction[]{this.getDirection().reverse()}; |
92 | 92 | ||
93 | // general case | 93 | // general case |
94 | for (Trail.Marking mark : Trail.Marking.ALL) { | 94 | for (Trail.Marking mark : Trail.Marking.ALL) { |