diff options
-rw-r--r-- | src/ch/epfl/maze/graphics/Animation.java | 25 | ||||
-rw-r--r-- | src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 38 |
2 files changed, 62 insertions, 1 deletions
diff --git a/src/ch/epfl/maze/graphics/Animation.java b/src/ch/epfl/maze/graphics/Animation.java index 47e0fc8..cd5fa38 100644 --- a/src/ch/epfl/maze/graphics/Animation.java +++ b/src/ch/epfl/maze/graphics/Animation.java | |||
@@ -19,6 +19,8 @@ import java.util.TreeMap; | |||
19 | /** | 19 | /** |
20 | * Handles the animation of a {@code Simulation} by extrapolating the positions | 20 | * Handles the animation of a {@code Simulation} by extrapolating the positions |
21 | * of animals. | 21 | * of animals. |
22 | * | ||
23 | * @author Pacien TRAN-GIRARD | ||
22 | */ | 24 | */ |
23 | 25 | ||
24 | public final class Animation { | 26 | public final class Animation { |
@@ -226,7 +228,9 @@ public final class Animation { | |||
226 | 228 | ||
227 | private BufferedImage loadImage(Animal animal) { | 229 | private BufferedImage loadImage(Animal animal) { |
228 | // path = "img/superclass/class.png" | 230 | // path = "img/superclass/class.png" |
229 | String folder = animal.getClass().getSuperclass().getSimpleName(); | 231 | String superClassName = animal.getClass().getSuperclass().getSimpleName(); |
232 | String[] superClassComponents = this.splitCamelCase(superClassName); | ||
233 | String folder = superClassComponents[superClassComponents.length - 1]; | ||
230 | String file = animal.getClass().getSimpleName(); | 234 | String file = animal.getClass().getSimpleName(); |
231 | String path = "img/" + folder + File.separator + file + ".png"; | 235 | String path = "img/" + folder + File.separator + file + ".png"; |
232 | 236 | ||
@@ -243,4 +247,23 @@ public final class Animation { | |||
243 | 247 | ||
244 | return img; | 248 | return img; |
245 | } | 249 | } |
250 | |||
251 | /** | ||
252 | * Splits a camel case string | ||
253 | * http://stackoverflow.com/a/2560017/1348634 | ||
254 | * | ||
255 | * @param s A string | ||
256 | * @return An array of words | ||
257 | */ | ||
258 | private static String[] splitCamelCase(String s) { | ||
259 | return s.replaceAll( | ||
260 | String.format("%s|%s|%s", | ||
261 | "(?<=[A-Z])(?=[A-Z][a-z])", | ||
262 | "(?<=[^A-Z])(?=[A-Z])", | ||
263 | "(?<=[A-Za-z])(?=[^A-Za-z])" | ||
264 | ), | ||
265 | " " | ||
266 | ).split(" "); | ||
267 | } | ||
268 | |||
246 | } | 269 | } |
diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java new file mode 100644 index 0000000..6b0c57f --- /dev/null +++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java | |||
@@ -0,0 +1,38 @@ | |||
1 | package ch.epfl.maze.physical; | ||
2 | |||
3 | import ch.epfl.maze.util.Direction; | ||
4 | import ch.epfl.maze.util.Vector2D; | ||
5 | |||
6 | import java.util.Random; | ||
7 | |||
8 | /** | ||
9 | * A probabilistic animal that use a random component in its decision making process. | ||
10 | * | ||
11 | * @author Pacien TRAN-GIRARD | ||
12 | */ | ||
13 | |||
14 | abstract public class ProbabilisticAnimal extends Animal { | ||
15 | |||
16 | private final Random randomSource; | ||
17 | |||
18 | /** | ||
19 | * Constructs a probabilistic animal with a starting position | ||
20 | * | ||
21 | * @param position Starting position of the probabilistic animal in the labyrinth | ||
22 | */ | ||
23 | |||
24 | public ProbabilisticAnimal(Vector2D position) { | ||
25 | super(position); // no pun intended | ||
26 | this.randomSource = new Random(); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Moves according to a <i>random walk</i>. | ||
31 | */ | ||
32 | |||
33 | @Override | ||
34 | public Direction move(Direction[] choices) { | ||
35 | return choices[randomSource.nextInt(choices.length)]; | ||
36 | } | ||
37 | |||
38 | } | ||