From fbbd459e2646870da9a96ce65e77bc090c2b9930 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 16:18:19 +0100 Subject: Refactor Hamster A.I. --- .../stragegies/reducer/BlindCaseReducer.java | 31 ++++++++ src/ch/epfl/maze/physical/zoo/Hamster.java | 87 +++++++++++++++++----- 2 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java new file mode 100644 index 0000000..b359cc2 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/reducer/BlindCaseReducer.java @@ -0,0 +1,31 @@ +package ch.epfl.maze.physical.stragegies.reducer; + +import ch.epfl.maze.util.Direction; + +import java.util.Set; +import java.util.stream.Collectors; + +/** + * A blind reducer filtering possibilities case by case. + * + * @author Pacien TRAN-GIRARD + */ +public interface BlindCaseReducer extends BlindChoiceReducer { + + /** + * Checks if the given choice should be kept or excluded by the filter. + * + * @param choice A Direction + * @return T(The filter should keep the given choice) + */ + boolean keepChoice(Direction choice); + + @Override + default Set reduce(Set choices) { + return choices + .stream() + .filter(choice -> this.keepChoice(choice)) + .collect(Collectors.toSet()); + } + +} diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java index 527e8ce..8586dee 100644 --- a/src/ch/epfl/maze/physical/zoo/Hamster.java +++ b/src/ch/epfl/maze/physical/zoo/Hamster.java @@ -1,14 +1,14 @@ package ch.epfl.maze.physical.zoo; import ch.epfl.maze.physical.Animal; -import ch.epfl.maze.physical.ProbabilisticAnimal; +import ch.epfl.maze.physical.stragegies.picker.RandomPicker; +import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; +import ch.epfl.maze.physical.stragegies.reducer.BlindCaseReducer; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; import java.util.Set; -import java.util.stream.Collectors; /** * Hamster A.I. that remembers the previous choice it has made and the dead ends @@ -17,9 +17,9 @@ import java.util.stream.Collectors; * @author EPFL * @author Pacien TRAN-GIRARD */ -public class Hamster extends ProbabilisticAnimal { +public class Hamster extends Animal implements BlindCaseReducer, BackwardReducer, RandomPicker { - private final List deadPaths; + private final Set positionBlacklist; /** * Constructs a hamster with a starting position. @@ -28,20 +28,33 @@ public class Hamster extends ProbabilisticAnimal { */ public Hamster(Vector2D position) { super(position); - this.deadPaths = new ArrayList<>(); + + this.positionBlacklist = new HashSet<>(); + } + + /** + * Checks is the choice leads to a known dead end. + * + * @param choice A Direction + * @return T(The given choice does not lead to a blacklisted path) + */ + @Override + public boolean keepChoice(Direction choice) { + Vector2D choicePosition = this.getPosition().addDirectionTo(choice); + return !this.isPositionBlacklisted(choicePosition); } /** - * Discard directions known to lead to dead ends. + * Filters the Direction choices using the blacklist. * - * @param choices A set of choices - * @return A set of smart choices + * @param choices A set of Direction choices + * @return A subset of choices */ - private Set excludeDeadPaths(Set choices) { - return choices - .stream() - .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) - .collect(Collectors.toSet()); + @Override + public Set reduce(Set choices) { + Set knownChoices = BlindCaseReducer.super.reduce(choices); + this.updateBlacklist(knownChoices); + return knownChoices.size() > 1 ? BackwardReducer.super.reduce(knownChoices) : knownChoices; } /** @@ -50,9 +63,8 @@ public class Hamster extends ProbabilisticAnimal { */ @Override public Direction move(Set choices) { - Set smartChoices = this.excludeDeadPaths(choices); - if (smartChoices.size() == 1) this.deadPaths.add(this.getPosition()); // dead end - return super.move(smartChoices); + Set smartChoices = this.reduce(choices); + return this.pick(smartChoices); } @Override @@ -60,4 +72,43 @@ public class Hamster extends ProbabilisticAnimal { return new Hamster(this.getPosition()); } + /** + * Updates the position blacklist if needed. + * + * @param choices The choices currently available + */ + private void updateBlacklist(Set choices) { + if (this.inDeadEnd(choices)) + this.blacklistPosition(this.getPosition()); + } + + /** + * Checks if the given choices set correspond to a dead end. + * + * @param choices The choices currently available + * @return T(The given choices set correspond to a dead end) + */ + private boolean inDeadEnd(Set choices) { + return choices.size() < 2; + } + + /** + * Checks if the given position is known to lead to a dead end. + * + * @param position The position to check + * @return T(The given position is blacklisted) + */ + private boolean isPositionBlacklisted(Vector2D position) { + return this.positionBlacklist.contains(position); + } + + /** + * Regiters the given position in the blacklist. + * + * @param position The position to blacklist + */ + private void blacklistPosition(Vector2D position) { + this.positionBlacklist.add(position); + } + } -- cgit v1.2.3