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/ProbabilisticAnimal.java | 33 +++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'src/ch/epfl/maze/physical/ProbabilisticAnimal.java') diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java index 650cecf..e461e8c 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java @@ -4,8 +4,9 @@ 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.Random; +import java.util.Set; import java.util.stream.Collectors; /** @@ -29,36 +30,36 @@ abstract public class ProbabilisticAnimal extends Animal { /** * Excludes the given direction from possible choices. * - * @param choices An array of choices + * @param choices A set of choices * @param toExclude The Direction to exclude - * @return An array of smart choices + * @return A set of smart choices */ - protected Direction[] excludeDirection(Direction[] choices, Direction toExclude) { - return (new ArrayList<>(Arrays.asList(choices))) + protected Set excludeDirection(Set choices, Direction toExclude) { + return choices .stream() .filter(dir -> dir != toExclude) - .collect(Collectors.toList()) - .toArray(new Direction[0]); + .collect(Collectors.toSet()); } /** * Excludes the origin direction from possible choices. * - * @param choices An array of choices - * @return An array of smart choices + * @param choices A set of choices + * @return A set of smart choices */ - protected Direction[] excludeOrigin(Direction[] choices) { + protected Set excludeOrigin(Set choices) { return this.excludeDirection(choices, this.getDirection().reverse()); } /** * Returns a random Direction from the given choices. * - * @param choices An array of Direction + * @param choices A set of Direction * @return A random Direction taken from the given choices */ - protected Direction getRandomDirection(Direction[] choices) { - return choices[RANDOM_SOURCE.nextInt(choices.length)]; + protected Direction getRandomDirection(Set choices) { + List choiceList = new ArrayList<>(choices); + return choiceList.get(RANDOM_SOURCE.nextInt(choices.size())); } /** @@ -66,10 +67,10 @@ abstract public class ProbabilisticAnimal extends Animal { * probabilistic animal does not directly retrace its steps if not forced. */ @Override - public Direction move(Direction[] choices) { - if (choices.length == 0) return Direction.NONE; + public Direction move(Set choices) { + if (choices.isEmpty()) return Direction.NONE; - Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; + Set smartChoices = choices.size() > 1 ? this.excludeOrigin(choices) : choices; return this.getRandomDirection(smartChoices); } -- cgit v1.2.3