summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 14:28:26 +0100
committerPacien TRAN-GIRARD2015-11-24 14:28:26 +0100
commite2354d82e09c3bf8ae472d174332670d2d12f9bb (patch)
tree92528b9231178fd47c6910163360413d4ae77256
parent5f9be99e7ebd4a90e666e6efbe9e9d4b3b8008e7 (diff)
downloadmaze-solver-e2354d82e09c3bf8ae472d174332670d2d12f9bb.tar.gz
Use Set instead of array for Direction choices
-rw-r--r--src/ch/epfl/maze/physical/Animal.java19
-rw-r--r--src/ch/epfl/maze/physical/GhostPredator.java20
-rw-r--r--src/ch/epfl/maze/physical/Predator.java24
-rw-r--r--src/ch/epfl/maze/physical/Prey.java24
-rw-r--r--src/ch/epfl/maze/physical/ProbabilisticAnimal.java33
-rw-r--r--src/ch/epfl/maze/physical/pacman/PacMan.java4
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java19
-rw-r--r--src/ch/epfl/maze/physical/zoo/Hamster.java19
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java23
-rw-r--r--src/ch/epfl/maze/physical/zoo/Panda.java55
-rw-r--r--src/ch/epfl/maze/physical/zoo/SpaceInvader.java6
-rw-r--r--src/ch/epfl/maze/tests/AnimalTest.java6
-rw-r--r--src/ch/epfl/maze/tests/GhostsTest.java4
13 files changed, 158 insertions, 98 deletions
diff --git a/src/ch/epfl/maze/physical/Animal.java b/src/ch/epfl/maze/physical/Animal.java
index 34a1136..04c4baf 100644
--- a/src/ch/epfl/maze/physical/Animal.java
+++ b/src/ch/epfl/maze/physical/Animal.java
@@ -3,6 +3,10 @@ package ch.epfl.maze.physical;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.Arrays;
7import java.util.EnumSet;
8import java.util.Set;
9
6/** 10/**
7 * Animal inside a {@code World} that can move depending on the available 11 * Animal inside a {@code World} that can move depending on the available
8 * choices it has at its position. 12 * choices it has at its position.
@@ -44,7 +48,20 @@ abstract public class Animal {
44 * World.getChoices(Vector2D)}) 48 * World.getChoices(Vector2D)})
45 * @return The next direction of the animal, chosen in {@code choices} 49 * @return The next direction of the animal, chosen in {@code choices}
46 */ 50 */
47 abstract public Direction move(Direction[] choices); 51 abstract public Direction move(Set<Direction> choices);
52
53 /**
54 * Retrieves the next direction of the animal, by selecting one choice among
55 * the ones available from its position.
56 *
57 * @param choices The choices left to the animal at its current position (see
58 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D)
59 * World.getChoices(Vector2D)})
60 * @return The next direction of the animal, chosen in {@code choices}
61 */
62 public final Direction move(Direction[] choices) {
63 return this.move(EnumSet.copyOf(Arrays.asList(choices)));
64 }
48 65
49 /** 66 /**
50 * Updates the animal position with a direction. 67 * Updates the animal position with a direction.
diff --git a/src/ch/epfl/maze/physical/GhostPredator.java b/src/ch/epfl/maze/physical/GhostPredator.java
index e4a64b3..c1cfca8 100644
--- a/src/ch/epfl/maze/physical/GhostPredator.java
+++ b/src/ch/epfl/maze/physical/GhostPredator.java
@@ -3,8 +3,8 @@ package ch.epfl.maze.physical;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.ArrayList; 6import java.util.EnumSet;
7import java.util.List; 7import java.util.Set;
8 8
9/** 9/**
10 * Predator ghost that have two different modes and a home position in the labyrinth. 10 * Predator ghost that have two different modes and a home position in the labyrinth.
@@ -138,11 +138,11 @@ abstract public class GhostPredator extends Predator {
138 * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position. 138 * Selects the best Direction in the given choices by minimizing the Euclidean distance to the targeted position.
139 * 139 *
140 * @param targetPosition The targeted position 140 * @param targetPosition The targeted position
141 * @param choices An array of Direction choices 141 * @param choices A set of Direction choices
142 * @return An array of optimal Direction choices 142 * @return A set of optimal Direction choices
143 */ 143 */
144 private Direction[] selectBestPaths(Vector2D targetPosition, Direction[] choices) { 144 private Set<Direction> selectBestPaths(Vector2D targetPosition, Set<Direction> choices) {
145 List<Direction> bestPaths = new ArrayList<>(); 145 Set<Direction> bestPaths = EnumSet.noneOf(Direction.class);
146 double minDist = Double.MAX_VALUE; 146 double minDist = Double.MAX_VALUE;
147 147
148 for (Direction dir : choices) { 148 for (Direction dir : choices) {
@@ -157,7 +157,7 @@ abstract public class GhostPredator extends Predator {
157 bestPaths.add(dir); 157 bestPaths.add(dir);
158 } 158 }
159 159
160 return bestPaths.toArray(new Direction[bestPaths.size()]); 160 return bestPaths;
161 } 161 }
162 162
163 /** 163 /**
@@ -179,11 +179,11 @@ abstract public class GhostPredator extends Predator {
179 } 179 }
180 180
181 @Override 181 @Override
182 public Direction move(Direction[] choices, Daedalus daedalus) { 182 public Direction move(Set<Direction> choices, Daedalus daedalus) {
183 this.countCycle(); 183 this.countCycle();
184 184
185 Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; 185 Set<Direction> smartChoices = choices.size() > 1 ? this.excludeOrigin(choices) : choices;
186 Direction[] bestPaths = this.selectBestPaths(this.getTargetPosition(daedalus), smartChoices); 186 Set<Direction> bestPaths = this.selectBestPaths(this.getTargetPosition(daedalus), smartChoices);
187 return this.move(bestPaths); 187 return this.move(bestPaths);
188 } 188 }
189 189
diff --git a/src/ch/epfl/maze/physical/Predator.java b/src/ch/epfl/maze/physical/Predator.java
index 0b20ca0..5f0a3bb 100644
--- a/src/ch/epfl/maze/physical/Predator.java
+++ b/src/ch/epfl/maze/physical/Predator.java
@@ -3,6 +3,10 @@ package ch.epfl.maze.physical;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.Arrays;
7import java.util.EnumSet;
8import java.util.Set;
9
6/** 10/**
7 * Predator that kills a prey when they meet with each other in the labyrinth. 11 * Predator that kills a prey when they meet with each other in the labyrinth.
8 * 12 *
@@ -34,6 +38,24 @@ abstract public class Predator extends ProbabilisticAnimal {
34 * @param daedalus The world in which the animal moves 38 * @param daedalus The world in which the animal moves
35 * @return The next direction of the animal, chosen in {@code choices} 39 * @return The next direction of the animal, chosen in {@code choices}
36 */ 40 */
37 abstract public Direction move(Direction[] choices, Daedalus daedalus); 41 abstract public Direction move(Set<Direction> choices, Daedalus daedalus);
42
43 /**
44 * Retrieves the next direction of the animal, by selecting one choice among
45 * the ones available from its position.
46 * <p>
47 * In this variation, the animal knows the world entirely. It can therefore
48 * use the position of other animals in the daedalus to hunt more
49 * effectively.
50 *
51 * @param choices The choices left to the animal at its current position (see
52 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D)
53 * World.getChoices(Vector2D)})
54 * @param daedalus The world in which the animal moves
55 * @return The next direction of the animal, chosen in {@code choices}
56 */
57 public final Direction move(Direction[] choices, Daedalus daedalus) {
58 return this.move(EnumSet.copyOf(Arrays.asList(choices)), daedalus);
59 }
38 60
39} 61}
diff --git a/src/ch/epfl/maze/physical/Prey.java b/src/ch/epfl/maze/physical/Prey.java
index 26fe92b..3654807 100644
--- a/src/ch/epfl/maze/physical/Prey.java
+++ b/src/ch/epfl/maze/physical/Prey.java
@@ -3,6 +3,10 @@ package ch.epfl.maze.physical;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.Arrays;
7import java.util.EnumSet;
8import java.util.Set;
9
6/** 10/**
7 * Prey that is killed by a predator when they meet each other in the labyrinth. 11 * Prey that is killed by a predator when they meet each other in the labyrinth.
8 * 12 *
@@ -34,6 +38,24 @@ abstract public class Prey extends ProbabilisticAnimal {
34 * @param daedalus The world in which the animal moves 38 * @param daedalus The world in which the animal moves
35 * @return The next direction of the animal, chosen in {@code choices} 39 * @return The next direction of the animal, chosen in {@code choices}
36 */ 40 */
37 abstract public Direction move(Direction[] choices, Daedalus daedalus); 41 abstract public Direction move(Set<Direction> choices, Daedalus daedalus);
42
43 /**
44 * Retrieves the next direction of the animal, by selecting one choice among
45 * the ones available from its position.
46 * <p>
47 * In this variation, the animal knows the world entirely. It can therefore
48 * use the position of other animals in the daedalus to evade predators more
49 * effectively.
50 *
51 * @param choices The choices left to the animal at its current position (see
52 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D)
53 * World.getChoices(Vector2D)})
54 * @param daedalus The world in which the animal moves
55 * @return The next direction of the animal, chosen in {@code choices}
56 */
57 public final Direction move(Direction[] choices, Daedalus daedalus) {
58 return this.move(EnumSet.copyOf(Arrays.asList(choices)), daedalus);
59 }
38 60
39} 61}
diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
index 650cecf..e461e8c 100644
--- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
+++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
@@ -4,8 +4,9 @@ import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.ArrayList; 6import java.util.ArrayList;
7import java.util.Arrays; 7import java.util.List;
8import java.util.Random;