summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java69
1 files changed, 61 insertions, 8 deletions
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;
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;
8import java.util.Arrays;
9import java.util.List;
10
7/** 11/**
8 * Bear A.I. that implements the Pledge Algorithm. 12 * Bear A.I. that implements the Pledge Algorithm.
13 *
14 * @author Pacien TRAN-GIRARD
9 */ 15 */
10
11public class Bear extends Animal { 16public class Bear extends Animal {
12 17
18 private Direction favoriteDirection;
19 private Direction currentDirection;
20 private int rightRotationCounter;
21
13 /** 22 /**
14 * Constructs a bear with a starting position. 23 * Constructs a bear with a starting position.
15 * 24 *
16 * @param position Starting position of the bear in the labyrinth 25 * @param position Starting position of the bear in the labyrinth
17 */ 26 */
18
19 public Bear(Vector2D position) { 27 public Bear(Vector2D position) {
20 super(position); 28 super(position);
21 // TODO 29 this.favoriteDirection = Direction.NONE;
30 this.currentDirection = Direction.NONE;
31 this.rightRotationCounter = 0;
32 }
33
34 /**
35 * Checks if the Bear is currently trying to bypass an obstacle.
36 *
37 * @return T(The Bear is bypassing an obstacle)
38 */
39 private boolean isBypassingObstacle() {
40 return this.rightRotationCounter != 0;
41 }
42
43 /**
44 * Returns the preferred Direction to take according to the current strategy.
45 *
46 * @return The preferred Direction
47 */
48 private Direction getPreferredDir() {
49 return this.isBypassingObstacle() ? this.currentDirection.rotateLeft() : this.favoriteDirection;
50 }
51
52 /**
53 * Finds a currentDirection following the "left paw rule".
54 *
55 * @param choices An array of possible directions
56 * @return The currentDirection to take according to the "left paw rule"
57 */
58 private Direction followLeft(Direction[] choices) {
59 if (choices.length == 0) return Direction.NONE;
60 if (choices.length == 1) return choices[0];
61
62 List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
63 Direction dir = this.getPreferredDir();
64 while (!choiceList.contains(dir)) dir = dir.rotateRight();
65
66 return dir;
22 } 67 }
23 68
24 /** 69 /**
@@ -29,16 +74,24 @@ public class Bear extends Animal {
29 * will repeat the procedure when the counter comes to zero, or until it 74 * will repeat the procedure when the counter comes to zero, or until it
30 * leaves the maze. 75 * leaves the maze.
31 */ 76 */
32
33 @Override 77 @Override
34 public Direction move(Direction[] choices) { 78 public Direction move(Direction[] choices) {
35 // TODO 79 if (this.favoriteDirection == Direction.NONE)
36 return Direction.NONE; 80 this.favoriteDirection = choices[0];
81
82 Direction newDirection = this.followLeft(choices);
83
84 if (this.currentDirection.reverse() == newDirection) this.rightRotationCounter += 2;
85 else if (this.currentDirection.rotateRight() == newDirection) this.rightRotationCounter += 1;
86 else if (this.currentDirection.rotateLeft() == newDirection) this.rightRotationCounter -= 1;
87
88 this.currentDirection = newDirection;
89 return this.currentDirection;
37 } 90 }
38 91
39 @Override 92 @Override
40 public Animal copy() { 93 public Animal copy() {
41 // TODO 94 return new Monkey(this.getPosition());
42 return null;
43 } 95 }
96
44} 97}