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; /** * Monkey A.I. that puts its hand on the left wall and follows it. * * @author EPFL * @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 A set of possible directions * @return The currentDirection to take according to the "left paw rule" */ private Direction followLeft(Set choices) { Direction dir = this.currentDirection.rotateLeft(); while (!choices.contains(dir)) dir = dir.rotateRight(); return dir; } /** * Determines the strategy to follow depending on special cases. * * @param choices A set of possible directions * @return The Direction to take */ private Direction findDirection(Set choices) { if (choices.isEmpty()) return Direction.NONE; if (this.currentDirection == Direction.NONE) return choices.stream().findAny().get(); if (choices.size() == 1) return choices.stream().findAny().get(); return this.followLeft(choices); } /** * Moves according to the relative left wall that the monkey has to follow. */ @Override public Direction move(Set choices) { this.currentDirection = this.findDirection(choices); return this.currentDirection; } @Override public Animal copy() { return new Monkey(this.getPosition()); } }