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