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

/**
 * Monkey A.I. that puts its hand on the left wall and follows it.
 *
 * @author Pacien TRAN-GIRARD
 */

public class Monkey extends Animal {

    private Direction currentDirection;

    /**
     * Constructs a monkey with a starting position.
     *
     * @param position Starting position of the monkey in the labyrinth
     */

    public Monkey(Vector2D position) {
        super(position);
        this.currentDirection = Direction.NONE;
    }

    /**
     * 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) {
        List<Direction> choiceList = new ArrayList<>(Arrays.asList(choices));
        Direction dir = this.currentDirection.rotateLeft();
        while (!choiceList.contains(dir)) dir = dir.rotateRight();

        return dir;
    }

    /**
     * Determines the strategy to follow depending on special cases.
     *
     * @param choices An array of possible directions
     * @return The Direction to take
     */
    private Direction findDirection(Direction[] choices) {
        if (choices.length == 0) return Direction.NONE;
        if (this.currentDirection == Direction.NONE) return choices[0];
        if (choices.length == 1) return choices[0];
        if (choices.length > 3) return choices[0];
        return this.followLeft(choices);
    }

    /**
     * Moves according to the relative left wall that the monkey has to follow.
     */

    @Override
    public Direction move(Direction[] choices) {
        this.currentDirection = this.findDirection(choices);
        return this.currentDirection;
    }

    @Override
    public Animal copy() {
        return new Monkey(this.getPosition());
    }
}