From c5e5646b7785e1b5d1c46efd5e93a6f95d5bad9f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 25 Nov 2015 00:07:56 +0100 Subject: Implement PacMan predator avoidance --- src/ch/epfl/maze/physical/pacman/Ghost.java | 18 ++---------- src/ch/epfl/maze/physical/pacman/PacMan.java | 19 +++++++++++-- .../stragegies/planner/DistanceCalculator.java | 33 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 src/ch/epfl/maze/physical/stragegies/planner/DistanceCalculator.java 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; import ch.epfl.maze.physical.Predator; import ch.epfl.maze.physical.Prey; import ch.epfl.maze.physical.stragegies.picker.RandomPicker; +import ch.epfl.maze.physical.stragegies.planner.DistanceCalculator; import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; import ch.epfl.maze.physical.stragegies.reducer.CostReducer; import ch.epfl.maze.util.Direction; @@ -17,7 +18,7 @@ import java.util.Set; * * @author Pacien TRAN-GIRARD */ -abstract public class Ghost extends Predator implements BackwardReducer, CostReducer, RandomPicker { +abstract public class Ghost extends Predator implements DistanceCalculator, BackwardReducer, CostReducer, RandomPicker { public enum Mode { CHASE(40), @@ -207,21 +208,6 @@ abstract public class Ghost extends Predator implements BackwardReducer, CostRed return prey.getDirection(); } - /** - * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position. - * - * @param dir The adjacent Direction - * @param targetPosition The targeted position - * @return The Euclidean distance between the two positions - */ - private double getDistanceTo(Direction dir, Vector2D targetPosition) { - return this - .getPosition() - .addDirectionTo(dir) - .sub(targetPosition) - .dist(); - } - /** * Rotates to the next Mode. */ 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; import ch.epfl.maze.physical.Daedalus; import ch.epfl.maze.physical.Prey; import ch.epfl.maze.physical.stragegies.picker.RandomPicker; -import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; +import ch.epfl.maze.physical.stragegies.planner.DistanceCalculator; +import ch.epfl.maze.physical.stragegies.reducer.CostReducer; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; @@ -16,7 +17,7 @@ import java.util.Set; * @author EPFL * @author Pacien TRAN-GIRARD */ -public class PacMan extends Prey implements BackwardReducer, RandomPicker { +public class PacMan extends Prey implements DistanceCalculator, CostReducer, RandomPicker { /** * Constructs a new Pac-Man. @@ -27,9 +28,21 @@ public class PacMan extends Prey implements BackwardReducer, RandomPicker { super(position); } + @Override + public int getChoiceCost(Direction choice, Daedalus daedalus) { + return (-1) * daedalus + .getPredatorSet() + .stream() + .map(pred -> this.getDistanceTo(choice, pred.getPosition())) + .map(dist -> dist * 100.0d) + .min(Double::compare) + .get() + .intValue(); + } + @Override public Direction move(Set choices, Daedalus daedalus) { - Set smartChoices = choices.size() > 1 ? this.reduce(choices) : choices; + Set smartChoices = this.reduce(choices, daedalus); return this.pick(smartChoices); } 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 @@ +package ch.epfl.maze.physical.stragegies.planner; + +import ch.epfl.maze.util.Direction; +import ch.epfl.maze.util.Vector2D; + +/** + * @author Pacien TRAN-GIRARD + */ +public interface DistanceCalculator { + + /** + * Returns the current position. + * + * @return The current position vector + */ + Vector2D getPosition(); + + /** + * Calculates the Euclidean distance from the adjacent position at the given Direction to the target position. + * + * @param dir The adjacent Direction + * @param targetPosition The targeted position + * @return The Euclidean distance between the two positions + */ + default double getDistanceTo(Direction dir, Vector2D targetPosition) { + return this + .getPosition() + .addDirectionTo(dir) + .sub(targetPosition) + .dist(); + } + +} -- cgit v1.2.3