From 3bac5786bf9d14854d90843cb6bc7317afffc810 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 23 Nov 2015 11:44:01 +0100 Subject: Implement Inky A.I. --- src/ch/epfl/maze/physical/pacman/Inky.java | 82 +++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 8 deletions(-) (limited to 'src/ch/epfl/maze/physical/pacman') diff --git a/src/ch/epfl/maze/physical/pacman/Inky.java b/src/ch/epfl/maze/physical/pacman/Inky.java index 146ecef..de0466d 100644 --- a/src/ch/epfl/maze/physical/pacman/Inky.java +++ b/src/ch/epfl/maze/physical/pacman/Inky.java @@ -3,35 +3,101 @@ package ch.epfl.maze.physical.pacman; import ch.epfl.maze.physical.Animal; import ch.epfl.maze.physical.Daedalus; import ch.epfl.maze.physical.GhostPredator; -import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; +import java.util.NoSuchElementException; + /** * Blue ghost from the Pac-Man game, targets the result of two times the vector * from Blinky to its target. + * + * @author Pacien TRAN-GIRARD */ - public class Inky extends GhostPredator { + private GhostPredator companion; + + /** + * Finds Inky's best friend (Blinky) in the Daedalus. + * + * @param daedalus The Daedalus + * @return The companion if found, null otherwise + */ + private static GhostPredator findCompanion(Daedalus daedalus) { + try { + return (Blinky) daedalus + .getPredators() + .stream() + .filter(pred -> pred instanceof Blinky) + .findFirst() + .get(); + } catch (NoSuchElementException e) { + return null; + } + } + /** * Constructs a Inky with a starting position. * - * @param position Starting position of Inky in the labyrinth + * @param position Starting position of Inky in the labyrinth + * @param companion Inky's accomplice */ + public Inky(Vector2D position, GhostPredator companion) { + super(position); + this.companion = companion; + } + /** + * Constructs a Inky with a starting position. + * + * @param position Starting position of Inky in the labyrinth + */ public Inky(Vector2D position) { - super(position); - // TODO + this(position, null); + } + + /** + * Returns Inky's companion. + * + * @param daedalus The Daedalus + * @return Inky's companion if present, null otherwise + */ + private GhostPredator getCompanion(Daedalus daedalus) { + if (this.companion == null) + this.companion = Inky.findCompanion(daedalus); + + return this.companion; + } + + /** + * Returns Inky's companion's position. + * + * @param daedalus The Daedalus + * @return Inky's companion's position if present, null otherwise + */ + private Vector2D getCompanionPosition(Daedalus daedalus) { + GhostPredator companion = this.getCompanion(daedalus); + if (companion == null) return new Vector2D(); + return companion.getPosition(); } + /** + * Targets beyond his friend's scope. + * + * @param daedalus The Daedalus + * @return The targeted position + */ @Override - public Direction move(Direction[] choices, Daedalus daedalus) { - // TODO - return Direction.NONE; + protected Vector2D getPreyTargetPosition(Daedalus daedalus) { + return this + .getPreyPosition(daedalus) + .mul(2) + .sub(this.getCompanionPosition(daedalus)); } @Override public Animal copy() { return new Inky(this.getPosition()); } + } -- cgit v1.2.3