summaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 15:23:36 +0100
committerPacien TRAN-GIRARD2015-11-24 15:37:19 +0100
commit33e497e3083c8446588244c1fa9e69f70af05664 (patch)
tree3ef665e3c9478f196bb6d61dd15a71c0a9919216 /src/ch
parent6f2f503bbf75901273376c8c009a6b96886650c8 (diff)
downloadmaze-solver-33e497e3083c8446588244c1fa9e69f70af05664.tar.gz
Refactor Mouse strategy
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java20
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java44
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java27
-rw-r--r--src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java22
-rw-r--r--src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java20
-rw-r--r--src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java34
-rw-r--r--src/ch/epfl/maze/physical/zoo/Mouse.java18
7 files changed, 183 insertions, 2 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java
new file mode 100644
index 0000000..6dcf229
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java
@@ -0,0 +1,20 @@
1package ch.epfl.maze.physical.stragegies.picker;
2
3import ch.epfl.maze.physical.World;
4import ch.epfl.maze.util.Direction;
5
6import java.util.Set;
7
8/**
9 * A decision maker unaware of its distant environment.
10 *
11 * @author Pacien TRAN-GIRARD
12 */
13public interface BlindPicker extends ChoicePicker {
14
15 @Override
16 default Direction pick(Set<Direction> choices, World world) {
17 return this.pick(choices);
18 }
19
20}
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java
new file mode 100644
index 0000000..76fe3eb
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java
@@ -0,0 +1,44 @@
1package ch.epfl.maze.physical.stragegies.picker;
2
3import ch.epfl.maze.physical.World;
4import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D;
6
7import java.util.Set;
8
9/**
10 * A decision maker that can pick a Direction given multiple choices.
11 *
12 * @author Pacien TRAN-GIRARD
13 */
14public interface ChoicePicker {
15
16 Direction FALLBACK_DIRECTION = Direction.NONE;
17
18 /**
19 * Retrieves the next direction of the animal, by selecting one choice among
20 * the ones available from its position.
21 *
22 * @param choices The choices left to the animal at its current position (see
23 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D)
24 * World.getChoices(Vector2D)})
25 * @return The next direction of the animal, chosen in {@code choices}
26 */
27 Direction pick(Set<Direction> choices);
28
29 /**
30 * Retrieves the next direction of the animal, by selecting one choice among
31 * the ones available from its position.
32 * <p>
33 * In this variation, the animal knows the world entirely. It can therefore
34 * use the position of other animals in the daedalus to move more effectively.
35 *
36 * @param choices The choices left to the animal at its current position (see
37 * {@link ch.epfl.maze.physical.World#getChoices(Vector2D)
38 * World.getChoices(Vector2D)})
39 * @param world The world in which the animal moves
40 * @return The next direction of the animal, chosen in {@code choices}
41 */
42 Direction pick(Set<Direction> choices, World world);
43
44}
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java
new file mode 100644
index 0000000..427c0eb
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java
@@ -0,0 +1,27 @@
1package ch.epfl.maze.physical.stragegies.picker;
2
3import ch.epfl.maze.util.Direction;
4
5import java.util.ArrayList;
6import java.util.List;
7import java.util.Random;
8import java.util.Set;
9
10/**
11 * A probabilistic decision maker.
12 *
13 * @author Pacien TRAN-GIRARD
14 */
15public interface RandomPicker extends BlindPicker {
16
17 Random RANDOM_SOURCE = new Random();
18
19 @Override
20 default Direction pick(Set<Direction> choices) {
21 if (choices.isEmpty()) return FALLBACK_DIRECTION;
22
23 List<Direction> choiceList = new ArrayList<>(choices);
24 return choiceList.get(RANDOM_SOURCE.nextInt(choices.size()));
25 }
26
27}
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java
new file mode 100644
index 0000000..4ef2ff8
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java
@@ -0,0 +1,22 @@
1package ch.epfl.maze.physical.stragegies.reducer;
2
3import ch.epfl.maze.util.Direction;
4
5import java.util.Set;
6
7/**
8 * A filter removing the possibility to go backward.
9 *
10 * @author Pacien TRAN-GIRARD
11 */
12public interface BackwardReducer extends BlindChoiceReducer {
13
14 Direction getDirection();
15
16 @Override
17 default Set<Direction> reduce(Set<Direction> choices) {
18 choices.remove(this.getDirection().reverse());
19 return choices;
20 }
21
22}
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java
new file mode 100644
index 0000000..169a8f6
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java
@@ -0,0 +1,20 @@
1package ch.epfl.maze.physical.stragegies.reducer;
2
3import ch.epfl.maze.physical.World;
4import ch.epfl.maze.util.Direction;
5
6import java.util.Set;
7
8/**
9 * A decision filter unaware of its distant environment.
10 *
11 * @author Pacien TRAN-GIRARD
12 */
13public interface BlindChoiceReducer extends ChoiceReducer {
14
15 @Override
16 default Set<Direction> reduce(Set<Direction> choices, World world) {
17 return this.reduce(choices);
18 }
19
20}
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java
new file mode 100644
index 0000000..3cb744d
--- /dev/null
+++ b/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java
@@ -0,0 +1,34 @@
1package ch.epfl.maze.physical.stragegies.reducer;
2
3import ch.epfl.maze.physical.World;
4import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D;
6
7import java.util.Set;
8
9/**
10 * A choice filter that can exclude choices.
11 *
12 * @author Pacien TRAN-GIRARD
13 */
14public interface ChoiceReducer {
15
16 /**
17 * Reduces the Direction choices by eliminating the improper ones from the given choices.
18 *
19 * @param choices The choices left to the animal
20 * @return A subset of possible direction of the animal, chosen in {@code choices}
21 */
22 Set<Direction> reduce(Set<Direction> choices);
23
24 /**
25 * Reduces the Direction choices by eliminating the improper ones from the given choices.
26 * In this variation, the animal knows the world entirely. It can therefore
27 * use the position of other animals in the world to move more effectively.
28 *
29 * @param choices The choices left to the animal
30 * @return A subset of possible direction of the animal, chosen in {@code choices}
31 */
32 Set<Direction> reduce(Set<Direction> choices, World world);
33
34}
diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java
index f7084d7..8f68af6 100644
--- a/src/ch/epfl/maze/physical/zoo/Mouse.java
+++ b/src/ch/epfl/maze/physical/zoo/Mouse.java
@@ -1,16 +1,20 @@
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.util.Direction;
5import ch.epfl.maze.util.Vector2D; 7import ch.epfl.maze.util.Vector2D;
6 8
9import java.util.Set;
10
7/** 11/**
8 * Mouse A.I. that remembers only the previous choice it has made. 12 * Mouse A.I. that remembers only the previous choice it has made.
9 * 13 *
10 * @author EPFL 14 * @author EPFL
11 * @author Pacien TRAN-GIRARD 15 * @author Pacien TRAN-GIRARD