summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/zoo/Bear.java
blob: 11faba56862071dfdd6e5162c6fd518588106699 (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
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.Set;

/**
 * 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 A set of possible directions
     * @return The currentDirection to take according to the "left paw rule"
     */
    private Direction followLeft(Set<Direction> 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;
    }

    /**
     * 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 = choices.stream().findAny().get();

        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());
    }

}