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/Animal.java | 4 ++-- src/ch/epfl/maze/physical/World.java | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'src/ch') 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 { * the ones available from its position. * * @param choices The choices left to the animal at its current position (see - * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) - * World.getChoices(Vector2D)}) + * {@link ch.epfl.maze.physical.World#getChoiceSet(Vector2D) + * World.getChoiceSet(Vector2D)}) * @return The next direction of the animal, chosen in {@code choices} * @implNote Not abstract for compatibility purpose (in order not to break tests) */ 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