From e326c2759d6529048d19c2c913b2b1c264f74485 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 26 Nov 2015 09:31:01 +0100 Subject: Replace choice array by choice set --- src/ch/epfl/maze/physical/World.java | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/ch/epfl/maze/physical/World.java') 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; import ch.epfl.maze.util.Vector2D; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -129,18 +130,33 @@ public abstract class World { /** * Computes and returns the available choices for a position in the * labyrinth. The result will be typically used by {@code Animal} in - * {@link ch.epfl.maze.physical.Animal#move(Direction[]) move(Direction[])} + * {@link ch.epfl.maze.physical.Animal#move(Set) move(Set)} * * @param position A position in the maze - * @return An array of all available choices at a position + * @return A set of all available choices at a position */ - public final Direction[] getChoices(Vector2D position) { - List choices = new ArrayList<>(); + public final Set getChoiceSet(Vector2D position) { + Set choices = EnumSet.noneOf(Direction.class); + for (Direction dir : Direction.POSSIBLE_DIRECTIONS) if (this.isFree(position.addDirectionTo(dir))) choices.add(dir); - return choices.isEmpty() ? new Direction[]{Direction.NONE} : choices.toArray(new Direction[choices.size()]); + return choices.isEmpty() ? EnumSet.of(Direction.NONE) : choices; + } + + /** + * Computes and returns the available choices for a position in the + * labyrinth. The result will be typically used by {@code Animal} in + * {@link ch.epfl.maze.physical.Animal#move(Direction[]) move(Direction[])} + * + * @param position A position in the maze + * @return An array of all available choices at a position + * @deprecated Use @code{Set getChoiceSet(Vector2D position)} instead + */ + public final Direction[] getChoices(Vector2D position) { + Set choiceSet = this.getChoiceSet(position); + return choiceSet.toArray(new Direction[choiceSet.size()]); } /** -- cgit v1.2.3