From 96932b453e3f2fa1cfb45e79aca49e72d7b8d10f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 16:59:05 +0100 Subject: Update Monkey A.I. for open positions --- .../physical/stragegies/picker/ForwardPicker.java | 29 ++++++++++++++++++++++ src/ch/epfl/maze/physical/zoo/Monkey.java | 25 +++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java diff --git a/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java new file mode 100644 index 0000000..b7a44f8 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/ForwardPicker.java @@ -0,0 +1,29 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A simple decision maker that continues forward if possible. + * + * @author Pacien TRAN-GIRARD + */ +public interface ForwardPicker extends BlindPicker { + + /** + * Returns the current Direction the decision maker is walking toward. + * + * @return The current Direction + */ + Direction getDirection(); + + @Override + default Direction pick(Set choices) { + if (choices.contains(this.getDirection())) + return this.getDirection(); + else + return FALLBACK_DIRECTION; + } + +} diff --git a/src/ch/epfl/maze/physical/zoo/Monkey.java b/src/ch/epfl/maze/physical/zoo/Monkey.java index 3c84f59..5b6998d 100644 --- a/src/ch/epfl/maze/physical/zoo/Monkey.java +++ b/src/ch/epfl/maze/physical/zoo/Monkey.java @@ -2,6 +2,7 @@ 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.ForwardPicker; import ch.epfl.maze.physical.stragegies.picker.SimplePicker; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; @@ -14,10 +15,12 @@ import java.util.Set; * @author EPFL * @author Pacien TRAN-GIRARD */ -public class Monkey extends Animal implements SimplePicker, CyclePicker { +public class Monkey extends Animal implements SimplePicker, ForwardPicker, CyclePicker { public static final Direction SMART_PAW = Direction.LEFT; + private boolean foundWall; + /** * Constructs a monkey with a starting position. * @@ -25,6 +28,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker { */ public Monkey(Vector2D position) { super(position); + + this.foundWall = false; } @Override @@ -38,7 +43,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker { } /** - * Picks a Direction following the "paw rule", or any available if the Monkey has no current Direction. + * Picks a Direction preferably following the "paw rule", or forward if no wall has been encountered, + * or any available Direction if the Monkey has no current Direction. * * @param choices A set of Direction choices * @return The picked Direction @@ -47,6 +53,8 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker { public Direction pick(Set choices) { if (this.getDirection() == Direction.NONE) return SimplePicker.super.pick(choices); + else if (!this.foundWall) + return ForwardPicker.super.pick(choices); else return CyclePicker.super.pick(choices); } @@ -56,6 +64,9 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker { */ @Override public Direction move(Set choices) { + if (this.nextToWall(choices)) + this.foundWall = true; + return this.pick(choices); } @@ -64,4 +75,14 @@ public class Monkey extends Animal implements SimplePicker, CyclePicker { return new Monkey(this.getPosition()); } + /** + * Checks is the Monkey is next to a wall. + * + * @param choices The available Direction choices + * @return T(The Direction choices set indicates the presence of a wall) + */ + private boolean nextToWall(Set choices) { + return choices.size() < 4; + } + } -- cgit v1.2.3