From 10627af7a85b50bce965245a472d9b69e928d0ba Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 22:53:24 +0100 Subject: Generalize turn back avoidance to all ProbabilisticAnimals --- src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 38 ++++++++++++++++++--- src/ch/epfl/maze/physical/zoo/Mouse.java | 39 +--------------------- 2 files changed, 35 insertions(+), 42 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 f62fea1..c4d4f2a 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java @@ -3,37 +3,67 @@ package ch.epfl.maze.physical; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; +import java.util.stream.Collectors; /** * A probabilistic animal that use a random component in its decision making process. * * @author Pacien TRAN-GIRARD */ - abstract public class ProbabilisticAnimal extends Animal { private final Random randomSource; + private Direction currentDirection; /** * Constructs a probabilistic animal with a starting position * * @param position Starting position of the probabilistic animal in the labyrinth */ - public ProbabilisticAnimal(Vector2D position) { super(position); // no pun intended this.randomSource = new Random(); + this.currentDirection = Direction.NONE; + } + + /** + * Excludes the origin direction for choices. + * + * @param choices An array of choices + * @return An array of smart choices + */ + protected Direction[] excludeOrigin(Direction[] choices) { + return (new ArrayList<>(Arrays.asList(choices))) + .stream() + .filter(dir -> !dir.isOpposite(this.currentDirection)) + .collect(Collectors.toList()) + .toArray(new Direction[0]); } /** - * Moves according to a random walk. + * Returns a random Direction from the given choices. + * + * @param choices An array of Direction + * @return A random Direction taken from the given choices */ + protected Direction getRandomDirection(Direction[] choices) { + return choices[randomSource.nextInt(choices.length)]; + } + /** + * Moves according to an improved version of a random walk : the + * probabilistic animal does not directly retrace its steps if not forced. + */ @Override public Direction move(Direction[] choices) { if (choices.length == 0) return Direction.NONE; - return choices[randomSource.nextInt(choices.length)]; + + Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; + this.currentDirection = this.getRandomDirection(smartChoices); + return this.currentDirection; } } diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java index b5483c1..08d7f3d 100644 --- a/src/ch/epfl/maze/physical/zoo/Mouse.java +++ b/src/ch/epfl/maze/physical/zoo/Mouse.java @@ -2,64 +2,27 @@ package ch.epfl.maze.physical.zoo; import ch.epfl.maze.physical.Animal; import ch.epfl.maze.physical.ProbabilisticAnimal; -import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.stream.Collectors; - /** * Mouse A.I. that remembers only the previous choice it has made. * * @author Pacien TRAN-GIRARD */ - public class Mouse extends ProbabilisticAnimal { - private Direction previousChoice; - /** * Constructs a mouse with a starting position. * * @param position Starting position of the mouse in the labyrinth */ - public Mouse(Vector2D position) { super(position); - this.previousChoice = Direction.NONE; - } - - /** - * Excludes the origin direction for choices. - * - * @param choices An array of choices - * @return An array of smart choices - */ - - private Direction[] excludeOrigin(Direction[] choices) { - return (new ArrayList<>(Arrays.asList(choices))) - .stream() - .filter(dir -> !dir.isOpposite(this.previousChoice)) - .collect(Collectors.toList()) - .toArray(new Direction[0]); - } - - /** - * Moves according to an improved version of a random walk : the - * mouse does not directly retrace its steps. - */ - - @Override - public Direction move(Direction[] choices) { - Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; - Direction dir = super.move(smartChoices); - this.previousChoice = dir; - return dir; } @Override public Animal copy() { return new Mouse(this.getPosition()); } + } -- cgit v1.2.3