summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java41
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java21
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java44
3 files changed, 83 insertions, 23 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
new file mode 100644
index 0000000..e1a969f
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
@@ -0,0 +1,41 @@
1package ch.epfl.maze.physical.stragegies.picker;
2
3import ch.epfl.maze.util.Direction;
4
5import java.util.Set;
6
7/**
8 * A decision maker cycling in each Direction starting from a given one.
9 *
10 * @author Pacien TRAN-GIRARD
11 */
12public interface CyclePicker extends BlindPicker {
13
14 /**
15 * Returns the cycle starting Direction.
16 *
17 * @return The starting Direction
18 */
19 Direction getStartingDirection();
20
21 /**
22 * Returns the Rotation Direction.
23 *
24 * @return The Rotation Direction
25 */
26 Direction getRotationDirection();
27
28 @Override
29 default Direction pick(Set<Direction> choices) {
30 if (choices.isEmpty()) return FALLBACK_DIRECTION;
31 if (this.getStartingDirection() == Direction.NONE) return FALLBACK_DIRECTION;
32 if (this.getRotationDirection() == Direction.NONE) return FALLBACK_DIRECTION;
33
34 Direction dir = this.getStartingDirection();
35 while (!choices.contains(dir))
36 dir = dir.unRelativeDirection(this.getRotationDirection());
37
38 return dir;
39 }
40
41}
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java
new file mode 100644
index 0000000..71f0802
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java
@@ -0,0 +1,21 @@
1package ch.epfl.maze.physical.stragegies.picker;
2
3import ch.epfl.maze.util.Direction;
4
5import java.util.Set;
6
7/**
8 * A simple decision maker with no guarantee of randomness.
9 *
10 * @author Pacien TRAN-GIRARD
11 */
12public interface SimplePicker extends BlindPicker {
13
14 @Override
15 default Direction pick(Set<Direction> choices) {
16 if (choices.isEmpty()) return FALLBACK_DIRECTION;
17
18 return choices.stream().findFirst().get();
19 }
20
21}
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 @@
1package ch.epfl.maze.physical.zoo; 1package ch.epfl.maze.physical.zoo;
2 2
3import ch.epfl.maze.physical.Animal; 3import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.physical.stragegies.picker.CyclePicker;
5import ch.epfl.maze.physical.stragegies.picker.SimplePicker;
4import ch.epfl.maze.util.Direction; 6import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D; 7import 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 */
15public class Monkey extends Animal { 17public 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