summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo/Hamster.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo/Hamster.java')
-rw-r--r--src/ch/epfl/maze/physical/zoo/Hamster.java19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java
index 150880c..527e8ce 100644
--- a/src/ch/epfl/maze/physical/zoo/Hamster.java
+++ b/src/ch/epfl/maze/physical/zoo/Hamster.java
@@ -6,8 +6,8 @@ import ch.epfl.maze.util.Direction;
6import ch.epfl.maze.util.Vector2D; 6import ch.epfl.maze.util.Vector2D;
7 7
8import java.util.ArrayList; 8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List; 9import java.util.List;
10import java.util.Set;
11import java.util.stream.Collectors; 11import java.util.stream.Collectors;
12 12
13/** 13/**
@@ -34,15 +34,14 @@ public class Hamster extends ProbabilisticAnimal {
34 /** 34 /**
35 * Discard directions known to lead to dead ends. 35 * Discard directions known to lead to dead ends.
36 * 36 *
37 * @param choices An array of choices 37 * @param choices A set of choices
38 * @return An array of smart choices 38 * @return A set of smart choices
39 */ 39 */
40 private Direction[] excludeDeadPaths(Direction[] choices) { 40 private Set<Direction> excludeDeadPaths(Set<Direction> choices) {
41 return (new ArrayList<>(Arrays.asList(choices))) 41 return choices
42 .stream() 42 .stream()
43 .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) 43 .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir)))
44 .collect(Collectors.toList()) 44 .collect(Collectors.toSet());
45 .toArray(new Direction[0]);
46 } 45 }
47 46
48 /** 47 /**
@@ -50,9 +49,9 @@ public class Hamster extends ProbabilisticAnimal {
50 * it learns during its journey. 49 * it learns during its journey.
51 */ 50 */
52 @Override 51 @Override
53 public Direction move(Direction[] choices) { 52 public Direction move(Set<Direction> choices) {
54 Direction[] smartChoices = this.excludeDeadPaths(choices); 53 Set<Direction> smartChoices = this.excludeDeadPaths(choices);
55 if (smartChoices.length == 1) this.deadPaths.add(this.getPosition()); // dead end 54 if (smartChoices.size() == 1) this.deadPaths.add(this.getPosition()); // dead end
56 return super.move(smartChoices); 55 return super.move(smartChoices);
57 } 56 }
58 57