From e2354d82e09c3bf8ae472d174332670d2d12f9bb Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 14:28:26 +0100 Subject: Use Set instead of array for Direction choices --- src/ch/epfl/maze/physical/zoo/Monkey.java | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/ch/epfl/maze/physical/zoo/Monkey.java') 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; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.Set; /** * Monkey A.I. that puts its hand on the left wall and follows it. @@ -31,13 +29,12 @@ public class Monkey extends Animal { /** * Finds a currentDirection following the "left paw rule". * - * @param choices An array of possible directions + * @param choices A set of possible directions * @return The currentDirection to take according to the "left paw rule" */ - private Direction followLeft(Direction[] choices) { - List choiceList = new ArrayList<>(Arrays.asList(choices)); + private Direction followLeft(Set choices) { Direction dir = this.currentDirection.rotateLeft(); - while (!choiceList.contains(dir)) dir = dir.rotateRight(); + while (!choices.contains(dir)) dir = dir.rotateRight(); return dir; } @@ -45,13 +42,13 @@ public class Monkey extends Animal { /** * Determines the strategy to follow depending on special cases. * - * @param choices An array of possible directions + * @param choices A set of possible directions * @return The Direction to take */ - private Direction findDirection(Direction[] choices) { - if (choices.length == 0) return Direction.NONE; - if (this.currentDirection == Direction.NONE) return choices[0]; - if (choices.length == 1) return choices[0]; + private Direction findDirection(Set choices) { + if (choices.isEmpty()) return Direction.NONE; + if (this.currentDirection == Direction.NONE) return choices.stream().findAny().get(); + if (choices.size() == 1) return choices.stream().findAny().get(); return this.followLeft(choices); } @@ -59,7 +56,7 @@ public class Monkey extends Animal { * Moves according to the relative left wall that the monkey has to follow. */ @Override - public Direction move(Direction[] choices) { + public Direction move(Set choices) { this.currentDirection = this.findDirection(choices); return this.currentDirection; } -- cgit v1.2.3