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/PacMan.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/ch/epfl/maze/physical/pacman/PacMan.java') 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); } -- cgit v1.2.3