diff options
author | Pacien TRAN-GIRARD | 2015-11-21 18:45:38 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 18:45:38 +0100 |
commit | 2d592a87e00c7d0248f53f388269de902afe6916 (patch) | |
tree | ec86b7d388dd4e7b4a583c38bf4ce5243f487a01 /src/ch | |
parent | 7ca733457566fdfa097e3317d96a50147229ca74 (diff) | |
download | maze-solver-2d592a87e00c7d0248f53f388269de902afe6916.tar.gz |
Implement Hamster A.I.
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Hamster.java | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java index 2a610f2..27706a5 100644 --- a/src/ch/epfl/maze/physical/zoo/Hamster.java +++ b/src/ch/epfl/maze/physical/zoo/Hamster.java | |||
@@ -1,15 +1,23 @@ | |||
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 | * Hamster A.I. that remembers the previous choice it has made and the dead ends | 12 | * Hamster A.I. that remembers the previous choice it has made and the dead ends |
9 | * it has already met. | 13 | * it has already met. |
14 | * | ||
15 | * @author Pacien TRAN-GIRARD | ||
10 | */ | 16 | */ |
11 | 17 | ||
12 | public class Hamster extends Animal { | 18 | public class Hamster extends ProbabilisticAnimal { |
19 | |||
20 | private final List<Vector2D> deadPaths; | ||
13 | 21 | ||
14 | /** | 22 | /** |
15 | * Constructs a hamster with a starting position. | 23 | * Constructs a hamster with a starting position. |
@@ -19,7 +27,24 @@ public class Hamster extends Animal { | |||
19 | 27 | ||
20 | public Hamster(Vector2D position) { | 28 | public Hamster(Vector2D position) { |
21 | super(position); | 29 | super(position); |
22 | // TODO | 30 | this.deadPaths = new ArrayList<>(); |
31 | } | ||
32 | |||
33 | /** | ||
34 | * Discard directions known to lead to dead ends. | ||
35 | * | ||
36 | * @param choices An array of choices | ||
37 | * @return An array of smart choices | ||
38 | */ | ||
39 | |||
40 | private Direction[] excludeDeadPaths(Direction[] choices) { | ||
41 | List<Direction> smartChoices = new ArrayList<>(); | ||
42 | |||
43 | for (Direction dir : choices) | ||
44 | if (!this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) | ||
45 | smartChoices.add(dir); | ||
46 | |||
47 | return smartChoices.toArray(new Direction[smartChoices.size()]); | ||
23 | } | 48 | } |
24 | 49 | ||
25 | /** | 50 | /** |
@@ -29,13 +54,13 @@ public class Hamster extends Animal { | |||
29 | 54 | ||
30 | @Override | 55 | @Override |
31 | public Direction move(Direction[] choices) { | 56 | public Direction move(Direction[] choices) { |
32 | // TODO | 57 | Direction[] smartChoices = this.excludeDeadPaths(choices); |
33 | return Direction.NONE; | 58 | if (smartChoices.length == 1) this.deadPaths.add(this.getPosition()); // dead end |
59 | return super.move(smartChoices); | ||
34 | } | 60 | } |
35 | 61 | ||
36 | @Override | 62 | @Override |
37 | public Animal copy() { | 63 | public Animal copy() { |
38 | // TODO | 64 | return new Hamster(this.getPosition()); |
39 | return null; | ||
40 | } | 65 | } |
41 | } | 66 | } |