summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-24 17:57:23 +0100
committerPacien TRAN-GIRARD2015-11-24 17:57:23 +0100
commit9bec90d20c20b01e94a8b5d8364bbe60f9687f05 (patch)
tree69056ce67731922d41505e8e684b890f7942db31
parent96932b453e3f2fa1cfb45e79aca49e72d7b8d10f (diff)
downloadmaze-solver-9bec90d20c20b01e94a8b5d8364bbe60f9687f05.tar.gz
Refactor Bear A.I.
-rw-r--r--src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java21
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java79
2 files changed, 66 insertions, 34 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
index e1a969f..efa3794 100644
--- a/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
+++ b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
@@ -38,4 +38,25 @@ public interface CyclePicker extends BlindPicker {
38 return dir; 38 return dir;
39 } 39 }
40 40
41 /**
42 * Counts the number of rotations to rotates to the given Direction
43 *
44 * @param direction The Direction
45 * @return The number of rotations
46 */
47 default int countRotations(Direction direction) {
48 if (direction == Direction.NONE) return 0;
49 if (this.getStartingDirection() == Direction.NONE) return 0;
50 if (this.getRotationDirection() == Direction.NONE) return 0;
51
52 Direction dir = this.getStartingDirection();
53 int counter = 0;
54 while (dir != direction) {
55 dir = dir.unRelativeDirection(this.getRotationDirection());
56 counter += 1;
57 }
58
59 return counter;
60 }
61
41} 62}
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 @@
1package ch.epfl.maze.physical.zoo; 1package ch.epfl.maze.physical.zoo;
2 2
3import ch.epfl.maze.physical.Animal; 3import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.physical.stragegies.picker.CyclePicker;
5import ch.epfl.maze.physical.stragegies.picker.SimplePicker;
4import ch.epfl.maze.util.Direction; 6import ch.epfl.maze.util.Direction;
5import ch.epfl.maze.util.Vector2D; 7import ch.epfl.maze.util.Vector2D;
6 8
@@ -12,11 +14,12 @@ import java.util.Set;
12 * @author EPFL 14 * @author EPFL
13 * @author Pacien TRAN-GIRARD 15 * @author Pacien TRAN-GIRARD
14 */ 16 */
15public class Bear extends Animal { 17public class Bear extends Animal implements SimplePicker, CyclePicker {
18
19 public static final Direction SMART_PAW = Direction.LEFT;
16 20
17 private Direction favoriteDirection; 21 private Direction favoriteDirection;
18 private Direction currentDirection; 22 private int rotationCounter;
19 private int rightRotationCounter;
20 23
21 /** 24 /**
22 * Constructs a bear with a starting position. 25 * Constructs a bear with a starting position.
@@ -25,18 +28,9 @@ public class Bear extends Animal {
25 */ 28 */
26 public Bear(Vector2D position) { 29 public Bear(Vector2D position) {
27 super(position); 30 super(position);
28 this.favoriteDirection = Direction.NONE;
29 this.currentDirection = Direction.NONE;
30 this.rightRotationCounter = 0;
31 }
32 31
33 /** 32 this.favoriteDirection = Direction.NONE;
34 * Checks if the Bear is currently trying to bypass an obstacle. 33 this.rotationCounter = 0;
35 *
36 * @return T(The Bear is bypassing an obstacle)
37 */
38 private boolean isBypassingObstacle() {
39 return this.rightRotationCounter != 0;
40 } 34 }
41 35
42 /** 36 /**
@@ -44,24 +38,35 @@ public class Bear extends Animal {
44 * 38 *
45 * @return The preferred Direction 39 * @return The preferred Direction
46 */ 40 */
47 private Direction getPreferredDir() { 41 @Override
48 return this.isBypassingObstacle() ? this.currentDirection.rotateLeft() : this.favoriteDirection; 42 public Direction getStartingDirection() {
43 if (this.isBypassingObstacle())
44 return this.getDirection().unRelativeDirection(Bear.SMART_PAW);
45 else
46 return this.favoriteDirection;
47 }
48
49 @Override
50 public Direction getRotationDirection() {
51 return Bear.SMART_PAW.reverse();
49 } 52 }
50 53
51 /** 54 /**
52 * Finds a currentDirection following the "left paw rule". 55 * Preferably picks the Bear's favorite Direction,
56 * or a Direction following the "paw rule" if bypassing an obstacle,
57 * or any available Direction if the Bear has no preference yet.
53 * 58 *
54 * @param choices A set of possible directions 59 * @param choices A set of Direction choices
55 * @return The currentDirection to take according to the "left paw rule" 60 * @return The picked Direction
56 */ 61 */
57 private Direction followLeft(Set<Direction> choices) { 62 @Override
58 if (choices.isEmpty()) return Direction.NONE; 63 public Direction pick(Set<Direction> choices) {
59 if (choices.size() == 1) return choices.stream().findAny().get(); 64 if (this.getDirection() == Direction.NONE)
60 65 return SimplePicker.super.pick(choices);
61 Direction dir = this.getPreferredDir(); 66 else if (!choices.contains(this.getDirection()) || this.isBypassingObstacle())
62 while (!choices.contains(dir)) dir = dir.rotateRight(); 67 return CyclePicker.super.pick(choices);
63 68 else
64 return dir; 69 return this.favoriteDirection;
65 } 70 }
66 71
67 /** 72 /**
@@ -75,16 +80,13 @@ public class Bear extends Animal {
75 @Override 80 @Override
76 public Direction move(Set<Direction> choices) { 81 public Direction move(Set<Direction> choices) {
77 if (this.favoriteDirection == Direction.NONE) 82 if (this.favoriteDirection == Direction.NONE)
78 this.favoriteDirection = choices.stream().findAny().get(); 83 this.favoriteDirection = this.getDirection();
79 84
80 Direction newDirection = this.followLeft(choices); 85 Direction dir = this.pick(choices);
81 86
82 if (this.currentDirection.reverse() == newDirection) this.rightRotationCounter += 2; 87 this.rotationCounter += this.countRotations(dir);
83 else if (this.currentDirection.rotateRight() == newDirection) this.rightRotationCounter += 1;
84 else if (this.currentDirection.rotateLeft() == newDirection) this.rightRotationCounter -= 1;
85 88
86 this.currentDirection = newDirection; 89 return dir;
87 return this.currentDirection;
88 } 90 }
89 91
90 @Override 92 @Override
@@ -92,4 +94,13 @@ public class Bear extends Animal {
92 return new Monkey(this.getPosition()); 94 return new Monkey(this.getPosition());
93 } 95 }
94 96
97 /**
98 * Checks whether the Bear is currently trying to bypass an obstacle.
99 *
100 * @return T(The Bear is bypassing an obstacle)
101 */
102 private boolean isBypassingObstacle() {
103 return this.rotationCounter != 0;
104 }
105
95} 106}