summaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-23 21:22:35 +0100
committerPacien TRAN-GIRARD2015-11-23 21:22:35 +0100
commit293b24a88f941934d1ab832f1cc80c74183f0327 (patch)
treed82704f9598c1653c8c3dbcc28f90b6b58ef8fc1 /src/ch
parent7e82da671273101cb3063201a3e26be912134669 (diff)
downloadmaze-solver-293b24a88f941934d1ab832f1cc80c74183f0327.tar.gz
Fix Monkey undefined behaviour in special cases
Diffstat (limited to 'src/ch')
-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