From 62b603d3d74bdeff56811fae08e4f47136b7aea2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 22 Nov 2015 16:48:50 +0100 Subject: Implement Daedalus --- src/ch/epfl/maze/physical/Daedalus.java | 64 ++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 28 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/Daedalus.java b/src/ch/epfl/maze/physical/Daedalus.java index 2edad7b..8018b66 100644 --- a/src/ch/epfl/maze/physical/Daedalus.java +++ b/src/ch/epfl/maze/physical/Daedalus.java @@ -2,29 +2,41 @@ package ch.epfl.maze.physical; import java.util.ArrayList; import java.util.List; +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 Pacien TRAN-GIRARD */ - public final class Daedalus extends World { + private final List predators; + private final List predatorHistory; + + private final List preys; + private final List 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); - // TODO + + this.predators = new ArrayList<>(); + this.predatorHistory = new ArrayList<>(); + + this.preys = new ArrayList<>(); + this.preyHistory = new ArrayList<>(); } @Override public boolean isSolved() { - // TODO - return false; + return this.preys.isEmpty(); } /** @@ -32,9 +44,9 @@ public final class Daedalus extends World { * * @param p The predator to add */ - public void addPredator(Predator p) { - // TODO + this.predators.add(p); + this.predatorHistory.add((Predator) p.copy()); } /** @@ -42,9 +54,9 @@ public final class Daedalus extends World { * * @param p The prey to add */ - public void addPrey(Prey p) { - // TODO + this.preys.add(p); + this.preyHistory.add((Prey) p.copy()); } /** @@ -52,9 +64,8 @@ public final class Daedalus extends World { * * @param p The predator to remove */ - public void removePredator(Predator p) { - // TODO + this.predators.remove(p); } /** @@ -62,15 +73,15 @@ public final class Daedalus extends World { * * @param p The prey to remove */ - public void removePrey(Prey p) { - // TODO + this.preys.remove(p); } @Override public List getAnimals() { - // TODO - return null; + return Stream + .concat(this.predators.stream(), this.preys.stream()) + .collect(Collectors.toList()); } /** @@ -78,10 +89,8 @@ public final class Daedalus extends World { * * @return A list of all predators in the daedalus */ - public List getPredators() { - // TODO - return new ArrayList(); + return this.predators; } /** @@ -89,10 +98,8 @@ public final class Daedalus extends World { * * @return A list of all preys in the daedalus */ - public List getPreys() { - // TODO - return new ArrayList(); + return this.preys; } /** @@ -102,10 +109,8 @@ public final class Daedalus extends World { * @return true if the predator belongs to the world, false * otherwise. */ - public boolean hasPredator(Predator p) { - // TODO - return false; + return this.predators.contains(p); } /** @@ -115,14 +120,17 @@ public final class Daedalus extends World { * @return true if the prey belongs to the world, false * otherwise. */ - public boolean hasPrey(Prey p) { - // TODO - return false; + return this.preys.contains(p); } @Override public void reset() { - // TODO + this.predators.clear(); + this.predators.addAll(this.predatorHistory); + + this.preys.clear(); + this.preys.addAll(this.preyHistory); } + } -- cgit v1.2.3