From 1e2eff1e9829f8ffa694f3258433c91c804ab473 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 26 Nov 2015 10:00:43 +0100 Subject: Improve choice filtering --- src/ch/epfl/maze/physical/World.java | 12 ++++++------ src/ch/epfl/maze/util/Direction.java | 10 +++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src/ch/epfl/maze') diff --git a/src/ch/epfl/maze/physical/World.java b/src/ch/epfl/maze/physical/World.java index 257fa65..9f4f155 100644 --- a/src/ch/epfl/maze/physical/World.java +++ b/src/ch/epfl/maze/physical/World.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; /** * World that is represented by a labyrinth of tiles in which an {@code Animal} @@ -136,13 +137,12 @@ public abstract class World { * @return A set of all available choices at a position */ public final Set getChoiceSet(Vector2D position) { - Set choices = EnumSet.noneOf(Direction.class); + Set freeDirections = Direction.MOVING_DIRECTIONS + .stream() + .filter(dir -> this.isFree(position.addDirectionTo(dir))) + .collect(Collectors.toSet()); - for (Direction dir : Direction.POSSIBLE_DIRECTIONS) - if (this.isFree(position.addDirectionTo(dir))) - choices.add(dir); - - return choices.isEmpty() ? EnumSet.of(Direction.NONE) : choices; + return freeDirections.isEmpty() ? EnumSet.of(Direction.NONE) : freeDirections; } /** diff --git a/src/ch/epfl/maze/util/Direction.java b/src/ch/epfl/maze/util/Direction.java index ac172cb..b3f2e9c 100644 --- a/src/ch/epfl/maze/util/Direction.java +++ b/src/ch/epfl/maze/util/Direction.java @@ -1,5 +1,9 @@ package ch.epfl.maze.util; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + /** * Directions that an animal can take to move. They represent the four cardinal * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the @@ -12,14 +16,14 @@ public enum Direction { DOWN, UP, RIGHT, LEFT, NONE; /** - * An array of all the possible directions that can be taken. + * A set of all the possible directions that can be taken. */ - public static final Direction[] POSSIBLE_DIRECTIONS = new Direction[]{ + public static final Set MOVING_DIRECTIONS = Collections.unmodifiableSet(EnumSet.of( Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT - }; + )); /** * Returns the integer value of the direction -- cgit v1.2.3