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. --- .../physical/stragegies/picker/CyclePicker.java | 41 ++++++++++++++++++++ .../physical/stragegies/picker/SimplePicker.java | 21 +++++++++++ src/ch/epfl/maze/physical/zoo/Monkey.java | 44 +++++++++++----------- 3 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java new file mode 100644 index 0000000..e1a969f --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java @@ -0,0 +1,41 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A decision maker cycling in each Direction starting from a given one. + * + * @author Pacien TRAN-GIRARD + */ +public interface CyclePicker extends BlindPicker { + + /** + * Returns the cycle starting Direction. + * + * @return The starting Direction + */ + Direction getStartingDirection(); + + /** + * Returns the Rotation Direction. + * + * @return The Rotation Direction + */ + Direction getRotationDirection(); + + @Override + default Direction pick(Set choices) { + if (choices.isEmpty()) return FALLBACK_DIRECTION; + if (this.getStartingDirection() == Direction.NONE) return FALLBACK_DIRECTION; + if (this.getRotationDirection() == Direction.NONE) return FALLBACK_DIRECTION; + + Direction dir = this.getStartingDirection(); + while (!choices.contains(dir)) + dir = dir.unRelativeDirection(this.getRotationDirection()); + + return dir; + } + +} diff --git a/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java new file mode 100644 index 0000000..71f0802 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/SimplePicker.java @@ -0,0 +1,21 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A simple decision maker with no guarantee of randomness. + * + * @author Pacien TRAN-GIRARD + */ +public interface SimplePicker extends BlindPicker { + + @Override + default Direction pick(Set choices) { + if (choices.isEmpty()) return FALLBACK_DIRECTION; + + return choices.stream().findFirst().get(); + } + +} 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