summaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java29
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java25
2 files changed, 52 insertions, 2 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java
new file mode 100644
index 0000000..b7a44f8
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java
@@ -0,0 +1,29 @@
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 that continues forward if possible.
9 *
10 * @author Pacien TRAN-GIRARD
11 */
12public interface ForwardPicker extends BlindPicker {
13
14 /**
15 * Returns the current Direction the decision maker is walking toward.
16 *
17 * @return The current Direction
18 */
19 Direction getDirection();
20
21 @Override
22 default Direction pick(Set<Direction> choices) {
23 if (choices.contains(this.getDirection()))
24 return this.getDirection();
25 else
26 return FALLBACK_DIRECTION;
27 }
28
29}
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java
index 3c84f59..5b6998d 100644
--- a/src/ch/epfl/maze/physical/zoo/Monkey.java
+++ b/src/ch/epfl/maze/physical/zoo/Monkey.java
@@ -2,6 +2,7 @@ package 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; 4import ch.epfl.maze.physical.stragegies.picker.CyclePicker;
5import ch.epfl.maze.physical.stragegies.picker.ForwardPicker;
5import ch.epfl.maze.physical.stragegies.picker.SimplePicker; 6import ch.epfl.maze.physical.stragegies.picker.SimplePicker;
6import ch.epfl.maze.util.Direction; 7import ch.epfl.maze.util.Direction;
7import ch.epfl.maze.util.Vector2D; 8import ch.epfl.maze.util.Vector2D;
@@ -14,10 +15,12 @@ import java.util.Set;
14 * @author EPFL 15 * @author EPFL
15 * @author Pacien TRAN-GIRARD 16 * @author Pacien TRAN-GIRARD
16 */ 17 */
17public class Monkey extends Animal implements SimplePicker, CyclePicker { 18public class Monkey extends Animal implements SimplePicker, ForwardPicker, CyclePicker {
18 19
19 public static final Direction SMART_PAW = Direction.LEFT; 20 public static final Direction SMART_PAW = Direction.LEFT;
20 21
22 private boolean foundWall;
23
21 /** 24 /**
22 * Constructs a monkey with a starting position. 25 * Constructs a monkey with a starting position.
23 * 26 *
@@ -25,6 +28,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker {
25 */ 28 */
26 public Monkey(Vector2D position) { 29 public Monkey(Vector2D position) {
27 super(position); 30 super(position);
31
32 this.foundWall = false;
28 } 33 }
29 34
30 @Override 35 @Override
@@ -38,7 +43,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker {
38 } 43 }
39 44
40 /** 45 /**
41 * Picks a Direction following the "paw rule", or any available if the Monkey has no current Direction. 46 * Picks a Direction preferably following the "paw rule", or forward if no wall has been encountered,
47 * or any available Direction if the Monkey has no current Direction.
42 * 48 *
43 * @param choices A set of Direction choices 49 * @param choices A set of Direction choices
44 * @return The picked Direction 50 * @return The picked Direction
@@ -47,6 +53,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker {
47 public Direction pick(Set<Direction> choices) { 53 public Direction pick(Set<Direction> choices) {
48 if (this.getDirection() == Direction.NONE) 54 if (this.getDirection() == Direction.NONE)
49 return SimplePicker.super.pick(choices); 55 return SimplePicker.super.pick(choices);
56 else if (!this.foundWall)
57 return ForwardPicker.super.pick(choices);
50 else 58 else
51 return CyclePicker.super.pick(choices); 59 return CyclePicker.super.pick(choices);
52 } 60 }
@@ -56,6 +64,9 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker {
56 */ 64 */
57 @Override 65 @Override
58 public Direction move(Set<Direction> choices) { 66 public Direction move(Set<Direction> choices) {
67 if (this.nextToWall(choices))
68 this.foundWall = true;
69
59 return this.pick(choices); 70 return this.pick(choices);
60 } 71 }
61 72
@@ -64,4 +75,14 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker {
64 return new Monkey(this.getPosition()); 75 return new Monkey(this.getPosition());
65 } 76 }
66 77
78 /**
79 * Checks is the Monkey is next to a wall.
80 *
81 * @param choices The available Direction choices
82 * @return T(The Direction choices set indicates the presence of a wall)
83 */
84 private boolean nextToWall(Set<Direction> choices) {
85 return choices.size() < 4;
86 }
87
67} 88}