From e2354d82e09c3bf8ae472d174332670d2d12f9bb Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 14:28:26 +0100 Subject: Use Set instead of array for Direction choices --- src/ch/epfl/maze/physical/zoo/Bear.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/ch/epfl/maze/physical/zoo/Bear.java') 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; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.Set; /** * Bear A.I. that implements the Pledge Algorithm. @@ -53,16 +51,15 @@ public class Bear extends Animal { /** * Finds a currentDirection following the "left paw rule". * - * @param choices An array of possible directions + * @param choices A set of possible directions * @return The currentDirection to take according to the "left paw rule" */ - private Direction followLeft(Direction[] choices) { - if (choices.length == 0) return Direction.NONE; - if (choices.length == 1) return choices[0]; + private Direction followLeft(Set choices) { + if (choices.isEmpty()) return Direction.NONE; + if (choices.size() == 1) return choices.stream().findAny().get(); - List choiceList = new ArrayList<>(Arrays.asList(choices)); Direction dir = this.getPreferredDir(); - while (!choiceList.contains(dir)) dir = dir.rotateRight(); + while (!choices.contains(dir)) dir = dir.rotateRight(); return dir; } @@ -76,9 +73,9 @@ public class Bear extends Animal { * leaves the maze. */ @Override - public Direction move(Direction[] choices) { + public Direction move(Set choices) { if (this.favoriteDirection == Direction.NONE) - this.favoriteDirection = choices[0]; + this.favoriteDirection = choices.stream().findAny().get(); Direction newDirection = this.followLeft(choices); -- cgit v1.2.3