package ch.epfl.maze.physical; import java.util.HashSet; import java.util.Set; /** * 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 EPFL * @author Pacien TRAN-GIRARD */ public final class Maze extends World { private final Set animals; private final Set animalHistory; /** * Constructs a Maze with a labyrinth structure. * * @param labyrinth Structure of the labyrinth, an NxM array of tiles */ public Maze(int[][] labyrinth) { super(labyrinth); this.animals = new HashSet<>(); this.animalHistory = new HashSet<>(); } @Override public boolean isSolved() { return this.animals.isEmpty(); } @Override public Set getAnimalSet() { return new HashSet<>(this.animals); } /** * Determines if the maze contains an animal. * * @param a The animal in question * @return true if the animal belongs to the world, false * otherwise. */ public boolean hasAnimal(Animal a) { return this.animals.contains(a); } /** * Adds an animal to the maze at the start position. * * @param a The animal to add */ public void addAnimal(Animal a) { a.setPosition(this.getStart()); this.animals.add(a); } /** * Removes an animal from the maze. * * @param a The animal to remove */ public void removeAnimal(Animal a) { boolean contained = this.animals.remove(a); if (contained) this.animalHistory.add(a); } @Override public void reset() { this.animalHistory.addAll(this.animals); this.animals.clear(); this.animalHistory.forEach(a -> this.addAnimal(a.copy())); this.animalHistory.clear(); } }