diff options
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo')
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Mouse.java | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java index e5cb9ae..1b22c66 100644 --- a/src/ch/epfl/maze/physical/zoo/Mouse.java +++ b/src/ch/epfl/maze/physical/zoo/Mouse.java | |||
@@ -1,14 +1,22 @@ | |||
1 | package ch.epfl.maze.physical.zoo; | 1 | 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.util.Direction; | 5 | import ch.epfl.maze.util.Direction; |
5 | import ch.epfl.maze.util.Vector2D; | 6 | import ch.epfl.maze.util.Vector2D; |
6 | 7 | ||
8 | import java.util.ArrayList; | ||
9 | import java.util.List; | ||
10 | |||
7 | /** | 11 | /** |
8 | * Mouse A.I. that remembers only the previous choice it has made. | 12 | * Mouse A.I. that remembers only the previous choice it has made. |
13 | * | ||
14 | * @author Pacien TRAN-GIRARD | ||
9 | */ | 15 | */ |
10 | 16 | ||
11 | public class Mouse extends Animal { | 17 | public class Mouse extends ProbabilisticAnimal { |
18 | |||
19 | private Direction previousChoice; | ||
12 | 20 | ||
13 | /** | 21 | /** |
14 | * Constructs a mouse with a starting position. | 22 | * Constructs a mouse with a starting position. |
@@ -18,6 +26,24 @@ public class Mouse extends Animal { | |||
18 | 26 | ||
19 | public Mouse(Vector2D position) { | 27 | public Mouse(Vector2D position) { |
20 | super(position); | 28 | super(position); |
29 | this.previousChoice = 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 | |||
39 | private Direction[] excludeOrigin(Direction[] choices) { | ||
40 | List<Direction> smartChoices = new ArrayList<>(choices.length - 1); // max size, excluding origin | ||
41 | |||
42 | for (Direction dir : choices) | ||
43 | if (!dir.isOpposite(this.previousChoice)) | ||
44 | smartChoices.add(dir); | ||
45 | |||
46 | return smartChoices.toArray(new Direction[smartChoices.size()]); | ||
21 | } | 47 | } |
22 | 48 | ||
23 | /** | 49 | /** |
@@ -27,13 +53,14 @@ public class Mouse extends Animal { | |||
27 | 53 | ||
28 | @Override | 54 | @Override |
29 | public Direction move(Direction[] choices) { | 55 | public Direction move(Direction[] choices) { |
30 | // TODO | 56 | Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; |
31 | return Direction.NONE; | 57 | Direction dir = super.move(smartChoices); |
58 | this.previousChoice = dir; | ||
59 | return dir; | ||
32 | } | 60 | } |
33 | 61 | ||
34 | @Override | 62 | @Override |
35 | public Animal copy() { | 63 | public Animal copy() { |
36 | // TODO | 64 | return new Mouse(this.getPosition()); |
37 | return null; | ||
38 | } | 65 | } |
39 | } | 66 | } |