From 7ca733457566fdfa097e3317d96a50147229ca74 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 17:31:28 +0100 Subject: Implement Mouse A.I. --- src/ch/epfl/maze/physical/zoo/Mouse.java | 37 +++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java index e5cb9ae..1b22c66 100644 --- a/src/ch/epfl/maze/physical/zoo/Mouse.java +++ b/src/ch/epfl/maze/physical/zoo/Mouse.java @@ -1,14 +1,22 @@ 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; + /** * Mouse A.I. that remembers only the previous choice it has made. + * + * @author Pacien TRAN-GIRARD */ -public class Mouse extends Animal { +public class Mouse extends ProbabilisticAnimal { + + private Direction previousChoice; /** * Constructs a mouse with a starting position. @@ -18,6 +26,24 @@ public class Mouse extends Animal { public Mouse(Vector2D position) { super(position); + this.previousChoice = Direction.NONE; + } + + /** + * Excludes the origin direction for choices. + * + * @param choices An array of choices + * @return An array of smart choices + */ + + private Direction[] excludeOrigin(Direction[] choices) { + List smartChoices = new ArrayList<>(choices.length - 1); // max size, excluding origin + + for (Direction dir : choices) + if (!dir.isOpposite(this.previousChoice)) + smartChoices.add(dir); + + return smartChoices.toArray(new Direction[smartChoices.size()]); } /** @@ -27,13 +53,14 @@ public class Mouse extends Animal { @Override public Direction move(Direction[] choices) { - // TODO - return Direction.NONE; + Direction[] smartChoices = choices.length > 1 ? this.excludeOrigin(choices) : choices; + Direction dir = super.move(smartChoices); + this.previousChoice = dir; + return dir; } @Override public Animal copy() { - // TODO - return null; + return new Mouse(this.getPosition()); } } -- cgit v1.2.3