blob: 062ee5054bb236620bec48fe15f62a93ee84a64d (
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
99
100
101
102
103
104
105
106
|
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;
import java.util.Set;
/**
* Bear A.I. that implements the Pledge Algorithm.
*
* @author EPFL
* @author Pacien TRAN-GIRARD
*/
public class Bear extends Animal implements SimplePicker, CyclePicker {
public static final Direction SMART_PAW = Direction.LEFT;
private Direction favoriteDirection;
private int rotationCounter;
/**
* 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.rotationCounter = 0;
}
/**
* Returns the preferred Direction to take according to the current strategy.
*
* @return The preferred Direction
*/
@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();
}
/**
* 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 Direction choices
* @return The picked Direction
*/
@Override
public Direction pick(Set<Direction> 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;
}
/**
* 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(Set<Direction> choices) {
if (this.favoriteDirection == Direction.NONE)
this.favoriteDirection = this.getDirection();
Direction dir = this.pick(choices);
this.rotationCounter += this.countRotations(dir);
return dir;
}
@Override
public Animal copy() {
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;
}
}
|