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.java23
1 files changed, 10 insertions, 13 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java
index 119781c..23eec4e 100644
--- a/src/ch/epfl/maze/physical/zoo/Monkey.java
+++ b/src/ch/epfl/maze/physical/zoo/Monkey.java
@@ -4,9 +4,7 @@ import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.util.Direction; 4import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
6 6
7import java.util.ArrayList; 7import java.util.Set;
8import java.util.Arrays;
9import java.util.List;
10 8
11/** 9/**
12 * Monkey A.I. that puts its hand on the left wall and follows it. 10 * Monkey A.I. that puts its hand on the left wall and follows it.
@@ -31,13 +29,12 @@ public class Monkey extends Animal {
31 /** 29 /**
32 * Finds a currentDirection following the "left paw rule". 30 * Finds a currentDirection following the "left paw rule".
33 * 31 *
34 * @param choices An array of possible directions 32 * @param choices A set of possible directions
35 * @return The currentDirection to take according to the "left paw rule" 33 * @return The currentDirection to take according to the "left paw rule"
36 */ 34 */
37 private Direction followLeft(Direction[] choices) { 35 private Direction followLeft(Set<Direction> choices) {
38 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
39 Direction dir = this.currentDirection.rotateLeft(); 36 Direction dir = this.currentDirection.rotateLeft();
40 while (!choiceList.contains(dir)) dir = dir.rotateRight(); 37 while (!choices.contains(dir)) dir = dir.rotateRight();
41 38
42 return dir; 39 return dir;
43 } 40 }
@@ -45,13 +42,13 @@ public class Monkey extends Animal {
45 /** 42 /**
46 * Determines the strategy to follow depending on special cases. 43 * Determines the strategy to follow depending on special cases.
47 * 44 *
48 * @param choices An array of possible directions 45 * @param choices A set of possible directions
49 * @return The Direction to take 46 * @return The Direction to take
50 */ 47 */
51 private Direction findDirection(Direction[] choices) { 48 private Direction findDirection(Set<Direction> choices) {
52 if (choices.length == 0) return Direction.NONE; 49 if (choices.isEmpty()) return Direction.NONE;
53 if (this.currentDirection == Direction.NONE) return choices[0]; 50 if (this.currentDirection == Direction.NONE) return choices.stream().findAny().get();
54 if (choices.length == 1) return choices[0]; 51 if (choices.size() == 1) return choices.stream().findAny().get();
55 return this.followLeft(choices); 52 return this.followLeft(choices);
56 } 53 }
57 54
@@ -59,7 +56,7 @@ public class Monkey extends Animal {
59 * Moves according to the relative left wall that the monkey has to follow. 56 * Moves according to the relative left wall that the monkey has to follow.
60 */ 57 */
61 @Override 58 @Override
62 public Direction move(Direction[] choices) { 59 public Direction move(Set<Direction> choices) {
63 this.currentDirection = this.findDirection(choices); 60 this.currentDirection = this.findDirection(choices);
64 return this.currentDirection; 61 return this.currentDirection;
65 } 62 }