summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo/Monkey.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo/Monkey.java')
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java
index 8aea75d..a29b7d4 100644
--- a/src/ch/epfl/maze/physical/zoo/Monkey.java
+++ b/src/ch/epfl/maze/physical/zoo/Monkey.java
@@ -37,9 +37,6 @@ public class Monkey extends Animal {
37 */ 37 */
38 38
39 private Direction followLeft(Direction[] choices) { 39 private Direction followLeft(Direction[] choices) {
40 if (choices.length == 0) return Direction.NONE;
41 if (choices.length == 1) return choices[0];
42
43 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices)); 40 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
44 Direction dir = this.currentDirection.rotateLeft(); 41 Direction dir = this.currentDirection.rotateLeft();
45 while (!choiceList.contains(dir)) dir = dir.rotateRight(); 42 while (!choiceList.contains(dir)) dir = dir.rotateRight();
@@ -48,12 +45,26 @@ public class Monkey extends Animal {
48 } 45 }
49 46
50 /** 47 /**
48 * Determines the strategy to follow depending on special cases.
49 *
50 * @param choices An array of possible directions
51 * @return The Direction to take
52 */
53 private Direction findDirection(Direction[] choices) {
54 if (choices.length == 0) return Direction.NONE;
55 if (this.currentDirection == Direction.NONE) return choices[0];
56 if (choices.length == 1) return choices[0];
57 if (choices.length > 3) return choices[0];
58 return this.followLeft(choices);
59 }
60
61 /**
51 * Moves according to the relative left wall that the monkey has to follow. 62 * Moves according to the relative left wall that the monkey has to follow.
52 */ 63 */
53 64
54 @Override 65 @Override
55 public Direction move(Direction[] choices) { 66 public Direction move(Direction[] choices) {
56 this.currentDirection = this.followLeft(choices); 67 this.currentDirection = this.findDirection(choices);
57 return this.currentDirection; 68 return this.currentDirection;
58 } 69 }
59 70