From 2d592a87e00c7d0248f53f388269de902afe6916 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 18:45:38 +0100 Subject: Implement Hamster A.I. --- src/ch/epfl/maze/physical/zoo/Hamster.java | 37 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java index 2a610f2..27706a5 100644 --- a/src/ch/epfl/maze/physical/zoo/Hamster.java +++ b/src/ch/epfl/maze/physical/zoo/Hamster.java @@ -1,15 +1,23 @@ package ch.epfl.maze.physical.zoo; import ch.epfl.maze.physical.Animal; +import ch.epfl.maze.physical.ProbabilisticAnimal; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; +import java.util.ArrayList; +import java.util.List; + /** * Hamster A.I. that remembers the previous choice it has made and the dead ends * it has already met. + * + * @author Pacien TRAN-GIRARD */ -public class Hamster extends Animal { +public class Hamster extends ProbabilisticAnimal { + + private final List deadPaths; /** * Constructs a hamster with a starting position. @@ -19,7 +27,24 @@ public class Hamster extends Animal { public Hamster(Vector2D position) { super(position); - // TODO + this.deadPaths = new ArrayList<>(); + } + + /** + * Discard directions known to lead to dead ends. + * + * @param choices An array of choices + * @return An array of smart choices + */ + + private Direction[] excludeDeadPaths(Direction[] choices) { + List smartChoices = new ArrayList<>(); + + for (Direction dir : choices) + if (!this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) + smartChoices.add(dir); + + return smartChoices.toArray(new Direction[smartChoices.size()]); } /** @@ -29,13 +54,13 @@ public class Hamster extends Animal { @Override public Direction move(Direction[] choices) { - // TODO - return Direction.NONE; + Direction[] smartChoices = this.excludeDeadPaths(choices); + if (smartChoices.length == 1) this.deadPaths.add(this.getPosition()); // dead end + return super.move(smartChoices); } @Override public Animal copy() { - // TODO - return null; + return new Hamster(this.getPosition()); } } -- cgit v1.2.3