From 71342b11dab1ed49e611292849107a6eb9c41a67 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 19:14:44 +0100 Subject: Implement Monkey A.I. --- src/ch/epfl/maze/physical/zoo/Monkey.java | 35 ++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java index f0f9b2e..8aea75d 100644 --- a/src/ch/epfl/maze/physical/zoo/Monkey.java +++ b/src/ch/epfl/maze/physical/zoo/Monkey.java @@ -4,12 +4,20 @@ 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. * @@ -18,7 +26,25 @@ public class Monkey extends Animal { public Monkey(Vector2D position) { super(position); - // TODO + 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) { + if (choices.length == 0) return Direction.NONE; + if (choices.length == 1) return choices[0]; + + List choiceList = new ArrayList<>(Arrays.asList(choices)); + Direction dir = this.currentDirection.rotateLeft(); + while (!choiceList.contains(dir)) dir = dir.rotateRight(); + + return dir; } /** @@ -27,13 +53,12 @@ public class Monkey extends Animal { @Override public Direction move(Direction[] choices) { - // TODO - return Direction.NONE; + this.currentDirection = this.followLeft(choices); + return this.currentDirection; } @Override public Animal copy() { - // TODO - return null; + return new Monkey(this.getPosition()); } } -- cgit v1.2.3