summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/pacman
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/pacman')
-rw-r--r--src/ch/epfl/maze/physical/pacman/Blinky.java3
-rw-r--r--src/ch/epfl/maze/physical/pacman/Clyde.java3
-rw-r--r--src/ch/epfl/maze/physical/pacman/Ghost.java243
-rw-r--r--src/ch/epfl/maze/physical/pacman/Inky.java15
-rw-r--r--src/ch/epfl/maze/physical/pacman/Pinky.java3
5 files changed, 253 insertions, 14 deletions
diff --git a/src/ch/epfl/maze/physical/pacman/Blinky.java b/src/ch/epfl/maze/physical/pacman/Blinky.java
index 4e8c4a0..5f81d13 100644
--- a/src/ch/epfl/maze/physical/pacman/Blinky.java
+++ b/src/ch/epfl/maze/physical/pacman/Blinky.java
@@ -2,7 +2,6 @@ package ch.epfl.maze.physical.pacman;
2 2
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;
6import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
7 6
8/** 7/**
@@ -11,7 +10,7 @@ import ch.epfl.maze.util.Vector2D;
11 * @author EPFL 10 * @author EPFL
12 * @author Pacien TRAN-GIRARD 11 * @author Pacien TRAN-GIRARD
13 */ 12 */
14public class Blinky extends GhostPredator { 13public class Blinky extends Ghost {
15 14
16 /** 15 /**
17 * Constructs a Blinky with a starting position. 16 * Constructs a Blinky with a starting position.
diff --git a/src/ch/epfl/maze/physical/pacman/Clyde.java b/src/ch/epfl/maze/physical/pacman/Clyde.java
index 40089db..2d7e47a 100644
--- a/src/ch/epfl/maze/physical/pacman/Clyde.java
+++ b/src/ch/epfl/maze/physical/pacman/Clyde.java
@@ -2,7 +2,6 @@ package ch.epfl.maze.physical.pacman;
2 2
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;
6import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
7 6
8/** 7/**
@@ -12,7 +11,7 @@ import ch.epfl.maze.util.Vector2D;
12 * @author EPFL 11 * @author EPFL
13 * @author Pacien TRAN-GIRARD 12 * @author Pacien TRAN-GIRARD
14 */ 13 */
15public class Clyde extends GhostPredator { 14public class Clyde extends Ghost {
16 15
17 private static double PROXIMITY_THRESHOLD = 4.0d; 16 private static double PROXIMITY_THRESHOLD = 4.0d;
18 17
diff --git a/src/ch/epfl/maze/physical/pacman/Ghost.java b/src/ch/epfl/maze/physical/pacman/Ghost.java
new file mode 100644
index 0000000..f8f511e
--- /dev/null
+++ b/src/ch/epfl/maze/physical/pacman/Ghost.java
@@ -0,0 +1,243 @@
1package ch.epfl.maze.physical.pacman;
2
3import ch.epfl.maze.physical.Daedalus;
4import ch.epfl.maze.physical.Predator;
5import ch.epfl.maze.physical.Prey;
6import ch.epfl.maze.physical.stragegies.picker.RandomPicker;
7import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer;
8import ch.epfl.maze.physical.stragegies.reducer.CostReducer;
9import ch.epfl.maze.util.Direction;
10import ch.epfl.maze.util.Vector2D;
11
12import java.util.EnumSet;
13import java.util.Set;
14
15/**
16 * Predator ghost that have two different modes and a home position in the labyrinth.
17 *
18 * @author Pacien TRAN-GIRARD
19 */
20abstract public class Ghost extends Predator implements BackwardReducer, CostReducer, RandomPicker {
21
22 public enum Mode {
23 CHASE(40),
24 SCATTER(14);
25
26 public static final Mode DEFAULT = CHASE;
27 public final int duration;
28
29 /**
30 * Constructs a new Mode with the given duration.
31 *
32 * @param duration The duration in cycles
33 */
34 Mode(int duration) {
35 this.duration = duration;
36 }
37
38 /**
39 * Returns the next Mode.
40 *
41 * @return The next Mode
42 */
43 public Mode getNext() {
44 switch (this) {
45 case CHASE:
46 return SCATTER;
47 case SCATTER:
48 return CHASE;
49 default:
50 return DEFAULT;
51 }
52 }
53 }
54
55 private static Prey commonPrey;
56
57 private final Vector2D homePosition;
58
59 private Mode mode;
60 private int modeCycle;
61
62 /**
63 * Constructs a predator with a specified position.
64 *
65 * @param position Position of the predator in the labyrinth
66 */
67 public Ghost(Vector2D position) {
68 super(position);
69
70 this.homePosition = position;
71
72 this.mode = Mode.DEFAULT;
73 this.modeCycle = 0;
74 }
75
76 /**
77 * Returns the cost to reach the target by choosing the given Direction by calculating the Euclidean distance.
78 *
79 * @param choice The Direction choice
80 * @param daedalus The Daedalus
81 * @return The Euclidean distance cost
82 */
83 @Override
84 public int getChoiceCost(Direction choice, Daedalus daedalus) {
85 Vector2D target = this.getTargetPosition(daedalus);
86 return (int) (this.getDistanceTo(choice, target) * 100);
87 }
88
89 @Override
90 public Set<Direction> reduce(Set<Direction> choices) {
91 return EnumSet.noneOf(Direction.class);
92 }
93
94 /**
95 * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position
96 * after excluding the provenance if possible.
97 *
98 * @param choices A set of Direction choices
99 * @param daedalus The Daedalus
100 * @return A set of optimal Direction choices
101 */
102 @Override
103 public Set<Direction> reduce(Set<Direction> choices, Daedalus daedalus) {
104 Set<Direction> forwardChoices = choices.size() > 1 ? BackwardReducer.super.reduce(choices) : choices;
105 return CostReducer.super.reduce(forwardChoices, daedalus);
106 }
107
108 @Override
109 public Direction move(Set<Direction> choices, Daedalus daedalus) {
110 this.countCycle();
111
112 Set<Direction> bestChoices = this.reduce(choices, daedalus);
113 return this.pick(bestChoices);
114 }
115
116 /**
117 * Returns the Prey's projected targeted position.
118 *
119 * @param daedalus The Daedalus
120 * @return The projected position
121 */
122 abstract protected Vector2D getPreyTargetPosition(Daedalus daedalus);
123
124 /**
125 * Returns the current Mode.
126 *
127 * @param daedalus The Daedalus
128 * @return The current Mode
129 */
130 protected Mode getMode(Daedalus daedalus) {
131 return this.mode;
132 }
133
134 /**
135 * Returns the position to target according to the current Mode.
136 *
137 * @param daedalus The Daedalus
138 * @return The position to target
139 */
140 protected Vector2D getTargetPosition(Daedalus daedalus) {
141 switch (this.getMode(daedalus)) {
142 case CHASE:
143 return this.getPreyTargetPosition(daedalus);
144 case SCATTER:
145 return this.homePosition;
146 default:
147 return this.homePosition;
148 }
149 }
150
151 /**
152 * Selects a new random Prey to chase in the Daedalus.
153 *
154 * @param daedalus The Daedalus
155 * @return The Chosen One
156 */
157 private static Prey selectAnyPrey(Daedalus daedalus) {
158 if (daedalus.getPreySet().isEmpty()) return null;
159 return daedalus.getPreySet().stream().findAny().get();
160 }
161
162 /**
163 * Sets a random Prey as the common Pre.
164 *
165 * @param daedalus The Daedalus
166 */
167 private static synchronized void setAnyPrey(Daedalus daedalus) {
168 Ghost.commonPrey = Ghost.selectAnyPrey(daedalus);
169 }
170
171 /**
172 * Returns the commonly targeted Prey.
173 *
174 * @param daedalus The Daedalus