diff options
author | Pacien TRAN-GIRARD | 2015-11-25 00:07:56 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-25 00:07:56 +0100 |
commit | c5e5646b7785e1b5d1c46efd5e93a6f95d5bad9f (patch) | |
tree | eaa41d5e140f74c86b1d422c465919fc85ca0e6b /src | |
parent | 4da9bdc4fa2f44eedba3dff29af7b0ce9180e442 (diff) | |
download | maze-solver-c5e5646b7785e1b5d1c46efd5e93a6f95d5bad9f.tar.gz |
Implement PacMan predator avoidance
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Ghost.java | 18 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/PacMan.java | 19 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/stragegies/planner/DistanceCalculator.java | 33 |
3 files changed, 51 insertions, 19 deletions
diff --git a/src/ch/epfl/maze/physical/pacman/Ghost.java b/src/ch/epfl/maze/physical/pacman/Ghost.java index f8f511e..77d0e25 100644 --- a/src/ch/epfl/maze/physical/pacman/Ghost.java +++ b/src/ch/epfl/maze/physical/pacman/Ghost.java | |||
@@ -4,6 +4,7 @@ import ch.epfl.maze.physical.Daedalus; | |||
4 | import ch.epfl.maze.physical.Predator; | 4 | import ch.epfl.maze.physical.Predator; |
5 | import ch.epfl.maze.physical.Prey; | 5 | import ch.epfl.maze.physical.Prey; |
6 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; | 6 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; |
7 | import ch.epfl.maze.physical.stragegies.planner.DistanceCalculator; | ||
7 | import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; | 8 | import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; |
8 | import ch.epfl.maze.physical.stragegies.reducer.CostReducer; | 9 | import ch.epfl.maze.physical.stragegies.reducer.CostReducer; |
9 | import ch.epfl.maze.util.Direction; | 10 | import ch.epfl.maze.util.Direction; |
@@ -17,7 +18,7 @@ import java.util.Set; | |||
17 | * | 18 | * |
18 | * @author Pacien TRAN-GIRARD | 19 | * @author Pacien TRAN-GIRARD |
19 | */ | 20 | */ |
20 | abstract public class Ghost extends Predator implements BackwardReducer, CostReducer, RandomPicker { | 21 | abstract public class Ghost extends Predator implements DistanceCalculator, BackwardReducer, CostReducer, RandomPicker { |
21 | 22 | ||
22 | public enum Mode { | 23 | public enum Mode { |
23 | CHASE(40), | 24 | CHASE(40), |
@@ -208,21 +209,6 @@ abstract public class Ghost extends Predator implements BackwardReducer, CostRed | |||
208 | } | 209 | } |
209 | 210 | ||
210 | /** | 211 | /** |
211 | * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position. | ||
212 | * | ||
213 | * @param dir The adjacent Direction | ||
214 | * @param targetPosition The targeted position | ||
215 | * @return The Euclidean distance between the two positions | ||
216 | */ | ||
217 | private double getDistanceTo(Direction dir, Vector2D targetPosition) { | ||
218 | return this | ||
219 | .getPosition() | ||
220 | .addDirectionTo(dir) | ||
221 | .sub(targetPosition) | ||
222 | .dist(); | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * Rotates to the next Mode. | 212 | * Rotates to the next Mode. |
227 | */ | 213 | */ |
228 | protected void rotateMode() { | 214 | protected void rotateMode() { |
diff --git a/src/ch/epfl/maze/physical/pacman/PacMan.java b/src/ch/epfl/maze/physical/pacman/PacMan.java index e2a55e9..5458dd5 100644 --- a/src/ch/epfl/maze/physical/pacman/PacMan.java +++ b/src/ch/epfl/maze/physical/pacman/PacMan.java | |||
@@ -4,7 +4,8 @@ import ch.epfl.maze.physical.Animal; | |||
4 | import ch.epfl.maze.physical.Daedalus; | 4 | import ch.epfl.maze.physical.Daedalus; |
5 | import ch.epfl.maze.physical.Prey; | 5 | import ch.epfl.maze.physical.Prey; |
6 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; | 6 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; |
7 | import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; | 7 | import ch.epfl.maze.physical.stragegies.planner.DistanceCalculator; |
8 | import ch.epfl.maze.physical.stragegies.reducer.CostReducer; | ||
8 | import ch.epfl.maze.util.Direction; | 9 | import ch.epfl.maze.util.Direction; |
9 | import ch.epfl.maze.util.Vector2D; | 10 | import ch.epfl.maze.util.Vector2D; |
10 | 11 | ||
@@ -16,7 +17,7 @@ import java.util.Set; | |||
16 | * @author EPFL | 17 | * @author EPFL |
17 | * @author Pacien TRAN-GIRARD | 18 | * @author Pacien TRAN-GIRARD |
18 | */ | 19 | */ |
19 | public class PacMan extends Prey implements BackwardReducer, RandomPicker { | 20 | public class PacMan extends Prey implements DistanceCalculator, CostReducer, RandomPicker { |
20 | 21 | ||
21 | /** | 22 | /** |
22 | * Constructs a new Pac-Man. | 23 | * Constructs a new Pac-Man. |
@@ -28,8 +29,20 @@ public class PacMan extends Prey implements BackwardReducer, RandomPicker { | |||
28 | } | 29 | } |
29 | 30 | ||
30 | @Override | 31 | @Override |
32 | public int getChoiceCost(Direction choice, Daedalus daedalus) { | ||
33 | return (-1) * daedalus | ||
34 | .getPredatorSet() | ||
35 | .stream() | ||
36 | .map(pred -> this.getDistanceTo(choice, pred.getPosition())) | ||
37 | .map(dist -> dist * 100.0d) | ||
38 | .min(Double::compare) | ||
39 | .get() | ||
40 | .intValue(); | ||
41 | } | ||
42 | |||
43 | @Override | ||
31 | public Direction move(Set<Direction> choices, Daedalus daedalus) { | 44 | public Direction move(Set<Direction> choices, Daedalus daedalus) { |
32 | Set<Direction> smartChoices = choices.size() > 1 ? this.reduce(choices) : choices; | 45 | Set<Direction> smartChoices = this.reduce(choices, daedalus); |
33 | return this.pick(smartChoices); | 46 | return this.pick(smartChoices); |
34 | } | 47 | } |
35 | 48 | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/planner/DistanceCalculator.java b/src/ch/epfl/maze/physical/stragegies/planner/DistanceCalculator.java new file mode 100644 index 0000000..daeb581 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/planner/DistanceCalculator.java | |||
@@ -0,0 +1,33 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.planner; | ||
2 | |||
3 | import ch.epfl.maze.util.Direction; | ||
4 | import ch.epfl.maze.util.Vector2D; | ||
5 | |||
6 | /** | ||
7 | * @author Pacien TRAN-GIRARD | ||
8 | */ | ||
9 | public interface DistanceCalculator { | ||
10 | |||
11 | /** | ||
12 | * Returns the current position. | ||
13 | * | ||
14 | * @return The current position vector | ||
15 | */ | ||
16 | Vector2D getPosition(); | ||
17 | |||
18 | /** | ||
19 | * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position. | ||
20 | * | ||
21 | * @param dir The adjacent Direction | ||
22 | * @param targetPosition The targeted position | ||
23 | * @return The Euclidean distance between the two positions | ||
24 | */ | ||
25 | default double getDistanceTo(Direction dir, Vector2D targetPosition) { | ||
26 | return this | ||
27 | .getPosition() | ||
28 | .addDirectionTo(dir) | ||
29 | .sub(targetPosition) | ||
30 | .dist(); | ||
31 | } | ||
32 | |||
33 | } | ||