From d9f6761221c25c19e96b2f173d4db0a27b6c8e9d Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 23 Nov 2015 10:34:41 +0100 Subject: Properly define class constants and protect object attributes --- src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 16 ++++++++++++---- src/ch/epfl/maze/physical/zoo/Panda.java | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java index 45c2175..ed26d23 100644 --- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java @@ -15,8 +15,9 @@ import java.util.stream.Collectors; */ abstract public class ProbabilisticAnimal extends Animal { - private final Random randomSource; - protected Direction currentDirection; + public static final Random RANDOM_SOURCE = new Random(); + + private Direction currentDirection; /** * Constructs a probabilistic animal with a starting position @@ -25,10 +26,17 @@ abstract public class ProbabilisticAnimal extends Animal { */ public ProbabilisticAnimal(Vector2D position) { super(position); // no pun intended - this.randomSource = new Random(); this.currentDirection = Direction.NONE; } + protected Direction getCurrentDirection() { + return this.currentDirection; + } + + protected void setCurrentDirection(Direction direction) { + this.currentDirection = direction; + } + /** * Excludes the given direction from possible choices. * @@ -61,7 +69,7 @@ abstract public class ProbabilisticAnimal extends Animal { * @return A random Direction taken from the given choices */ protected Direction getRandomDirection(Direction[] choices) { - return choices[randomSource.nextInt(choices.length)]; + return choices[RANDOM_SOURCE.nextInt(choices.length)]; } /** diff --git a/src/ch/epfl/maze/physical/zoo/Panda.java b/src/ch/epfl/maze/physical/zoo/Panda.java index fecec84..dc1198e 100644 --- a/src/ch/epfl/maze/physical/zoo/Panda.java +++ b/src/ch/epfl/maze/physical/zoo/Panda.java @@ -87,7 +87,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.currentDirection.reverse()}; + return new Direction[]{this.getCurrentDirection().reverse()}; // general case for (Trail.Marking mark : Trail.Marking.ALL) { -- cgit v1.2.3