summaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-23 11:44:01 +0100
committerPacien TRAN-GIRARD2015-11-23 11:44:01 +0100
commit3bac5786bf9d14854d90843cb6bc7317afffc810 (patch)
tree7de512fe753392b312c702a5845d98e2e2b90f9a /src/ch
parente9acb9330ad2fed4235b0270d67d1ee1ac9d2005 (diff)
downloadmaze-solver-3bac5786bf9d14854d90843cb6bc7317afffc810.tar.gz
Implement Inky A.I.
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/maze/physical/pacman/Inky.java82
1 files changed, 74 insertions, 8 deletions
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;
3import ch.epfl.maze.physical.Animal; 3import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.physical.Daedalus; 4import ch.epfl.maze.physical.Daedalus;
5import ch.epfl.maze.physical.GhostPredator; 5import ch.epfl.maze.physical.GhostPredator;
6import ch.epfl.maze.util.Direction;
7import ch.epfl.maze.util.Vector2D; 6import ch.epfl.maze.util.Vector2D;
8 7
8import java.util.NoSuchElementException;
9
9/** 10/**
10 * Blue ghost from the Pac-Man game, targets the result of two times the vector 11 * Blue ghost from the Pac-Man game, targets the result of two times the vector
11 * from Blinky to its target. 12 * from Blinky to its target.
13 *
14 * @author Pacien TRAN-GIRARD
12 */ 15 */
13
14public class Inky extends GhostPredator { 16public class Inky extends GhostPredator {
15 17
18 private GhostPredator companion;
19
20 /**
21 * Finds Inky's best friend (Blinky) in the Daedalus.
22 *
23 * @param daedalus The Daedalus
24 * @return The companion if found, null otherwise
25 */
26 private static GhostPredator findCompanion(Daedalus daedalus) {
27 try {
28 return (Blinky) daedalus
29 .getPredators()
30 .stream()
31 .filter(pred -> pred instanceof Blinky)
32 .findFirst()
33 .get();
34 } catch (NoSuchElementException e) {
35 return null;
36 }
37 }
38
16 /** 39 /**
17 * Constructs a Inky with a starting position. 40 * Constructs a Inky with a starting position.
18 * 41 *
19 * @param position Starting position of Inky in the labyrinth 42 * @param position Starting position of Inky in the labyrinth
43 * @param companion Inky's accomplice
20 */ 44 */
45 public Inky(Vector2D position, GhostPredator companion) {
46 super(position);
47 this.companion = companion;
48 }
21 49
50 /**
51 * Constructs a Inky with a starting position.
52 *
53 * @param position Starting position of Inky in the labyrinth
54 */
22 public Inky(Vector2D position) { 55 public Inky(Vector2D position) {
23 super(position); 56 this(position, null);
24 // TODO 57 }
58
59 /**
60 * Returns Inky's companion.
61 *
62 * @param daedalus The Daedalus
63 * @return Inky's companion if present, null otherwise
64 */
65 private GhostPredator getCompanion(Daedalus daedalus) {
66 if (this.companion == null)
67 this.companion = Inky.findCompanion(daedalus);
68
69 return this.companion;
70 }
71
72 /**
73 * Returns Inky's companion's position.
74 *
75 * @param daedalus The Daedalus
76 * @return Inky's companion's position if present, null otherwise
77 */
78 private Vector2D getCompanionPosition(Daedalus daedalus) {
79 GhostPredator companion = this.getCompanion(daedalus);
80 if (companion == null) return new Vector2D();
81 return companion.getPosition();
25 } 82 }
26 83
84 /**
85 * Targets beyond his friend's scope.
86 *
87 * @param daedalus The Daedalus
88 * @return The targeted position
89 */
27 @Override 90 @Override
28 public Direction move(Direction[] choices, Daedalus daedalus) { 91 protected Vector2D getPreyTargetPosition(Daedalus daedalus) {
29 // TODO 92 return this
30 return Direction.NONE; 93 .getPreyPosition(daedalus)
94 .mul(2)
95 .sub(this.getCompanionPosition(daedalus));
31 } 96 }
32 97
33 @Override 98 @Override
34 public Animal copy() { 99 public Animal copy() {
35 return new Inky(this.getPosition()); 100 return new Inky(this.getPosition());
36 } 101 }
102
37} 103}