From 9dfd4cbfce88951f196cfbb86154d7d7b2320cf3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 17:31:08 +0100 Subject: Add ProbabilisticAnimal type and adapt assets retrieval accordingly --- src/ch/epfl/maze/graphics/Animation.java | 25 +++++++++++++- src/ch/epfl/maze/physical/ProbabilisticAnimal.java | 38 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/ch/epfl/maze/physical/ProbabilisticAnimal.java (limited to 'src') 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; /** * Handles the animation of a {@code Simulation} by extrapolating the positions * of animals. + * + * @author Pacien TRAN-GIRARD */ public final class Animation { @@ -226,7 +228,9 @@ public final class Animation { private BufferedImage loadImage(Animal animal) { // path = "img/superclass/class.png" - String folder = animal.getClass().getSuperclass().getSimpleName(); + String superClassName = animal.getClass().getSuperclass().getSimpleName(); + String[] superClassComponents = this.splitCamelCase(superClassName); + String folder = superClassComponents[superClassComponents.length - 1]; String file = animal.getClass().getSimpleName(); String path = "img/" + folder + File.separator + file + ".png"; @@ -243,4 +247,23 @@ public final class Animation { return img; } + + /** + * Splits a camel case string + * http://stackoverflow.com/a/2560017/1348634 + * + * @param s A string + * @return An array of words + */ + private static String[] splitCamelCase(String s) { + return s.replaceAll( + String.format("%s|%s|%s", + "(?<=[A-Z])(?=[A-Z][a-z])", + "(?<=[^A-Z])(?=[A-Z])", + "(?<=[A-Za-z])(?=[^A-Za-z])" + ), + " " + ).split(" "); + } + } 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 @@ +package ch.epfl.maze.physical; + +import ch.epfl.maze.util.Direction; +import ch.epfl.maze.util.Vector2D; + +import java.util.Random; + +/** + * A probabilistic animal that use a random component in its decision making process. + * + * @author Pacien TRAN-GIRARD + */ + +abstract public class ProbabilisticAnimal extends Animal { + + private final Random randomSource; + + /** + * Constructs a probabilistic animal with a starting position + * + * @param position Starting position of the probabilistic animal in the labyrinth + */ + + public ProbabilisticAnimal(Vector2D position) { + super(position); // no pun intended + this.randomSource = new Random(); + } + + /** + * Moves according to a random walk. + */ + + @Override + public Direction move(Direction[] choices) { + return choices[randomSource.nextInt(choices.length)]; + } + +} -- cgit v1.2.3