diff options
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo')
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Monkey.java | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java index 23eec4e..3c84f59 100644 --- a/src/ch/epfl/maze/physical/zoo/Monkey.java +++ b/src/ch/epfl/maze/physical/zoo/Monkey.java | |||
@@ -1,6 +1,8 @@ | |||
1 | package ch.epfl.maze.physical.zoo; | 1 | package ch.epfl.maze.physical.zoo; |
2 | 2 | ||
3 | import ch.epfl.maze.physical.Animal; | 3 | import ch.epfl.maze.physical.Animal; |
4 | import ch.epfl.maze.physical.stragegies.picker.CyclePicker; | ||
5 | import ch.epfl.maze.physical.stragegies.picker.SimplePicker; | ||
4 | import ch.epfl.maze.util.Direction; | 6 | import ch.epfl.maze.util.Direction; |
5 | import ch.epfl.maze.util.Vector2D; | 7 | import ch.epfl.maze.util.Vector2D; |
6 | 8 | ||
@@ -12,9 +14,9 @@ import java.util.Set; | |||
12 | * @author EPFL | 14 | * @author EPFL |
13 | * @author Pacien TRAN-GIRARD | 15 | * @author Pacien TRAN-GIRARD |
14 | */ | 16 | */ |
15 | public class Monkey extends Animal { | 17 | public class Monkey extends Animal implements SimplePicker, CyclePicker { |
16 | 18 | ||
17 | private Direction currentDirection; | 19 | public static final Direction SMART_PAW = Direction.LEFT; |
18 | 20 | ||
19 | /** | 21 | /** |
20 | * Constructs a monkey with a starting position. | 22 | * Constructs a monkey with a starting position. |
@@ -23,33 +25,30 @@ public class Monkey extends Animal { | |||
23 | */ | 25 | */ |
24 | public Monkey(Vector2D position) { | 26 | public Monkey(Vector2D position) { |
25 | super(position); | 27 | super(position); |
26 | this.currentDirection = Direction.NONE; | ||
27 | } | 28 | } |
28 | 29 | ||
29 | /** | 30 | @Override |
30 | * Finds a currentDirection following the "left paw rule". | 31 | public Direction getStartingDirection() { |
31 | * | 32 | return this.getDirection().unRelativeDirection(Monkey.SMART_PAW); |
32 | * @param choices A set of possible directions | 33 | } |
33 | * @return The currentDirection to take according to the "left paw rule" | ||
34 | */ | ||
35 | private Direction followLeft(Set<Direction> choices) { | ||
36 | Direction dir = this.currentDirection.rotateLeft(); | ||
37 | while (!choices.contains(dir)) dir = dir.rotateRight(); | ||
38 | 34 | ||
39 | return dir; | 35 | @Override |
36 | public Direction getRotationDirection() { | ||
37 | return Monkey.SMART_PAW.reverse(); | ||
40 | } | 38 | } |
41 | 39 | ||
42 | /** | 40 | /** |
43 | * Determines the strategy to follow depending on special cases. | 41 | * Picks a Direction following the "paw rule", or any available if the Monkey has no current Direction. |
44 | * | 42 | * |
45 | * @param choices A set of possible directions | 43 | * @param choices A set of Direction choices |
46 | * @return The Direction to take | 44 | * @return The picked Direction |
47 | */ | 45 | */ |
48 | private Direction findDirection(Set<Direction> choices) { | 46 | @Override |
49 | if (choices.isEmpty()) return Direction.NONE; | 47 | public Direction pick(Set<Direction> choices) { |
50 | if (this.currentDirection == Direction.NONE) return choices.stream().findAny().get(); | 48 | if (this.getDirection() == Direction.NONE) |
51 | if (choices.size() == 1) return choices.stream().findAny().get(); | 49 | return SimplePicker.super.pick(choices); |
52 | return this.followLeft(choices); | 50 | else |
51 | return CyclePicker.super.pick(choices); | ||
53 | } | 52 | } |
54 | 53 | ||
55 | /** | 54 | /** |
@@ -57,8 +56,7 @@ public class Monkey extends Animal { | |||
57 | */ | 56 | */ |
58 | @Override | 57 | @Override |
59 | public Direction move(Set<Direction> choices) { | 58 | public Direction move(Set<Direction> choices) { |
60 | this.currentDirection = this.findDirection(choices); | 59 | return this.pick(choices); |
61 | return this.currentDirection; | ||
62 | } | 60 | } |
63 | 61 | ||
64 | @Override | 62 | @Override |