summaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-26 09:31:01 +0100
committerPacien TRAN-GIRARD2015-11-26 09:40:17 +0100
commite326c2759d6529048d19c2c913b2b1c264f74485 (patch)
treeca23531b04b5bbd3a272d7e69a26cf0baedd5957 /src/ch
parent43c5d1cb303955c1ac2fa5ac86449e87916a0257 (diff)
downloadmaze-solver-e326c2759d6529048d19c2c913b2b1c264f74485.tar.gz
Replace choice array by choice set
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/maze/physical/Animal.java4
-rw-r--r--src/ch/epfl/maze/physical/World.java26
2 files changed, 23 insertions, 7 deletions
diff --git a/src/ch/epfl/maze/physical/Animal.java b/src/ch/epfl/maze/physical/Animal.java
index e15cb74..9d89e8b 100644
--- a/src/ch/epfl/maze/physical/Animal.java
+++ b/src/ch/epfl/maze/physical/Animal.java
@@ -44,8 +44,8 @@ abstract public class Animal {
44 * the ones available from its position. 44 * the ones available from its position.
45 * 45 *
46 * @param choices The choices left to the animal at its current position (see 46 * @param choices The choices left to the animal at its current position (see
47 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) 47 * {@link ch.epfl.maze.physical.World#getChoiceSet(Vector2D)
48 * World.getChoices(Vector2D)}) 48 * World.getChoiceSet(Vector2D)})
49 * @return The next direction of the animal, chosen in {@code choices} 49 * @return The next direction of the animal, chosen in {@code choices}
50 * @implNote Not abstract for compatibility purpose (in order not to break tests) 50 * @implNote Not abstract for compatibility purpose (in order not to break tests)
51 */ 51 */
diff --git a/src/ch/epfl/maze/physical/World.java b/src/ch/epfl/maze/physical/World.java
index 9730876..257fa65 100644
--- a/src/ch/epfl/maze/physical/World.java
+++ b/src/ch/epfl/maze/physical/World.java
@@ -4,6 +4,7 @@ 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.EnumSet;
7import java.util.List; 8import java.util.List;
8import java.util.Set; 9import java.util.Set;
9 10
@@ -129,18 +130,33 @@ public abstract class World {
129 /** 130 /**
130 * Computes and returns the available choices for a position in the 131 * Computes and returns the available choices for a position in the
131 * labyrinth. The result will be typically used by {@code Animal} in 132 * labyrinth. The result will be typically used by {@code Animal} in
132 * {@link ch.epfl.maze.physical.Animal#move(Direction[]) move(Direction[])} 133 * {@link ch.epfl.maze.physical.Animal#move(Set) move(Set<Direction>)}
133 * 134 *
134 * @param position A position in the maze 135 * @param position A position in the maze
135 * @return An array of all available choices at a position 136 * @return A set of all available choices at a position
136 */ 137 */
137 public final Direction[] getChoices(Vector2D position) { 138 public final Set<Direction> getChoiceSet(Vector2D position) {
138 List<Direction> choices = new ArrayList<>(); 139 Set<Direction> choices = EnumSet.noneOf(Direction.class);
140
139 for (Direction dir : Direction.POSSIBLE_DIRECTIONS) 141 for (Direction dir : Direction.POSSIBLE_DIRECTIONS)
140 if (this.isFree(position.addDirectionTo(dir))) 142 if (this.isFree(position.addDirectionTo(dir)))
141 choices.add(dir); 143 choices.add(dir);
142 144
143 return choices.isEmpty() ? new Direction[]{Direction.NONE} : choices.toArray(new Direction[choices.size()]); 145 return choices.isEmpty() ? EnumSet.of(Direction.NONE) : choices;
146 }
147
148 /**
149 * Computes and returns the available choices for a position in the
150 * labyrinth. The result will be typically used by {@code Animal} in
151 * {@link ch.epfl.maze.physical.Animal#move(Direction[]) move(Direction[])}
152 *
153 * @param position A position in the maze
154 * @return An array of all available choices at a position
155 * @deprecated Use @code{Set<Direction> getChoiceSet(Vector2D position)} instead
156 */
157 public final Direction[] getChoices(Vector2D position) {
158 Set<Direction> choiceSet = this.getChoiceSet(position);
159 return choiceSet.toArray(new Direction[choiceSet.size()]);
144 } 160 }
145 161
146 /** 162 /**