summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo/Bear.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 14:28:26 +0100
committerPacien TRAN-GIRARD2015-11-24 14:28:26 +0100
commite2354d82e09c3bf8ae472d174332670d2d12f9bb (patch)
tree92528b9231178fd47c6910163360413d4ae77256 /src/ch/epfl/maze/physical/zoo/Bear.java
parent5f9be99e7ebd4a90e666e6efbe9e9d4b3b8008e7 (diff)
downloadmaze-solver-e2354d82e09c3bf8ae472d174332670d2d12f9bb.tar.gz
Use Set instead of array for Direction choices
Diffstat (limited to 'src/ch/epfl/maze/physical/zoo/Bear.java')
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java19
1 files changed, 8 insertions, 11 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