blob: 803b574f01c011122aa5b5453128ec3e0eac7008 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package ch.epfl.maze.physical.zoo;
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 EPFL
* @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);
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<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
Direction dir = this.getPreferredDir();
while (!choiceList.contains(dir)) dir = dir.rotateRight();
return dir;
}
/**
* Moves according to the <i>Pledge Algorithm</i> : the bear tries to move
* towards a favorite direction until it hits a wall. In this case, it will
* turn right, put its paw on the left wall, count the number of times it
* turns right, and subtract to this the number of times it turns left. It
* will repeat the procedure when the counter comes to zero, or until it
* leaves the maze.
*/
@Override
public Direction move(Direction[] choices) {
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() {
return new Monkey(this.getPosition());
}
}
|