From 3f5c3d91a084233cfc77635697b3c537adbced74 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 16:33:34 +0100 Subject: Refactor Monkey A.I. --- src/ch/epfl/maze/physical/zoo/Monkey.java | 44 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'src/ch/epfl/maze/physical/zoo/Monkey.java') diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java index 23eec4e..3c84f59 100644 --- a/src/ch/epfl/maze/physical/zoo/Monkey.java +++ b/src/ch/epfl/maze/physical/zoo/Monkey.java @@ -1,6 +1,8 @@ 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; @@ -12,9 +14,9 @@ import java.util.Set; * @author EPFL * @author Pacien TRAN-GIRARD */ -public class Monkey extends Animal { +public class Monkey extends Animal implements SimplePicker, CyclePicker { - private Direction currentDirection; + public static final Direction SMART_PAW = Direction.LEFT; /** * Constructs a monkey with a starting position. @@ -23,33 +25,30 @@ public class Monkey extends Animal { */ 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(); + @Override + public Direction getStartingDirection() { + return this.getDirection().unRelativeDirection(Monkey.SMART_PAW); + } - return dir; + @Override + public Direction getRotationDirection() { + return Monkey.SMART_PAW.reverse(); } /** - * Determines the strategy to follow depending on special cases. + * Picks a Direction following the "paw rule", or any available if the Monkey has no current Direction. * - * @param choices A set of possible directions - * @return The Direction to take + * @param choices A set of Direction choices + * @return The picked Direction */ - 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); + @Override + public Direction pick(Set choices) { + if (this.getDirection() == Direction.NONE) + return SimplePicker.super.pick(choices); + else + return CyclePicker.super.pick(choices); } /** @@ -57,8 +56,7 @@ public class Monkey extends Animal { */ @Override public Direction move(Set choices) { - this.currentDirection = this.findDirection(choices); - return this.currentDirection; + return this.pick(choices); } @Override -- cgit v1.2.3