From 5f9be99e7ebd4a90e666e6efbe9e9d4b3b8008e7 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 12:56:50 +0100 Subject: Move directional attribute to Animal --- src/ch/epfl/maze/physical/Animal.java | 24 +++++++++++++++++++++- src/ch/epfl/maze/physical/GhostPredator.java | 2 +- src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 16 ++------------- src/ch/epfl/maze/physical/zoo/Panda.java | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/ch/epfl/maze/physical/Animal.java b/src/ch/epfl/maze/physical/Animal.java index 3697c32..34a1136 100644 --- a/src/ch/epfl/maze/physical/Animal.java +++ b/src/ch/epfl/maze/physical/Animal.java @@ -13,6 +13,18 @@ import ch.epfl.maze.util.Vector2D; abstract public class Animal { private Vector2D position; + private Direction direction; + + /** + * Constructs an animal with a specified position and direction. + * + * @param position Position of the animal in the labyrinth + * @param direction Direction the animal is facing + */ + public Animal(Vector2D position, Direction direction) { + this.position = position; + this.direction = direction; + } /** * Constructs an animal with a specified position. @@ -20,7 +32,7 @@ abstract public class Animal { * @param position Position of the animal in the labyrinth */ public Animal(Vector2D position) { - this.position = position; + this(position, Direction.NONE); } /** @@ -44,6 +56,7 @@ abstract public class Animal { */ public final void update(Direction dir) { this.position = this.position.addDirectionTo(dir); + this.direction = dir; } /** @@ -67,6 +80,15 @@ abstract public class Animal { return this.position; } + /** + * Returns the direction of the Animal. + * + * @return The current direction of the Animal + */ + public final Direction getDirection() { + return this.direction; + } + abstract public Animal copy(); } diff --git a/src/ch/epfl/maze/physical/GhostPredator.java b/src/ch/epfl/maze/physical/GhostPredator.java index 5408bf6..e4a64b3 100644 --- a/src/ch/epfl/maze/physical/GhostPredator.java +++ b/src/ch/epfl/maze/physical/GhostPredator.java @@ -116,7 +116,7 @@ abstract public class GhostPredator extends Predator { Prey prey = GhostPredator.getPrey(daedalus); if (prey == null) return Direction.NONE; - return prey.getCurrentDirection(); + return prey.getDirection(); } /** diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java index ed26d23..650cecf 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java @@ -17,8 +17,6 @@ abstract public class ProbabilisticAnimal extends Animal { public static final Random RANDOM_SOURCE = new Random(); - private Direction currentDirection; - /** * Constructs a probabilistic animal with a starting position * @@ -26,15 +24,6 @@ abstract public class ProbabilisticAnimal extends Animal { */ public ProbabilisticAnimal(Vector2D position) { super(position); // no pun intended - this.currentDirection = Direction.NONE; - } - - protected Direction getCurrentDirection() { - return this.currentDirection; - } - - protected void setCurrentDirection(Direction direction) { - this.currentDirection = direction; } /** @@ -59,7 +48,7 @@ abstract public class ProbabilisticAnimal extends Animal { * @return An array of smart choices */ protected Direction[] excludeOrigin(Direction[] choices) { - return this.excludeDirection(choices, this.currentDirection.reverse()); + return this.excludeDirection(choices, this.getDirection().reverse()); } /** @@ -81,8 +70,7 @@ abstract public class ProbabilisticAnimal extends Animal { if (choices.length == 0) return Direction.NONE; Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; - this.currentDirection = this.getRandomDirection(smartChoices); - return this.currentDirection; + return this.getRandomDirection(smartChoices); } } diff --git a/src/ch/epfl/maze/physical/zoo/Panda.java b/src/ch/epfl/maze/physical/zoo/Panda.java index b794d10..ce38fda 100644 --- a/src/ch/epfl/maze/physical/zoo/Panda.java +++ b/src/ch/epfl/maze/physical/zoo/Panda.java @@ -88,7 +88,7 @@ public class Panda extends ProbabilisticAnimal { private Direction[] selectBestDirections(Direction[] choices) { // special case if (this.isIntersection(choices) && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) - return new Direction[]{this.getCurrentDirection().reverse()}; + return new Direction[]{this.getDirection().reverse()}; // general case for (Trail.Marking mark : Trail.Marking.ALL) { -- cgit v1.2.3