From 9bec90d20c20b01e94a8b5d8364bbe60f9687f05 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 17:57:23 +0100 Subject: Refactor Bear A.I. --- src/ch/epfl/maze/physical/zoo/Bear.java | 79 +++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 34 deletions(-) (limited to 'src/ch/epfl/maze/physical/zoo') diff --git a/src/ch/epfl/maze/physical/zoo/Bear.java b/src/ch/epfl/maze/physical/zoo/Bear.java index 11faba5..062ee50 100644 --- a/src/ch/epfl/maze/physical/zoo/Bear.java +++ b/src/ch/epfl/maze/physical/zoo/Bear.java @@ -1,6 +1,8 @@ package ch.epfl.maze.physical.zoo; import ch.epfl.maze.physical.Animal; +import ch.epfl.maze.physical.stragegies.picker.CyclePicker; +import ch.epfl.maze.physical.stragegies.picker.SimplePicker; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; @@ -12,11 +14,12 @@ import java.util.Set; * @author EPFL * @author Pacien TRAN-GIRARD */ -public class Bear extends Animal { +public class Bear extends Animal implements SimplePicker, CyclePicker { + + public static final Direction SMART_PAW = Direction.LEFT; private Direction favoriteDirection; - private Direction currentDirection; - private int rightRotationCounter; + private int rotationCounter; /** * Constructs a bear with a starting position. @@ -25,18 +28,9 @@ public class Bear extends Animal { */ public Bear(Vector2D position) { super(position); - 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; + this.favoriteDirection = Direction.NONE; + this.rotationCounter = 0; } /** @@ -44,24 +38,35 @@ public class Bear extends Animal { * * @return The preferred Direction */ - private Direction getPreferredDir() { - return this.isBypassingObstacle() ? this.currentDirection.rotateLeft() : this.favoriteDirection; + @Override + public Direction getStartingDirection() { + if (this.isBypassingObstacle()) + return this.getDirection().unRelativeDirection(Bear.SMART_PAW); + else + return this.favoriteDirection; + } + + @Override + public Direction getRotationDirection() { + return Bear.SMART_PAW.reverse(); } /** - * Finds a currentDirection following the "left paw rule". + * Preferably picks the Bear's favorite Direction, + * or a Direction following the "paw rule" if bypassing an obstacle, + * or any available Direction if the Bear has no preference yet. * - * @param choices A set of possible directions - * @return The currentDirection to take according to the "left paw rule" + * @param choices A set of Direction choices + * @return The picked Direction */ - private Direction followLeft(Set choices) { - if (choices.isEmpty()) return Direction.NONE; - if (choices.size() == 1) return choices.stream().findAny().get(); - - Direction dir = this.getPreferredDir(); - while (!choices.contains(dir)) dir = dir.rotateRight(); - - return dir; + @Override + public Direction pick(Set choices) { + if (this.getDirection() == Direction.NONE) + return SimplePicker.super.pick(choices); + else if (!choices.contains(this.getDirection()) || this.isBypassingObstacle()) + return CyclePicker.super.pick(choices); + else + return this.favoriteDirection; } /** @@ -75,16 +80,13 @@ public class Bear extends Animal { @Override public Direction move(Set choices) { if (this.favoriteDirection == Direction.NONE) - this.favoriteDirection = choices.stream().findAny().get(); + this.favoriteDirection = this.getDirection(); - Direction newDirection = this.followLeft(choices); + Direction dir = this.pick(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.rotationCounter += this.countRotations(dir); - this.currentDirection = newDirection; - return this.currentDirection; + return dir; } @Override @@ -92,4 +94,13 @@ public class Bear extends Animal { return new Monkey(this.getPosition()); } + /** + * Checks whether the Bear is currently trying to bypass an obstacle. + * + * @return T(The Bear is bypassing an obstacle) + */ + private boolean isBypassingObstacle() { + return this.rotationCounter != 0; + } + } -- cgit v1.2.3