diff options
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo/Panda.java')
-rw-r--r-- | src/ch/epfl/maze/physical/zoo/Panda.java | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Panda.java b/src/ch/epfl/maze/physical/zoo/Panda.java index ce38fda..f4c2035 100644 --- a/src/ch/epfl/maze/physical/zoo/Panda.java +++ b/src/ch/epfl/maze/physical/zoo/Panda.java | |||
@@ -6,8 +6,8 @@ import ch.epfl.maze.util.Direction; | |||
6 | import ch.epfl.maze.util.Trail; | 6 | import ch.epfl.maze.util.Trail; |
7 | import ch.epfl.maze.util.Vector2D; | 7 | import ch.epfl.maze.util.Vector2D; |
8 | 8 | ||
9 | import java.util.ArrayList; | 9 | import java.util.EnumSet; |
10 | import java.util.Arrays; | 10 | import java.util.Set; |
11 | import java.util.stream.Collectors; | 11 | import java.util.stream.Collectors; |
12 | 12 | ||
13 | /** | 13 | /** |
@@ -33,11 +33,11 @@ public class Panda extends ProbabilisticAnimal { | |||
33 | /** | 33 | /** |
34 | * Checks if the current position is an intersection given the possible choices. | 34 | * Checks if the current position is an intersection given the possible choices. |
35 | * | 35 | * |
36 | * @param choices An array of possible Directions | 36 | * @param choices A set of possible Directions |
37 | * @return T(the current position is an intersection) | 37 | * @return T(the current position is an intersection) |
38 | */ | 38 | */ |
39 | private boolean isIntersection(Direction[] choices) { | 39 | private boolean isIntersection(Set<Direction> choices) { |
40 | return choices.length > 2; | 40 | return choices.size() > 2; |
41 | } | 41 | } |
42 | 42 | ||
43 | /** | 43 | /** |
@@ -54,12 +54,12 @@ public class Panda extends ProbabilisticAnimal { | |||
54 | /** | 54 | /** |
55 | * Checks if all Direction choices are leading to the given Marking. | 55 | * Checks if all Direction choices are leading to the given Marking. |
56 | * | 56 | * |
57 | * @param choices An array of possible Directions | 57 | * @param choices A set of possible Directions |
58 | * @param marking The Marking | 58 | * @param marking The Marking |
59 | * @return T(all choices are leading to positions with the given Marking) | 59 | * @return T(all choices are leading to positions with the given Marking) |
60 | */ | 60 | */ |
61 | private boolean allChoicesLeadingTo(Direction[] choices, Trail.Marking marking) { | 61 | private boolean allChoicesLeadingTo(Set<Direction> choices, Trail.Marking marking) { |
62 | return (new ArrayList<>(Arrays.asList(choices))) | 62 | return choices |
63 | .stream() | 63 | .stream() |
64 | .allMatch(dir -> this.getMarkingAtDirection(dir) == marking); | 64 | .allMatch(dir -> this.getMarkingAtDirection(dir) == marking); |
65 | } | 65 | } |
@@ -67,48 +67,47 @@ public class Panda extends ProbabilisticAnimal { | |||
67 | /** | 67 | /** |
68 | * Selects the Direction choices leading to the given Marking. | 68 | * Selects the Direction choices leading to the given Marking. |
69 | * | 69 | * |
70 | * @param choices An array of possible Directions | 70 | * @param choices A set of possible Directions |
71 | * @param marking The Marking | 71 | * @param marking The Marking |
72 | * @return An array of choices leading to the given Marking | 72 | * @return A set of choices leading to the given Marking |
73 | */ | 73 | */ |
74 | private Direction[] selectDirectionsWithMarking(Direction[] choices, Trail.Marking marking) { | 74 | private Set<Direction> selectDirectionsWithMarking(Set<Direction> choices, Trail.Marking marking) { |
75 | return (new ArrayList<>(Arrays.asList(choices))) | 75 | return choices |
76 | .stream() | 76 | .stream() |
77 | .filter(dir -> this.getMarkingAtDirection(dir) == marking) | 77 | .filter(dir -> this.getMarkingAtDirection(dir) == marking) |
78 | .collect(Collectors.toList()) | 78 | .collect(Collectors.toSet()); |
79 | .toArray(new Direction[0]); | ||
80 | } | 79 | } |
81 | 80 | ||
82 | /** | 81 | /** |
83 | * Selects the best choices according to the neighbours Markings. | 82 | * Selects the best choices according to the neighbours Markings. |
84 | * | 83 | * |
85 | * @param choices An array of possible Directions | 84 | * @param choices A set of possible Directions |
86 | * @return An array of smart choices | 85 | * @return A set of smart choices |
87 | */ | 86 | */ |
88 | private Direction[] selectBestDirections(Direction[] choices) { | 87 | private Set<Direction> selectBestDirections(Set<Direction> choices) { |
89 | // special case | 88 | // special case |
90 | if (this.isIntersection(choices) && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) | 89 | if (this.isIntersection(choices) && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) |
91 | return new Direction[]{this.getDirection().reverse()}; | 90 | return EnumSet.of(this.getDirection().reverse()); |
92 | 91 | ||
93 | // general case | 92 | // general case |
94 | for (Trail.Marking mark : Trail.Marking.ALL) { | 93 | for (Trail.Marking mark : Trail.Marking.ALL) { |
95 | Direction[] smartChoices = this.selectDirectionsWithMarking(choices, mark); | 94 | Set<Direction> smartChoices = this.selectDirectionsWithMarking(choices, mark); |
96 | if (smartChoices.length > 0) return smartChoices; | 95 | if (!smartChoices.isEmpty()) return smartChoices; |
97 | } | 96 | } |
98 | 97 | ||
99 | // panda is trapped :( | 98 | // panda is trapped :( |
100 | return new Direction[]{}; | 99 | return EnumSet.noneOf(Direction.class); |
101 | } | 100 | } |
102 | 101 | ||
103 | /** | 102 | /** |
104 | * Determines if the current position should be marked according to the rules of the <i>Trémeaux's Algorithm</i>, | 103 | * Determines if the current position should be marked according to the rules of the <i>Trémeaux's Algorithm</i>, |
105 | * avoiding intersections over-marking. | 104 | * avoiding intersections over-marking. |
106 | * | 105 | * |
107 | * @param choices An array of possible Directions | 106 | * @param choices A set of possible Directions |
108 | * @param choice The selected Direction | 107 | * @param choice The selected Direction |
109 | * @return T(the current position should be marked) | 108 | * @return T(the current position should be marked) |
110 | */ | 109 | */ |
111 | private boolean shouldMarkCurrentPosition(Direction[] choices, Direction choice) { | 110 | private boolean shouldMarkCurrentPosition(Set<Direction> choices, Direction choice) { |
112 | return !(this.isIntersection(choices) | 111 | return !(this.isIntersection(choices) |
113 | && this.trail.getMarking(this.getPosition()) == Trail.Marking.AVOID_MARKING | 112 | && this.trail.getMarking(this.getPosition()) == Trail.Marking.AVOID_MARKING |
114 | && this.getMarkingAtDirection(choice) == Trail.Marking.NO_MARKING); | 113 | && this.getMarkingAtDirection(choice) == Trail.Marking.NO_MARKING); |
@@ -117,10 +116,10 @@ public class Panda extends ProbabilisticAnimal { | |||
117 | /** | 116 | /** |
118 | * Marks the current position according to the rules of the <i>Trémeaux's Algorithm</i>. | 117 | * Marks the current position according to the rules of the <i>Trémeaux's Algorithm</i>. |
119 | * | 118 | * |
120 | * @param choices An array of possible Direction to take | 119 | * @param choices A set of possible Direction to take |
121 | */ | 120 | */ |
122 | private void markCurrentPosition(Direction[] choices) { | 121 | private void markCurrentPosition(Set<Direction> choices) { |
123 | if (choices.length == 1 && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) // dead end | 122 | if (choices.size() == 1 && this.allChoicesLeadingTo(choices, Trail.Marking.AVOID_MARKING)) // dead end |
124 | this.trail.markPosition(this.getPosition(), Trail.Marking.NO_GO_MARKING); | 123 | this.trail.markPosition(this.getPosition(), Trail.Marking.NO_GO_MARKING); |
125 | else | 124 | else |
126 | this.trail.markPosition(this.getPosition()); | 125 | this.trail.markPosition(this.getPosition()); |
@@ -133,8 +132,8 @@ public class Panda extends ProbabilisticAnimal { | |||
133 | * have to be handled, especially when the panda is at an intersection. | 132 | * have to be handled, especially when the panda is at an intersection. |
134 | */ | 133 | */ |
135 | @Override | 134 | @Override |
136 | public Direction move(Direction[] choices) { | 135 | public Direction move(Set<Direction> choices) { |
137 | Direction[] smartChoices = this.selectBestDirections(choices); | 136 | Set<Direction> smartChoices = this.selectBestDirections(choices); |
138 | Direction choice = super.move(smartChoices); | 137 | Direction choice = super.move(smartChoices); |
139 | 138 | ||
140 | if (this.shouldMarkCurrentPosition(choices, choice)) | 139 | if (this.shouldMarkCurrentPosition(choices, choice)) |