summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-21 22:53:24 +0100
committerPacien TRAN-GIRARD2015-11-21 22:53:24 +0100
commit10627af7a85b50bce965245a472d9b69e928d0ba (patch)
tree13612620b7b1cbee85653cee436abd9feaf36273 /src
parent78f0dd183821ef471c430ddb6cb0444b28eb1367 (diff)
downloadmaze-solver-10627af7a85b50bce965245a472d9b69e928d0ba.tar.gz
Generalize turn back avoidance to all ProbabilisticAnimals
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/maze/physical/ProbabilisticAnimal.java38
-rw-r--r--src/ch/epfl/maze/physical/zoo/Mouse.java39
2 files changed, 35 insertions, 42 deletions
diff --git a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
index f62fea1..c4d4f2a 100644
--- a/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
+++ b/src/ch/epfl/maze/physical/ProbabilisticAnimal.java
@@ -3,37 +3,67 @@ package ch.epfl.maze.physical;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.ArrayList;
7import java.util.Arrays;
6import java.util.Random; 8import java.util.Random;
9import java.util.stream.Collectors;
7 10
8/** 11/**
9 * A probabilistic animal that use a random component in its decision making process. 12 * A probabilistic animal that use a random component in its decision making process.
10 * 13 *
11 * @author Pacien TRAN-GIRARD 14 * @author Pacien TRAN-GIRARD
12 */ 15 */
13
14abstract public class ProbabilisticAnimal extends Animal { 16abstract public class ProbabilisticAnimal extends Animal {
15 17
16 private final Random randomSource; 18 private final Random randomSource;
19 private Direction currentDirection;
17 20
18 /** 21 /**
19 * Constructs a probabilistic animal with a starting position 22 * Constructs a probabilistic animal with a starting position
20 * 23 *
21 * @param position Starting position of the probabilistic animal in the labyrinth 24 * @param position Starting position of the probabilistic animal in the labyrinth
22 */ 25 */
23
24 public ProbabilisticAnimal(Vector2D position) { 26 public ProbabilisticAnimal(Vector2D position) {
25 super(position); // no pun intended 27 super(position); // no pun intended
26 this.randomSource = new Random(); 28 this.randomSource = new Random();
29 this.currentDirection = Direction.NONE;
30 }
31
32 /**
33 * Excludes the origin direction for choices.
34 *
35 * @param choices An array of choices
36 * @return An array of smart choices
37 */
38 protected Direction[] excludeOrigin(Direction[] choices) {
39 return (new ArrayList<>(Arrays.asList(choices)))
40 .stream()
41 .filter(dir -> !dir.isOpposite(this.currentDirection))
42 .collect(Collectors.toList())
43 .toArray(new Direction[0]);
27 } 44 }
28 45
29 /** 46 /**
30 * Moves according to a <i>random walk</i>. 47 * Returns a random Direction from the given choices.
48 *
49 * @param choices An array of Direction
50 * @return A random Direction taken from the given choices
31 */ 51 */
52 protected Direction getRandomDirection(Direction[] choices) {
53 return choices[randomSource.nextInt(choices.length)];
54 }
32 55
56 /**
57 * Moves according to an improved version of a <i>random walk</i> : the
58 * probabilistic animal does not directly retrace its steps if not forced.
59 */
33 @Override 60 @Override
34 public Direction move(Direction[] choices) { 61 public Direction move(Direction[] choices) {
35 if (choices.length == 0) return Direction.NONE; 62 if (choices.length == 0) return Direction.NONE;
36 return choices[randomSource.nextInt(choices.length)]; 63
64 Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices;
65 this.currentDirection = this.getRandomDirection(smartChoices);
66 return this.currentDirection;
37 } 67 }
38 68
39} 69}
diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java
index b5483c1..08d7f3d 100644
--- a/src/ch/epfl/maze/physical/zoo/Mouse.java
+++ b/src/ch/epfl/maze/physical/zoo/Mouse.java
@@ -2,64 +2,27 @@ package 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.ProbabilisticAnimal;
5import ch.epfl.maze.util.Direction;
6import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
7 6
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.stream.Collectors;
11
12/** 7/**
13 * Mouse A.I. that remembers only the previous choice it has made. 8 * Mouse A.I. that remembers only the previous choice it has made.
14 * 9 *
15 * @author Pacien TRAN-GIRARD 10 * @author Pacien TRAN-GIRARD
16 */ 11 */
17
18public class Mouse extends ProbabilisticAnimal { 12public class Mouse extends ProbabilisticAnimal {
19 13
20 private Direction previousChoice;
21
22 /** 14 /**
23 * Constructs a mouse with a starting position. 15 * Constructs a mouse with a starting position.
24 * 16 *
25 * @param position Starting position of the mouse in the labyrinth 17 * @param position Starting position of the mouse in the labyrinth
26 */ 18 */
27
28 public Mouse(Vector2D position) { 19 public Mouse(Vector2D position) {
29 super(position); 20 super(position);
30 this.previousChoice = Direction.NONE;
31 }
32
33 /**
34 * Excludes the origin direction for choices.
35 *
36 * @param choices An array of choices
37 * @return An array of smart choices
38 */
39
40 private Direction[] excludeOrigin(Direction[] choices) {
41 return (new ArrayList<>(Arrays.asList(choices)))
42 .stream()
43 .filter(dir -> !dir.isOpposite(this.previousChoice))
44 .collect(Collectors.toList())
45 .toArray(new Direction[0]);
46 }
47
48 /**
49 * Moves according to an improved version of a <i>random walk</i> : the
50 * mouse does not directly retrace its steps.
51 */
52
53 @Override
54 public Direction move(Direction[] choices) {
55 Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices;
56 Direction dir = super.move(smartChoices);
57 this.previousChoice = dir;
58 return dir;
59 } 21 }
60 22
61 @Override 23 @Override
62 public Animal copy() { 24 public Animal copy() {
63 return new Mouse(this.getPosition()); 25 return new Mouse(this.getPosition());
64 } 26 }
27
65} 28}