blob: bff87919de2616ae00cdc71bce227cab1b348b8c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package ch.epfl.maze.physical;
import java.util.ArrayList;
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<Animal> animals;
private final List<Animal> 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 ArrayList<>();
this.animalHistory = new ArrayList<>();
}
@Override
public boolean isSolved() {
return this.animals.isEmpty();
}
@Override
public List<Animal> getAnimals() {
return this.animals;
}
/**
* Determines if the maze contains an animal.
*
* @param a The animal in question
* @return <b>true</b> if the animal belongs to the world, <b>false</b>
* 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();
}
}
|