summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 16:18:19 +0100
committerPacien TRAN-GIRARD2015-11-24 16:18:19 +0100
commitfbbd459e2646870da9a96ce65e77bc090c2b9930 (patch)
tree2c156e920c5fccd2cd10d3a4725a303fa6f59f93
parent33e497e3083c8446588244c1fa9e69f70af05664 (diff)
downloadmaze-solver-fbbd459e2646870da9a96ce65e77bc090c2b9930.tar.gz
Refactor Hamster A.I.
-rw-r--r--src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java31
-rw-r--r--src/ch/epfl/maze/physical/zoo/Hamster.java87
2 files changed, 100 insertions, 18 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java
new file mode 100644
index 0000000..b359cc2
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java
@@ -0,0 +1,31 @@
1package ch.epfl.maze.physical.stragegies.reducer;
2
3import ch.epfl.maze.util.Direction;
4
5import java.util.Set;
6import java.util.stream.Collectors;
7
8/**
9 * A blind reducer filtering possibilities case by case.
10 *
11 * @author Pacien TRAN-GIRARD
12 */
13public interface BlindCaseReducer extends BlindChoiceReducer {
14
15 /**
16 * Checks if the given choice should be kept or excluded by the filter.
17 *
18 * @param choice A Direction
19 * @return T(The filter should keep the given choice)
20 */
21 boolean keepChoice(Direction choice);
22
23 @Override
24 default Set<Direction> reduce(Set<Direction> choices) {
25 return choices
26 .stream()
27 .filter(choice -> this.keepChoice(choice))
28 .collect(Collectors.toSet());
29 }
30
31}
diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java
index 527e8ce..8586dee 100644
--- a/src/ch/epfl/maze/physical/zoo/Hamster.java
+++ b/src/ch/epfl/maze/physical/zoo/Hamster.java
@@ -1,14 +1,14 @@
1package ch.epfl.maze.physical.zoo; 1package ch.epfl.maze.physical.zoo;
2 2
3import ch.epfl.maze.physical.Animal; 3import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.physical.ProbabilisticAnimal; 4import ch.epfl.maze.physical.stragegies.picker.RandomPicker;
5import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer;
6import ch.epfl.maze.physical.stragegies.reducer.BlindCaseReducer;
5import ch.epfl.maze.util.Direction; 7import ch.epfl.maze.util.Direction;
6import ch.epfl.maze.util.Vector2D; 8import ch.epfl.maze.util.Vector2D;
7 9
8import java.util.ArrayList; 10import java.util.HashSet;
9import java.util.List;
10import java.util.Set; 11import java.util.Set;
11import java.util.stream.Collectors;
12 12
13/** 13/**
14 * Hamster A.I. that remembers the previous choice it has made and the dead ends 14 * Hamster A.I. that remembers the previous choice it has made and the dead ends
@@ -17,9 +17,9 @@ import java.util.stream.Collectors;
17 * @author EPFL 17 * @author EPFL
18 * @author Pacien TRAN-GIRARD 18 * @author Pacien TRAN-GIRARD
19 */ 19 */
20public class Hamster extends ProbabilisticAnimal { 20public class Hamster extends Animal implements BlindCaseReducer, BackwardReducer, RandomPicker {
21 21
22 private final List<Vector2D> deadPaths; 22 private final Set<Vector2D> positionBlacklist;
23 23
24 /** 24 /**
25 * Constructs a hamster with a starting position. 25 * Constructs a hamster with a starting position.
@@ -28,20 +28,33 @@ public class Hamster extends ProbabilisticAnimal {
28 */ 28 */
29 public Hamster(Vector2D position) { 29 public Hamster(Vector2D position) {
30 super(position); 30 super(position);
31 this.deadPaths = new ArrayList<>(); 31
32 this.positionBlacklist = new HashSet<>();
33 }
34
35 /**
36 * Checks is the choice leads to a known dead end.
37 *
38 * @param choice A Direction
39 * @return T(The given choice does not lead to a blacklisted path)
40 */
41 @Override
42 public boolean keepChoice(Direction choice) {
43 Vector2D choicePosition = this.getPosition().addDirectionTo(choice);
44 return !this.isPositionBlacklisted(choicePosition);
32 } 45 }
33 46
34 /** 47 /**
35 * Discard directions known to lead to dead ends. 48 * Filters the Direction choices using the blacklist.
36 * 49 *
37 * @param choices A set of choices 50 * @param choices A set of Direction choices
38 * @return A set of smart choices 51 * @return A subset of choices
39 */ 52 */
40 private Set<Direction> excludeDeadPaths(Set<Direction> choices) { 53 @Override
41 return choices 54 public Set<Direction> reduce(Set<Direction> choices) {
42 .stream() 55 Set<Direction> knownChoices = BlindCaseReducer.super.reduce(choices);
43 .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) 56 this.updateBlacklist(knownChoices);
44 .collect(Collectors.toSet()); 57 return knownChoices.size() > 1 ? BackwardReducer.super.reduce(knownChoices) : knownChoices;
45 } 58 }
46 59
47 /** 60 /**
@@ -50,9 +63,8 @@ public class Hamster extends ProbabilisticAnimal {
50 */ 63 */
51 @Override 64 @Override
52 public Direction move(Set<Direction> choices) { 65 public Direction move(Set<Direction> choices) {
53 Set<Direction> smartChoices = this.excludeDeadPaths(choices); 66 Set<Direction> smartChoices = this.reduce(choices);
54 if (smartChoices.size() == 1) this.deadPaths.add(this.getPosition()); // dead end 67 return this.pick(smartChoices);
55 return super.move(smartChoices);
56 } 68 }
57 69
58 @Override 70 @Override
@@ -60,4 +72,43 @@ public class Hamster extends ProbabilisticAnimal {
60 return new Hamster(this.getPosition()); 72 return new Hamster(this.getPosition());
61 } 73 }
62 74
75 /**
76 * Updates the position blacklist if needed.
77 *
78 * @param choices The choices currently available
79 */
80 private void updateBlacklist(Set<Direction> choices) {
81 if (this.inDeadEnd(choices))
82 this.blacklistPosition(this.getPosition());
83 }
84
85 /**
86 * Checks if the given choices set correspond to a dead end.
87 *
88 * @param choices The choices currently available
89 * @return T(The given choices set correspond to a dead end)
90 */
91 private boolean inDeadEnd(Set<Direction> choices) {
92 return choices.size() < 2;
93 }
94
95 /**
96 * Checks if the given position is known to lead to a dead end.
97 *
98 * @param position The position to check
99 * @return T(The given position is blacklisted)
100 */
101 private boolean isPositionBlacklisted(Vector2D position) {
102 return this.positionBlacklist.contains(position);
103 }
104
105 /**
106 * Regiters the given position in the blacklist.
107 *
108 * @param position The position to blacklist
109 */
110 private void blacklistPosition(Vector2D position) {
111 this.positionBlacklist.add(position);
112 }
113
63} 114}