summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo')
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java19
-rw-r--r--src/ch/epfl/maze/physical/zoo/Hamster.java19
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java23
-rw-r--r--src/ch/epfl/maze/physical/zoo/Panda.java55
-rw-r--r--src/ch/epfl/maze/physical/zoo/SpaceInvader.java6
5 files changed, 58 insertions, 64 deletions
diff --git a/src/ch/epfl/maze/physical/zoo/Bear.java b/src/ch/epfl/maze/physical/zoo/Bear.java
index 803b574..11faba5 100644
--- a/src/ch/epfl/maze/physical/zoo/Bear.java
+++ b/src/ch/epfl/maze/physical/zoo/Bear.java
@@ -4,9 +4,7 @@ import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.util.Direction; 4import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
6 6
7import java.util.ArrayList; 7import java.util.Set;
8import java.util.Arrays;
9import java.util.List;
10 8
11/** 9/**
12 * Bear A.I. that implements the Pledge Algorithm. 10 * Bear A.I. that implements the Pledge Algorithm.
@@ -53,16 +51,15 @@ public class Bear extends Animal {
53 /** 51 /**
54 * Finds a currentDirection following the "left paw rule". 52 * Finds a currentDirection following the "left paw rule".
55 * 53 *
56 * @param choices An array of possible directions 54 * @param choices A set of possible directions
57 * @return The currentDirection to take according to the "left paw rule" 55 * @return The currentDirection to take according to the "left paw rule"
58 */ 56 */
59 private Direction followLeft(Direction[] choices) { 57 private Direction followLeft(Set<Direction> choices) {
60 if (choices.length == 0) return Direction.NONE; 58 if (choices.isEmpty()) return Direction.NONE;
61 if (choices.length == 1) return choices[0]; 59 if (choices.size() == 1) return choices.stream().findAny().get();
62 60
63 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
64 Direction dir = this.getPreferredDir(); 61 Direction dir = this.getPreferredDir();
65 while (!choiceList.contains(dir)) dir = dir.rotateRight(); 62 while (!choices.contains(dir)) dir = dir.rotateRight();
66 63
67 return dir; 64 return dir;
68 } 65 }
@@ -76,9 +73,9 @@ public class Bear extends Animal {
76 * leaves the maze. 73 * leaves the maze.
77 */ 74 */
78 @Override 75 @Override
79 public Direction move(Direction[] choices) { 76 public Direction move(Set<Direction> choices) {
80 if (this.favoriteDirection == Direction.NONE) 77 if (this.favoriteDirection == Direction.NONE)
81 this.favoriteDirection = choices[0]; 78 this.favoriteDirection = choices.stream().findAny().get();
82 79
83 Direction newDirection = this.followLeft(choices); 80 Direction newDirection = this.followLeft(choices);
84 81
diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java
index 150880c..527e8ce 100644
--- a/src/ch/epfl/maze/physical/zoo/Hamster.java
+++ b/src/ch/epfl/maze/physical/zoo/Hamster.java
@@ -6,8 +6,8 @@ import ch.epfl.maze.util.Direction;
6import ch.epfl.maze.util.Vector2D; 6import ch.epfl.maze.util.Vector2D;
7 7
8import java.util.ArrayList; 8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List; 9import java.util.List;
10import java.util.Set;
11import java.util.stream.Collectors; 11import java.util.stream.Collectors;
12 12
13/** 13/**
@@ -34,15 +34,14 @@ public class Hamster extends ProbabilisticAnimal {
34 /** 34 /**
35 * Discard directions known to lead to dead ends. 35 * Discard directions known to lead to dead ends.
36 * 36 *
37 * @param choices An array of choices 37 * @param choices A set of choices
38 * @return An array of smart choices 38 * @return A set of smart choices
39 */ 39 */
40 private Direction[] excludeDeadPaths(Direction[] choices) { 40 private Set<Direction> excludeDeadPaths(Set<Direction> choices) {
41 return (new ArrayList<>(Arrays.asList(choices))) 41 return choices
42 .stream() 42 .stream()
43 .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) 43 .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir)))
44 .collect(Collectors.toList()) 44 .collect(Collectors.toSet());
45 .toArray(new Direction[0]);
46 } 45 }
47 46
48 /** 47 /**
@@ -50,9 +49,9 @@ public class Hamster extends ProbabilisticAnimal {
50 * it learns during its journey. 49 * it learns during its journey.
51 */ 50 */
52 @Override 51 @Override
53 public Direction move(Direction[] choices) { 52 public Direction move(Set<Direction> choices) {
54 Direction[] smartChoices = this.excludeDeadPaths(choices); 53 Set<Direction> smartChoices = this.excludeDeadPaths(choices);
55 if (smartChoices.length == 1) this.deadPaths.add(this.getPosition()); // dead end 54 if (smartChoices.size() == 1) this.deadPaths.add(this.getPosition()); // dead end
56 return super.move(smartChoices); 55 return super.move(smartChoices);
57 } 56 }
58 57
diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java
index 119781c..23eec4e 100644
--- a/src/ch/epfl/maze/physical/zoo/Monkey.java
+++ b/src/ch/epfl/maze/physical/zoo/Monkey.java
@@ -4,9 +4,7 @@ import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.util.Direction; 4import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D; 5import ch.epfl.maze.util.Vector2D;
6 6
7import java.util.ArrayList; 7import java.util.Set;
8import java.util.Arrays;
9import java.util.List;
10 8
11/** 9/**
12 * Monkey A.I. that puts its hand on the left wall and follows it. 10 * Monkey A.I. that puts its hand on the left wall and follows it.
@@ -31,13 +29,12 @@ public class Monkey extends Animal {
31 /** 29 /**
32 * Finds a currentDirection following the "left paw rule". 30 * Finds a currentDirection following the "left paw rule".
33 * 31 *
34 * @param choices An array of possible directions 32 * @param choices A set of possible directions
35 * @return The currentDirection to take according to the "left paw rule" 33 * @return The currentDirection to take according to the "left paw rule"
36 */ 34 */
37 private Direction followLeft(Direction[] choices) { 35 private Direction followLeft(Set<Direction> choices) {
38 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
39 Direction dir = this.currentDirection.rotateLeft(); 36 Direction dir = this.currentDirection.rotateLeft();
40 while (!choiceList.contains(dir)) dir = dir.rotateRight(); 37 while (!choices.contains(dir)) dir = dir.rotateRight();
41 38
42 return dir; 39 return dir;
43 } 40 }
@@ -45,13 +42,13 @@ public class Monkey extends Animal {
45 /** 42 /**
46 * Determines the strategy to follow depending on special cases. 43 * Determines the strategy to follow depending on special cases.
47 * 44 *
48 * @param choices An array of possible directions 45 * @param choices A set of possible directions
49 * @return The Direction to take 46 * @return The Direction to take
50 */ 47 */
51 private Direction findDirection(Direction[] choices) { 48 private Direction findDirection(Set<Direction> choices) {
52 if (choices.length == 0) return Direction.NONE; 49 if (choices.isEmpty()) return Direction.NONE;
53 if (this.currentDirection == Direction.NONE) return choices[0]; 50 if (this.currentDirection == Direction.NONE) return choices.stream().findAny().get();
54 if (choices.length == 1) return choices[0]; 51 if (choices.size() == 1) return choices.stream().findAny().get();
55 return this.followLeft(choices); 52 return this.followLeft(choices);
56 } 53 }
57 54
@@ -59,7 +56,7 @@ public class Monkey extends Animal {
59 * Moves according to the relative left wall that the monkey has to follow. 56 * Moves according to the relative left wall that the monkey has to follow.
60 */ 57 */
61 @Override 58 @Override
62 public Direction move(Direction[] choices) { 59 public Direction move(Set<Direction> choices) {
63 this.currentDirection = this.findDirection(choices); 60 this.currentDirection = this.findDirection(choices);
64 return this.currentDirection; 61 return this.currentDirection;
65 } 62 }
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;
6import ch.epfl.maze.util.Trail; 6import ch.epfl.maze.util.Trail;
7import ch.epfl.maze.util.Vector2D; 7import ch.epfl.maze.util.Vector2D;
8 8
9import java.util.ArrayList; 9import java.util.EnumSet;
10import java.util.Arrays; 10import java.util.Set;
11import java.util.stream.Collectors; 11import 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 /**