summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/ProbabilisticAnimal.java')
-rw-r--r--src/ch/epfl/maze/physical/ProbabilisticAnimal.java33
1 files changed, 17 insertions, 16 deletions
diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
index 650cecf..e461e8c 100644
--- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
+++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
@@ -4,8 +4,9 @@ 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.Arrays; 7import java.util.List;
8import java.util.Random; 8import java.util.Random;
9import java.util.Set;
9import java.util.stream.Collectors; 10import java.util.stream.Collectors;
10 11
11/** 12/**
@@ -29,36 +30,36 @@ abstract public class ProbabilisticAnimal extends Animal {
29 /** 30 /**
30 * Excludes the given direction from possible choices. 31 * Excludes the given direction from possible choices.
31 * 32 *
32 * @param choices An array of choices 33 * @param choices A set of choices
33 * @param toExclude The Direction to exclude 34 * @param toExclude The Direction to exclude
34 * @return An array of smart choices 35 * @return A set of smart choices
35 */ 36 */
36 protected Direction[] excludeDirection(Direction[] choices, Direction toExclude) { 37 protected Set<Direction> excludeDirection(Set<Direction> choices, Direction toExclude) {
37 return (new ArrayList<>(Arrays.asList(choices))) 38 return choices
38 .stream() 39 .stream()
39 .filter(dir -> dir != toExclude) 40 .filter(dir -> dir != toExclude)
40 .collect(Collectors.toList()) 41 .collect(Collectors.toSet());
41 .toArray(new Direction[0]);
42 } 42 }
43 43
44 /** 44 /**
45 * Excludes the origin direction from possible choices. 45 * Excludes the origin direction from possible choices.
46 * 46 *
47 * @param choices An array of choices 47 * @param choices A set of choices
48 * @return An array of smart choices 48 * @return A set of smart choices
49 */ 49 */
50 protected Direction[] excludeOrigin(Direction[] choices) { 50 protected Set<Direction> excludeOrigin(Set<Direction> choices) {
51 return this.excludeDirection(choices, this.getDirection().reverse()); 51 return this.excludeDirection(choices, this.getDirection().reverse());
52 } 52 }
53 53
54 /** 54 /**
55 * Returns a random Direction from the given choices. 55 * Returns a random Direction from the given choices.
56 * 56 *
57 * @param choices An array of Direction 57 * @param choices A set of Direction
58 * @return A random Direction taken from the given choices 58 * @return A random Direction taken from the given choices
59 */ 59 */
60 protected Direction getRandomDirection(Direction[] choices) { 60 protected Direction getRandomDirection(Set<Direction> choices) {
61 return choices[RANDOM_SOURCE.nextInt(choices.length)]; 61 List<Direction> choiceList = new ArrayList<>(choices);
62 return choiceList.get(RANDOM_SOURCE.nextInt(choices.size()));
62 } 63 }
63 64
64 /** 65 /**
@@ -66,10 +67,10 @@ abstract public class ProbabilisticAnimal extends Animal {
66 * probabilistic animal does not directly retrace its steps if not forced. 67 * probabilistic animal does not directly retrace its steps if not forced.
67 */ 68 */
68 @Override 69 @Override
69 public Direction move(Direction[] choices) { 70 public Direction move(Set<Direction> choices) {
70 if (choices.length == 0) return Direction.NONE; 71 if (choices.isEmpty()) return Direction.NONE;
71 72
72 Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; 73 Set<Direction> smartChoices = choices.size() > 1 ? this.excludeOrigin(choices) : choices;
73 return this.getRandomDirection(smartChoices); 74 return this.getRandomDirection(smartChoices);
74 } 75 }
75 76