diff options
Diffstat (limited to 'src/ch/epfl/maze/physical/ProbabilisticAnimal.java')
-rw-r--r-- | src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 38 |
1 files changed, 34 insertions, 4 deletions
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; | |||
3 | import ch.epfl.maze.util.Direction; | 3 | import ch.epfl.maze.util.Direction; |
4 | import ch.epfl.maze.util.Vector2D; | 4 | import ch.epfl.maze.util.Vector2D; |
5 | 5 | ||
6 | import java.util.ArrayList; | ||
7 | import java.util.Arrays; | ||
6 | import java.util.Random; | 8 | import java.util.Random; |
9 | import java.util.stream.Collectors; | ||
7 | 10 | ||
8 | /** | 11 | /** |
9 | * A probabilistic animal that use a random component in its decision making process. | 12 | * A probabilistic animal that use a random component in its decision making process. |
10 | * | 13 | * |
11 | * @author Pacien TRAN-GIRARD | 14 | * @author Pacien TRAN-GIRARD |
12 | */ | 15 | */ |
13 | |||
14 | abstract public class ProbabilisticAnimal extends Animal { | 16 | abstract public class ProbabilisticAnimal extends Animal { |
15 | 17 | ||
16 | private final Random randomSource; | 18 | private final Random randomSource; |
19 | private Direction currentDirection; | ||
17 | 20 | ||
18 | /** | 21 | /** |
19 | * Constructs a probabilistic animal with a starting position | 22 | * Constructs a probabilistic animal with a starting position |
20 | * | 23 | * |
21 | * @param position Starting position of the probabilistic animal in the labyrinth | 24 | * @param position Starting position of the probabilistic animal in the labyrinth |
22 | */ | 25 | */ |
23 | |||
24 | public ProbabilisticAnimal(Vector2D position) { | 26 | public ProbabilisticAnimal(Vector2D position) { |
25 | super(position); // no pun intended | 27 | super(position); // no pun intended |
26 | this.randomSource = new Random(); | 28 | this.randomSource = new Random(); |
29 | this.currentDirection = Direction.NONE; | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Excludes the origin direction for choices. | ||
34 | * | ||
35 | * @param choices An array of choices | ||
36 | * @return An array of smart choices | ||
37 | */ | ||
38 | protected Direction[] excludeOrigin(Direction[] choices) { | ||
39 | return (new ArrayList<>(Arrays.asList(choices))) | ||
40 | .stream() | ||
41 | .filter(dir -> !dir.isOpposite(this.currentDirection)) | ||
42 | .collect(Collectors.toList()) | ||
43 | .toArray(new Direction[0]); | ||
27 | } | 44 | } |
28 | 45 | ||
29 | /** | 46 | /** |
30 | * Moves according to a <i>random walk</i>. | 47 | * Returns a random Direction from the given choices. |
48 | * | ||
49 | * @param choices An array of Direction | ||
50 | * @return A random Direction taken from the given choices | ||
31 | */ | 51 | */ |
52 | protected Direction getRandomDirection(Direction[] choices) { | ||
53 | return choices[randomSource.nextInt(choices.length)]; | ||
54 | } | ||
32 | 55 | ||
56 | /** | ||
57 | * Moves according to an improved version of a <i>random walk</i> : the | ||
58 | * probabilistic animal does not directly retrace its steps if not forced. | ||
59 | */ | ||
33 | @Override | 60 | @Override |
34 | public Direction move(Direction[] choices) { | 61 | public Direction move(Direction[] choices) { |
35 | if (choices.length == 0) return Direction.NONE; | 62 | if (choices.length == 0) return Direction.NONE; |
36 | return choices[randomSource.nextInt(choices.length)]; | 63 | |
64 | Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; | ||
65 | this.currentDirection = this.getRandomDirection(smartChoices); | ||
66 | return this.currentDirection; | ||
37 | } | 67 | } |
38 | 68 | ||
39 | } | 69 | } |