summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/Daedalus.java
blob: 017915628bb42c05a54b533cfc3b59386175f16f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ch.epfl.maze.physical;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Daedalus in which predators hunt preys. Once a prey has been caught by a
 * predator, it will be removed from the daedalus.
 *
 * @author EPFL
 * @author Pacien TRAN-GIRARD
 */
public final class Daedalus extends World {

    private final Set<Predator> predators;
    private final Set<Predator> predatorHistory;

    private final Set<Prey> preys;
    private final Set<Prey> preyHistory;

    /**
     * Constructs a Daedalus with a labyrinth structure
     *
     * @param labyrinth Structure of the labyrinth, an NxM array of tiles
     */
    public Daedalus(int[][] labyrinth) {
        super(labyrinth);

        this.predators = new HashSet<>();
        this.predatorHistory = new HashSet<>();

        this.preys = new HashSet<>();
        this.preyHistory = new HashSet<>();
    }

    @Override
    public boolean isSolved() {
        return this.preys.isEmpty();
    }

    /**
     * Adds a predator to the daedalus.
     *
     * @param p The predator to add
     */
    public void addPredator(Predator p) {
        this.predators.add(p);
        this.predatorHistory.add((Predator) p.copy());
    }

    /**
     * Adds a prey to the daedalus.
     *
     * @param p The prey to add
     */
    public void addPrey(Prey p) {
        this.preys.add(p);
        this.preyHistory.add((Prey) p.copy());
    }

    /**
     * Removes a predator from the daedalus.
     *
     * @param p The predator to remove
     */
    public void removePredator(Predator p) {
        this.predators.remove(p);
    }

    /**
     * Removes a prey from the daedalus.
     *
     * @param p The prey to remove
     */
    public void removePrey(Prey p) {
        this.preys.remove(p);
    }

    @Override
    public Set<Animal> getAnimalSet() {
        return Stream
                .concat(this.predators.stream(), this.preys.stream())
                .collect(Collectors.toSet());
    }

    /**
     * Returns a copy of the set of all current predators in the daedalus.
     *
     * @return A set of all predators in the daedalus
     */
    public Set<Predator> getPredatorSet() {
        return new HashSet<>(this.predators);
    }

    /**
     * Returns a copy of the list of all current predators in the daedalus.
     *
     * @return A list of all predators in the daedalus
     * @deprecated Use @code{Set<Predator> getPredatorSet()} instead
     */
    public List<Predator> getPredators() {
        return new ArrayList<>(this.getPredatorSet());
    }

    /**
     * Returns a copy of the set of all current preys in the daedalus.
     *
     * @return A set of all preys in the daedalus
     */
    public Set<Prey> getPreySet() {
        return new HashSet<>(this.preys);
    }

    /**
     * Returns a copy of the list of all current preys in the daedalus.
     *
     * @return A list of all preys in the daedalus
     * @deprecated Use @code{Set<Prey> getPreySet()} instead
     */
    public List<Prey> getPreys() {
        return new ArrayList<>(this.getPreySet());
    }

    /**
     * Determines if the daedalus contains a predator.
     *
     * @param p The predator in question
     * @return <b>true</b> if the predator belongs to the world, <b>false</b>
     * otherwise.
     */
    public boolean hasPredator(Predator p) {
        return this.predators.contains(p);
    }

    /**
     * Determines if the daedalus contains a prey.
     *
     * @param p The prey in question
     * @return <b>true</b> if the prey belongs to the world, <b>false</b>
     * otherwise.
     */
    public boolean hasPrey(Prey p) {
        return this.preys.contains(p);
    }

    @Override
    public void reset() {
        this.predators.clear();
        this.predators.addAll(this.predatorHistory);

        this.preys.clear();
        this.preys.addAll(this.preyHistory);
    }

}