diff options
author | Pacien TRAN-GIRARD | 2015-11-21 19:14:44 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 19:14:44 +0100 |
commit | 71342b11dab1ed49e611292849107a6eb9c41a67 (patch) | |
tree | a7d6ee6d5c4a5cbcfe92752183b93657fad9023c /src/ch/epfl/maze | |
parent | 00c2ba041b65d8777925da699125f10911f4a7e0 (diff) | |
download | maze-solver-71342b11dab1ed49e611292849107a6eb9c41a67.tar.gz |
Implement Monkey A.I.
Diffstat (limited to 'src/ch/epfl/maze')
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Monkey.java | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java index f0f9b2e..8aea75d 100644 --- a/src/ch/epfl/maze/physical/zoo/Monkey.java +++ b/src/ch/epfl/maze/physical/zoo/Monkey.java | |||
@@ -4,12 +4,20 @@ import ch.epfl.maze.physical.Animal; | |||
4 | import ch.epfl.maze.util.Direction; | 4 | import ch.epfl.maze.util.Direction; |
5 | import ch.epfl.maze.util.Vector2D; | 5 | import ch.epfl.maze.util.Vector2D; |
6 | 6 | ||
7 | import java.util.ArrayList; | ||
8 | import java.util.Arrays; | ||
9 | import java.util.List; | ||
10 | |||
7 | /** | 11 | /** |
8 | * Monkey A.I. that puts its hand on the left wall and follows it. | 12 | * Monkey A.I. that puts its hand on the left wall and follows it. |
13 | * | ||
14 | * @author Pacien TRAN-GIRARD | ||
9 | */ | 15 | */ |
10 | 16 | ||
11 | public class Monkey extends Animal { | 17 | public class Monkey extends Animal { |
12 | 18 | ||
19 | private Direction currentDirection; | ||
20 | |||
13 | /** | 21 | /** |
14 | * Constructs a monkey with a starting position. | 22 | * Constructs a monkey with a starting position. |
15 | * | 23 | * |
@@ -18,7 +26,25 @@ public class Monkey extends Animal { | |||
18 | 26 | ||
19 | public Monkey(Vector2D position) { | 27 | public Monkey(Vector2D position) { |
20 | super(position); | 28 | super(position); |
21 | // TODO | 29 | this.currentDirection = Direction.NONE; |
30 | } | ||
31 | |||
32 | /** | ||
33 | * Finds a currentDirection following the "left paw rule". | ||
34 | * | ||
35 | * @param choices An array of possible directions | ||
36 | * @return The currentDirection to take according to the "left paw rule" | ||
37 | */ | ||
38 | |||
39 | private Direction followLeft(Direction[] choices) { | ||
40 | if (choices.length == 0) return Direction.NONE; | ||
41 | if (choices.length == 1) return choices[0]; | ||
42 | |||
43 | List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices)); | ||
44 | Direction dir = this.currentDirection.rotateLeft(); | ||
45 | while (!choiceList.contains(dir)) dir = dir.rotateRight(); | ||
46 | |||
47 | return dir; | ||
22 | } | 48 | } |
23 | 49 | ||
24 | /** | 50 | /** |
@@ -27,13 +53,12 @@ public class Monkey extends Animal { | |||
27 | 53 | ||
28 | @Override | 54 | @Override |
29 | public Direction move(Direction[] choices) { | 55 | public Direction move(Direction[] choices) { |
30 | // TODO | 56 | this.currentDirection = this.followLeft(choices); |
31 | return Direction.NONE; | 57 | return this.currentDirection; |
32 | } | 58 | } |
33 | 59 | ||
34 | @Override | 60 | @Override |
35 | public Animal copy() { | 61 | public Animal copy() { |
36 | // TODO | 62 | return new Monkey(this.getPosition()); |
37 | return null; | ||
38 | } | 63 | } |
39 | } | 64 | } |