diff options
author | Pacien TRAN-GIRARD | 2015-11-24 23:43:00 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-24 23:43:00 +0100 |
commit | 4da9bdc4fa2f44eedba3dff29af7b0ce9180e442 (patch) | |
tree | a7bb129adc714d4c06e2fdeb0008343aa816bd97 /src/ch/epfl/maze/physical | |
parent | 5c943f7c7b53d850d7ad3413183fef7f001c6cc4 (diff) | |
download | maze-solver-4da9bdc4fa2f44eedba3dff29af7b0ce9180e442.tar.gz |
Refactor Ghosts
Diffstat (limited to 'src/ch/epfl/maze/physical')
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Blinky.java | 3 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Clyde.java | 3 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Ghost.java (renamed from src/ch/epfl/maze/physical/GhostPredator.java) | 185 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Inky.java | 15 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/pacman/Pinky.java | 3 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java | 51 |
6 files changed, 163 insertions, 97 deletions
diff --git a/src/ch/epfl/maze/physical/pacman/Blinky.java b/src/ch/epfl/maze/physical/pacman/Blinky.java index 4e8c4a0..5f81d13 100644 --- a/src/ch/epfl/maze/physical/pacman/Blinky.java +++ b/src/ch/epfl/maze/physical/pacman/Blinky.java | |||
@@ -2,7 +2,6 @@ package ch.epfl.maze.physical.pacman; | |||
2 | 2 | ||
3 | import ch.epfl.maze.physical.Animal; | 3 | 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.GhostPredator; | ||
6 | import ch.epfl.maze.util.Vector2D; | 5 | import ch.epfl.maze.util.Vector2D; |
7 | 6 | ||
8 | /** | 7 | /** |
@@ -11,7 +10,7 @@ import ch.epfl.maze.util.Vector2D; | |||
11 | * @author EPFL | 10 | * @author EPFL |
12 | * @author Pacien TRAN-GIRARD | 11 | * @author Pacien TRAN-GIRARD |
13 | */ | 12 | */ |
14 | public class Blinky extends GhostPredator { | 13 | public class Blinky extends Ghost { |
15 | 14 | ||
16 | /** | 15 | /** |
17 | * Constructs a Blinky with a starting position. | 16 | * Constructs a Blinky with a starting position. |
diff --git a/src/ch/epfl/maze/physical/pacman/Clyde.java b/src/ch/epfl/maze/physical/pacman/Clyde.java index 40089db..2d7e47a 100644 --- a/src/ch/epfl/maze/physical/pacman/Clyde.java +++ b/src/ch/epfl/maze/physical/pacman/Clyde.java | |||
@@ -2,7 +2,6 @@ package ch.epfl.maze.physical.pacman; | |||
2 | 2 | ||
3 | import ch.epfl.maze.physical.Animal; | 3 | 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.GhostPredator; | ||
6 | import ch.epfl.maze.util.Vector2D; | 5 | import ch.epfl.maze.util.Vector2D; |
7 | 6 | ||
8 | /** | 7 | /** |
@@ -12,7 +11,7 @@ import ch.epfl.maze.util.Vector2D; | |||
12 | * @author EPFL | 11 | * @author EPFL |
13 | * @author Pacien TRAN-GIRARD | 12 | * @author Pacien TRAN-GIRARD |
14 | */ | 13 | */ |
15 | public class Clyde extends GhostPredator { | 14 | public class Clyde extends Ghost { |
16 | 15 | ||
17 | private static double PROXIMITY_THRESHOLD = 4.0d; | 16 | private static double PROXIMITY_THRESHOLD = 4.0d; |
18 | 17 | ||
diff --git a/src/ch/epfl/maze/physical/GhostPredator.java b/src/ch/epfl/maze/physical/pacman/Ghost.java index c1cfca8..f8f511e 100644 --- a/src/ch/epfl/maze/physical/GhostPredator.java +++ b/src/ch/epfl/maze/physical/pacman/Ghost.java | |||
@@ -1,5 +1,11 @@ | |||
1 | package ch.epfl.maze.physical; | 1 | package ch.epfl.maze.physical.pacman; |
2 | 2 | ||
3 | import ch.epfl.maze.physical.Daedalus; | ||
4 | import ch.epfl.maze.physical.Predator; | ||
5 | import ch.epfl.maze.physical.Prey; | ||
6 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; | ||
7 | import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; | ||
8 | import ch.epfl.maze.physical.stragegies.reducer.CostReducer; | ||
3 | import ch.epfl.maze.util.Direction; | 9 | import ch.epfl.maze.util.Direction; |
4 | import ch.epfl.maze.util.Vector2D; | 10 | import ch.epfl.maze.util.Vector2D; |
5 | 11 | ||
@@ -11,7 +17,7 @@ import java.util.Set; | |||
11 | * | 17 | * |
12 | * @author Pacien TRAN-GIRARD | 18 | * @author Pacien TRAN-GIRARD |
13 | */ | 19 | */ |
14 | abstract public class GhostPredator extends Predator { | 20 | abstract public class Ghost extends Predator implements BackwardReducer, CostReducer, RandomPicker { |
15 | 21 | ||
16 | public enum Mode { | 22 | public enum Mode { |
17 | CHASE(40), | 23 | CHASE(40), |
@@ -58,7 +64,7 @@ abstract public class GhostPredator extends Predator { | |||
58 | * | 64 | * |
59 | * @param position Position of the predator in the labyrinth | 65 | * @param position Position of the predator in the labyrinth |
60 | */ | 66 | */ |
61 | public GhostPredator(Vector2D position) { | 67 | public Ghost(Vector2D position) { |
62 | super(position); | 68 | super(position); |
63 | 69 | ||
64 | this.homePosition = position; | 70 | this.homePosition = position; |
@@ -68,16 +74,98 @@ abstract public class GhostPredator extends Predator { | |||
68 | } | 74 | } |
69 | 75 | ||
70 | /** | 76 | /** |
77 | * Returns the cost to reach the target by choosing the given Direction by calculating the Euclidean distance. | ||
78 | * | ||
79 | * @param choice The Direction choice | ||
80 | * @param daedalus The Daedalus | ||
81 | * @return The Euclidean distance cost | ||
82 | */ | ||
83 | @Override | ||
84 | public int getChoiceCost(Direction choice, Daedalus daedalus) { | ||
85 | Vector2D target = this.getTargetPosition(daedalus); | ||
86 | return (int) (this.getDistanceTo(choice, target) * 100); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public Set<Direction> reduce(Set<Direction> choices) { | ||
91 | return EnumSet.noneOf(Direction.class); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position | ||
96 | * after excluding the provenance if possible. | ||
97 | * | ||
98 | * @param choices A set of Direction choices | ||
99 | * @param daedalus The Daedalus | ||
100 | * @return A set of optimal Direction choices | ||
101 | */ | ||
102 | @Override | ||
103 | public Set<Direction> reduce(Set<Direction> choices, Daedalus daedalus) { | ||
104 | Set<Direction> forwardChoices = choices.size() > 1 ? BackwardReducer.super.reduce(choices) : choices; | ||
105 | return CostReducer.super.reduce(forwardChoices, daedalus); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public Direction move(Set<Direction> choices, Daedalus daedalus) { | ||
110 | this.countCycle(); | ||
111 | |||
112 | Set<Direction> bestChoices = this.reduce(choices, daedalus); | ||
113 | return this.pick(bestChoices); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * Returns the Prey's projected targeted position. | ||
118 | * | ||
119 | * @param daedalus The Daedalus | ||
120 | * @return The projected position | ||
121 | */ | ||
122 | abstract protected Vector2D getPreyTargetPosition(Daedalus daedalus); | ||
123 | |||
124 | /** | ||
125 | * Returns the current Mode. | ||
126 | * | ||
127 | * @param daedalus The Daedalus | ||
128 | * @return The current Mode | ||
129 | */ | ||
130 | protected Mode getMode(Daedalus daedalus) { | ||
131 | return this.mode; | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Returns the position to target according to the current Mode. | ||
136 | * | ||
137 | * @param daedalus The Daedalus | ||
138 | * @return The position to target | ||
139 | */ | ||
140 | protected Vector2D getTargetPosition(Daedalus daedalus) { | ||
141 | switch (this.getMode(daedalus)) { | ||
142 | case CHASE: | ||
143 | return this.getPreyTargetPosition(daedalus); | ||
144 | case SCATTER: | ||
145 | return this.homePosition; | ||
146 | default: | ||
147 | return this.homePosition; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | /** | ||
71 | * Selects a new random Prey to chase in the Daedalus. | 152 | * Selects a new random Prey to chase in the Daedalus. |
72 | * | 153 | * |
73 | * @param daedalus The Daedalus | 154 | * @param daedalus The Daedalus |
74 | * @return The Chosen One | 155 | * @return The Chosen One |
75 | */ | 156 | */ |
76 | private static Prey selectRandomPrey(Daedalus daedalus) { | 157 | private static Prey selectAnyPrey(Daedalus daedalus) { |
77 | if (daedalus.getPreys().isEmpty()) return null; | 158 | if (daedalus.getPreySet().isEmpty()) return null; |
159 | return daedalus.getPreySet().stream().findAny().get(); | ||
160 | } | ||
78 | 161 | ||
79 | int randomPreyIndex = GhostPredator.RANDOM_SOURCE.nextInt(daedalus.getPreys().size()); | 162 | /** |
80 | return daedalus.getPreys().get(randomPreyIndex); | 163 | * Sets a random Prey as the common Pre. |
164 | * | ||
165 | * @param daedalus The Daedalus | ||
166 | */ | ||
167 | private static synchronized void setAnyPrey(Daedalus daedalus) { | ||
168 | Ghost.commonPrey = Ghost.selectAnyPrey(daedalus); | ||
81 | } | 169 | } |
82 | 170 | ||
83 | /** | 171 | /** |
@@ -87,10 +175,10 @@ abstract public class GhostPredator extends Predator { | |||
87 | * @return The common Prey | 175 | * @return The common Prey |
88 | */ | 176 | */ |
89 | private static Prey getPrey(Daedalus daedalus) { | 177 | private static Prey getPrey(Daedalus daedalus) { |
90 | if (GhostPredator.commonPrey == null || !daedalus.hasPrey(GhostPredator.commonPrey)) | 178 | if (Ghost.commonPrey == null || !daedalus.hasPrey(Ghost.commonPrey)) |
91 | GhostPredator.commonPrey = GhostPredator.selectRandomPrey(daedalus); | 179 | Ghost.setAnyPrey(daedalus); |
92 | 180 | ||
93 | return GhostPredator.commonPrey; | 181 | return Ghost.commonPrey; |
94 | } | 182 | } |
95 | 183 | ||
96 | /** | 184 | /** |
@@ -100,7 +188,7 @@ abstract public class GhostPredator extends Predator { | |||
100 | * @return The position of the Prey | 188 | * @return The position of the Prey |