summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo
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/ch/epfl/maze/physical/zoo
parent78f0dd183821ef471c430ddb6cb0444b28eb1367 (diff)
downloadmaze-solver-10627af7a85b50bce965245a472d9b69e928d0ba.tar.gz
Generalize turn back avoidance to all ProbabilisticAnimals
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo')
-rw-r--r--src/ch/epfl/maze/physical/zoo/Mouse.java39
1 files changed, 1 insertions, 38 deletions
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}