From 76eba89ebb2da6d19ac37dba5741a717edd8dd8e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 22 Nov 2015 16:02:57 +0100 Subject: Generalise and expose choices exclusion methods --- src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/ch/epfl/maze') diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java index c4d4f2a..6267500 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; abstract public class ProbabilisticAnimal extends Animal { private final Random randomSource; - private Direction currentDirection; + protected Direction currentDirection; /** * Constructs a probabilistic animal with a starting position @@ -30,19 +30,30 @@ abstract public class ProbabilisticAnimal extends Animal { } /** - * Excludes the origin direction for choices. + * Excludes the given direction from possible choices. * - * @param choices An array of choices + * @param choices An array of choices + * @param toExclude The Direction to exclude * @return An array of smart choices */ - protected Direction[] excludeOrigin(Direction[] choices) { + protected Direction[] excludeDirection(Direction[] choices, Direction toExclude) { return (new ArrayList<>(Arrays.asList(choices))) .stream() - .filter(dir -> !dir.isOpposite(this.currentDirection)) + .filter(dir -> dir != toExclude) .collect(Collectors.toList()) .toArray(new Direction[0]); } + /** + * Excludes the origin direction from possible choices. + * + * @param choices An array of choices + * @return An array of smart choices + */ + protected Direction[] excludeOrigin(Direction[] choices) { + return this.excludeDirection(choices, this.currentDirection.reverse()); + } + /** * Returns a random Direction from the given choices. * -- cgit v1.2.3