diff options
author | Pacien TRAN-GIRARD | 2015-11-26 10:00:43 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-26 10:00:43 +0100 |
commit | 1e2eff1e9829f8ffa694f3258433c91c804ab473 (patch) | |
tree | 0b0b8b7d7c07cf8ab3b26a1316d54199c18fd5b6 | |
parent | e326c2759d6529048d19c2c913b2b1c264f74485 (diff) | |
download | maze-solver-1e2eff1e9829f8ffa694f3258433c91c804ab473.tar.gz |
Improve choice filtering
-rw-r--r-- | src/ch/epfl/maze/physical/World.java | 12 | ||||
-rw-r--r-- | src/ch/epfl/maze/util/Direction.java | 10 |
2 files changed, 13 insertions, 9 deletions
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; | |||
7 | import java.util.EnumSet; | 7 | import java.util.EnumSet; |
8 | import java.util.List; | 8 | import java.util.List; |
9 | import java.util.Set; | 9 | import java.util.Set; |
10 | import java.util.stream.Collectors; | ||
10 | 11 | ||
11 | /** | 12 | /** |
12 | * World that is represented by a labyrinth of tiles in which an {@code Animal} | 13 | * World that is represented by a labyrinth of tiles in which an {@code Animal} |
@@ -136,13 +137,12 @@ public abstract class World { | |||
136 | * @return A set of all available choices at a position | 137 | * @return A set of all available choices at a position |
137 | */ | 138 | */ |
138 | public final Set<Direction> getChoiceSet(Vector2D position) { | 139 | public final Set<Direction> getChoiceSet(Vector2D position) { |
139 | Set<Direction> choices = EnumSet.noneOf(Direction.class); | 140 | Set<Direction> freeDirections = Direction.MOVING_DIRECTIONS |
141 | .stream() | ||
142 | .filter(dir -> this.isFree(position.addDirectionTo(dir))) | ||
143 | .collect(Collectors.toSet()); | ||
140 | 144 | ||
141 | for (Direction dir : Direction.POSSIBLE_DIRECTIONS) | 145 | return freeDirections.isEmpty() ? EnumSet.of(Direction.NONE) : freeDirections; |
142 | if (this.isFree(position.addDirectionTo(dir))) | ||
143 | choices.add(dir); | ||
144 | |||
145 | return choices.isEmpty() ? EnumSet.of(Direction.NONE) : choices; | ||
146 | } | 146 | } |
147 | 147 | ||
148 | /** | 148 | /** |
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 @@ | |||
1 | package ch.epfl.maze.util; | 1 | package ch.epfl.maze.util; |
2 | 2 | ||
3 | import java.util.Collections; | ||
4 | import java.util.EnumSet; | ||
5 | import java.util.Set; | ||
6 | |||
3 | /** | 7 | /** |
4 | * Directions that an animal can take to move. They represent the four cardinal | 8 | * Directions that an animal can take to move. They represent the four cardinal |
5 | * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the | 9 | * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the |
@@ -12,14 +16,14 @@ public enum Direction { | |||
12 | DOWN, UP, RIGHT, LEFT, NONE; | 16 | DOWN, UP, RIGHT, LEFT, NONE; |
13 | 17 | ||
14 | /** | 18 | /** |
15 | * An array of all the possible directions that can be taken. | 19 | * A set of all the possible directions that can be taken. |
16 | */ | 20 | */ |
17 | public static final Direction[] POSSIBLE_DIRECTIONS = new Direction[]{ | 21 | public static final Set<Direction> MOVING_DIRECTIONS = Collections.unmodifiableSet(EnumSet.of( |
18 | Direction.DOWN, | 22 | Direction.DOWN, |
19 | Direction.UP, | 23 | Direction.UP, |
20 | Direction.RIGHT, | 24 | Direction.RIGHT, |
21 | Direction.LEFT | 25 | Direction.LEFT |
22 | }; | 26 | )); |
23 | 27 | ||
24 | /** | 28 | /** |
25 | * Returns the integer value of the direction | 29 | * Returns the integer value of the direction |