summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/World.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/World.java')
-rw-r--r--src/ch/epfl/maze/physical/World.java26
1 files changed, 21 insertions, 5 deletions
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 /**