diff options
author | Pacien TRAN-GIRARD | 2015-11-21 17:31:08 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 17:31:08 +0100 |
commit | 9dfd4cbfce88951f196cfbb86154d7d7b2320cf3 (patch) | |
tree | 0513d3d269b05d08a526b880664d06e9e112ad1b /src/ch | |
parent | 6add32eb44cf4a9e3228c74f79742ade424f08b1 (diff) | |
download | maze-solver-9dfd4cbfce88951f196cfbb86154d7d7b2320cf3.tar.gz |
Add ProbabilisticAnimal type and adapt assets retrieval accordingly
Diffstat (limited to 'src/ch')
-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 | } | ||