summaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/maze/physical/Daedalus.java46
-rw-r--r--src/ch/epfl/maze/physical/Maze.java16
-rw-r--r--src/ch/epfl/maze/physical/World.java16
3 files changed, 57 insertions, 21 deletions
diff --git a/src/ch/epfl/maze/physical/Daedalus.java b/src/ch/epfl/maze/physical/Daedalus.java
index 4670064..0179156 100644
--- a/src/ch/epfl/maze/physical/Daedalus.java
+++ b/src/ch/epfl/maze/physical/Daedalus.java
@@ -1,7 +1,9 @@
1package ch.epfl.maze.physical; 1package ch.epfl.maze.physical;
2 2
3import java.util.ArrayList; 3import java.util.ArrayList;
4import java.util.HashSet;
4import java.util.List; 5import java.util.List;
6import java.util.Set;
5import java.util.stream.Collectors; 7import java.util.stream.Collectors;
6import java.util.stream.Stream; 8import java.util.stream.Stream;
7 9
@@ -14,11 +16,11 @@ import java.util.stream.Stream;
14 */ 16 */
15public final class Daedalus extends World { 17public final class Daedalus extends World {
16 18
17 private final List<Predator> predators; 19 private final Set<Predator> predators;
18 private final List<Predator> predatorHistory; 20 private final Set<Predator> predatorHistory;
19 21
20 private final List<Prey> preys; 22 private final Set<Prey> preys;
21 private final List<Prey> preyHistory; 23 private final Set<Prey> preyHistory;
22 24
23 /** 25 /**
24 * Constructs a Daedalus with a labyrinth structure 26 * Constructs a Daedalus with a labyrinth structure
@@ -28,11 +30,11 @@ public final class Daedalus extends World {
28 public Daedalus(int[][] labyrinth) { 30 public Daedalus(int[][] labyrinth) {
29 super(labyrinth); 31 super(labyrinth);
30 32
31 this.predators = new ArrayList<>(); 33 this.predators = new HashSet<>();
32 this.predatorHistory = new ArrayList<>(); 34 this.predatorHistory = new HashSet<>();
33 35
34 this.preys = new ArrayList<>(); 36 this.preys = new HashSet<>();
35 this.preyHistory = new ArrayList<>(); 37 this.preyHistory = new HashSet<>();
36 } 38 }
37 39
38 @Override 40 @Override
@@ -79,28 +81,48 @@ public final class Daedalus extends World {
79 } 81 }
80 82
81 @Override 83 @Override
82 public List<Animal> getAnimals() { 84 public Set<Animal> getAnimalSet() {
83 return Stream 85 return Stream
84 .concat(this.predators.stream(), this.preys.stream()) 86 .concat(this.predators.stream(), this.preys.stream())
85 .collect(Collectors.toList()); 87 .collect(Collectors.toSet());
88 }
89
90 /**
91 * Returns a copy of the set of all current predators in the daedalus.
92 *
93 * @return A set of all predators in the daedalus
94 */
95 public Set<Predator> getPredatorSet() {
96 return new HashSet<>(this.predators);
86 } 97 }
87 98
88 /** 99 /**
89 * Returns a copy of the list of all current predators in the daedalus. 100 * Returns a copy of the list of all current predators in the daedalus.
90 * 101 *
91 * @return A list of all predators in the daedalus 102 * @return A list of all predators in the daedalus
103 * @deprecated Use @code{Set<Predator> getPredatorSet()} instead
92 */ 104 */
93 public List<Predator> getPredators() { 105 public List<Predator> getPredators() {
94 return this.predators; 106 return new ArrayList<>(this.getPredatorSet());
107 }
108
109 /**
110 * Returns a copy of the set of all current preys in the daedalus.
111 *
112 * @return A set of all preys in the daedalus
113 */
114 public Set<Prey> getPreySet() {
115 return new HashSet<>(this.preys);
95 } 116 }
96 117
97 /** 118 /**
98 * Returns a copy of the list of all current preys in the daedalus. 119 * Returns a copy of the list of all current preys in the daedalus.
99 * 120 *
100 * @return A list of all preys in the daedalus 121 * @return A list of all preys in the daedalus
122 * @deprecated Use @code{Set<Prey> getPreySet()} instead
101 */ 123 */
102 public List<Prey> getPreys() { 124 public List<Prey> getPreys() {
103 return this.preys; 125 return new ArrayList<>(this.getPreySet());
104 } 126 }
105 127
106 /** 128 /**
diff --git a/src/ch/epfl/maze/physical/Maze.java b/src/ch/epfl/maze/physical/Maze.java
index d3ba645..d3273c8 100644
--- a/src/ch/epfl/maze/physical/Maze.java
+++ b/src/ch/epfl/maze/physical/Maze.java
@@ -1,7 +1,7 @@
1package ch.epfl.maze.physical; 1package ch.epfl.maze.physical;
2 2
3import java.util.ArrayList; 3import java.util.HashSet;
4import java.util.List; 4import java.util.Set;
5 5
6/** 6/**
7 * Maze in which an animal starts from a starting point and must find the exit. 7 * Maze in which an animal starts from a starting point and must find the exit.
@@ -13,8 +13,8 @@ import java.util.List;
13 */ 13 */
14public final class Maze extends World { 14public final class Maze extends World {
15 15
16 private final List<Animal> animals; 16 private final Set<Animal> animals;
17 private final List<Animal> animalHistory; 17 private final Set<Animal> animalHistory;
18 18
19 /** 19 /**
20 * Constructs a Maze with a labyrinth structure. 20 * Constructs a Maze with a labyrinth structure.
@@ -24,8 +24,8 @@ public final class Maze extends World {
24 public Maze(int[][] labyrinth) { 24 public Maze(int[][] labyrinth) {
25 super(labyrinth); 25 super(labyrinth);
26 26
27 this.animals = new ArrayList<>(); 27 this.animals = new HashSet<>();
28 this.animalHistory = new ArrayList<>(); 28 this.animalHistory = new HashSet<>();
29 } 29 }
30 30
31 @Override 31 @Override
@@ -34,8 +34,8 @@ public final class Maze extends World {
34 } 34 }
35 35
36 @Override 36 @Override
37 public List<Animal> getAnimals() { 37 public Set<Animal> getAnimalSet() {
38 return this.animals; 38 return new HashSet<>(this.animals);
39 } 39 }
40 40
41 /** 41 /**
diff --git a/src/ch/epfl/maze/physical/World.java b/src/ch/epfl/maze/physical/World.java
index 7d33472..9730876 100644
--- a/src/ch/epfl/maze/physical/World.java
+++ b/src/ch/epfl/maze/physical/World.java
@@ -5,6 +5,7 @@ import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.ArrayList; 6import java.util.ArrayList;
7import java.util.List; 7import java.util.List;
8import java.util.Set;
8 9
9/** 10/**
10 * World that is represented by a labyrinth of tiles in which an {@code Animal} 11 * World that is represented by a labyrinth of tiles in which an {@code Animal}
@@ -70,11 +71,24 @@ public abstract class World {
70 abstract public void reset(); 71 abstract public void reset();
71 72
72 /** 73 /**
74 * Returns a copy of the set of all current animals in the world.
75 *
76 * @return A set of all animals in the world
77 */
78 public Set<Animal> getAnimalSet() {
79 return null;
80 }
81
82 /**
73 * Returns a copy of the list of all current animals in the world. 83 * Returns a copy of the list of all current animals in the world.
74 * 84 *
75 * @return A list of all animals in the world 85 * @return A list of all animals in the world
86 * @implNote Not abstract for compatibility purpose (in order not to break tests)
87 * @deprecated Use getAnimalSet() instead
76 */ 88 */
77 abstract public List<Animal> getAnimals(); 89 public List<Animal> getAnimals() {
90 return new ArrayList<>(this.getAnimalSet());
91 }
78 92
79 /** 93 /**
80 * Checks in a safe way the tile number at position (x, y) in the labyrinth. 94 * Checks in a safe way the tile number at position (x, y) in the labyrinth.