From 7e7e2c2f5a2261dcb3c4035e42236a099fefc343 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 23 Nov 2015 21:24:39 +0100 Subject: Implement Bear A.I. --- src/ch/epfl/maze/physical/zoo/Bear.java | 69 +++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 8 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/zoo/Bear.java b/src/ch/epfl/maze/physical/zoo/Bear.java index 5cccc08..2ef9215 100644 --- a/src/ch/epfl/maze/physical/zoo/Bear.java +++ b/src/ch/epfl/maze/physical/zoo/Bear.java @@ -4,21 +4,66 @@ 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; + /** * Bear A.I. that implements the Pledge Algorithm. + * + * @author Pacien TRAN-GIRARD */ - public class Bear extends Animal { + private Direction favoriteDirection; + private Direction currentDirection; + private int rightRotationCounter; + /** * Constructs a bear with a starting position. * * @param position Starting position of the bear in the labyrinth */ - public Bear(Vector2D position) { super(position); - // TODO + this.favoriteDirection = Direction.NONE; + this.currentDirection = Direction.NONE; + this.rightRotationCounter = 0; + } + + /** + * Checks if the Bear is currently trying to bypass an obstacle. + * + * @return T(The Bear is bypassing an obstacle) + */ + private boolean isBypassingObstacle() { + return this.rightRotationCounter != 0; + } + + /** + * Returns the preferred Direction to take according to the current strategy. + * + * @return The preferred Direction + */ + private Direction getPreferredDir() { + return this.isBypassingObstacle() ? this.currentDirection.rotateLeft() : this.favoriteDirection; + } + + /** + * Finds a currentDirection following the "left paw rule". + * + * @param choices An array 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]; + + List choiceList = new ArrayList<>(Arrays.asList(choices)); + Direction dir = this.getPreferredDir(); + while (!choiceList.contains(dir)) dir = dir.rotateRight(); + + return dir; } /** @@ -29,16 +74,24 @@ public class Bear extends Animal { * will repeat the procedure when the counter comes to zero, or until it * leaves the maze. */ - @Override public Direction move(Direction[] choices) { - // TODO - return Direction.NONE; + if (this.favoriteDirection == Direction.NONE) + this.favoriteDirection = choices[0]; + + Direction newDirection = this.followLeft(choices); + + if (this.currentDirection.reverse() == newDirection) this.rightRotationCounter += 2; + else if (this.currentDirection.rotateRight() == newDirection) this.rightRotationCounter += 1; + else if (this.currentDirection.rotateLeft() == newDirection) this.rightRotationCounter -= 1; + + this.currentDirection = newDirection; + return this.currentDirection; } @Override public Animal copy() { - // TODO - return null; + return new Monkey(this.getPosition()); } + } -- cgit v1.2.3