From 6d278c3aa1fe96a7c1f56e8d9b1889beded513ba Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 16:32:17 +0100 Subject: Implement Maze --- src/ch/epfl/maze/physical/Maze.java | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/maze/physical/Maze.java b/src/ch/epfl/maze/physical/Maze.java index 71f2f80..e58a56a 100644 --- a/src/ch/epfl/maze/physical/Maze.java +++ b/src/ch/epfl/maze/physical/Maze.java @@ -7,10 +7,15 @@ import java.util.List; * Maze in which an animal starts from a starting point and must find the exit. * Every animal added will have its position set to the starting point. The * animal is removed from the maze when it finds the exit. + * + * @author Pacien TRAN-GIRARD */ public final class Maze extends World { + private final List animals; + private final List animalHistory; + /** * Constructs a Maze with a labyrinth structure. * @@ -19,19 +24,19 @@ public final class Maze extends World { public Maze(int[][] labyrinth) { super(labyrinth); - // TODO + + this.animals = new ArrayList<>(); + this.animalHistory = new ArrayList<>(); } @Override public boolean isSolved() { - // TODO - return false; + return this.animals.isEmpty(); } @Override public List getAnimals() { - // TODO - return new ArrayList(); + return this.animals; } /** @@ -43,18 +48,18 @@ public final class Maze extends World { */ public boolean hasAnimal(Animal a) { - // TODO - return false; + return this.animals.contains(a); } /** - * Adds an animal to the maze. + * Adds an animal to the maze at the start position. * * @param a The animal to add */ public void addAnimal(Animal a) { - // TODO + a.setPosition(this.getStart()); + this.animals.add(a); } /** @@ -64,11 +69,14 @@ public final class Maze extends World { */ public void removeAnimal(Animal a) { - // TODO + boolean contained = this.animals.remove(a); + if (contained) this.animalHistory.add(a); } @Override public void reset() { - // TODO + for (Animal a : this.animals) this.removeAnimal(a); + for (Animal a : this.animalHistory) this.addAnimal(a.copy()); + this.animalHistory.clear(); } } -- cgit v1.2.3