From 093fd685341c334be63ec950e44aa3a1edc82915 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 26 Nov 2015 10:28:22 +0100 Subject: Refactor main programs --- src/ch/epfl/maze/main/Console.java | 81 +--------------- src/ch/epfl/maze/main/Program.java | 83 ++-------------- src/ch/epfl/maze/util/SimulationGenerator.java | 128 +++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 153 deletions(-) create mode 100644 src/ch/epfl/maze/util/SimulationGenerator.java diff --git a/src/ch/epfl/maze/main/Console.java b/src/ch/epfl/maze/main/Console.java index 0c273e1..ebe1e3e 100644 --- a/src/ch/epfl/maze/main/Console.java +++ b/src/ch/epfl/maze/main/Console.java @@ -1,18 +1,8 @@ package ch.epfl.maze.main; -import ch.epfl.maze.physical.Daedalus; -import ch.epfl.maze.physical.Maze; -import ch.epfl.maze.physical.pacman.*; -import ch.epfl.maze.physical.zoo.Hamster; -import ch.epfl.maze.physical.zoo.Monkey; -import ch.epfl.maze.physical.zoo.Mouse; -import ch.epfl.maze.physical.zoo.Panda; -import ch.epfl.maze.simulation.DaedalusSimulation; -import ch.epfl.maze.simulation.MazeSimulation; import ch.epfl.maze.simulation.Simulation; -import ch.epfl.maze.util.LabyrinthGenerator; +import ch.epfl.maze.util.SimulationGenerator; import ch.epfl.maze.util.Statistics; -import ch.epfl.maze.util.Vector2D; import java.util.Collections; import java.util.List; @@ -30,12 +20,11 @@ public class Console { * Number of simulations launched. */ public static final int NUMBER_OF_SIMULATIONS = 1000; + public static final String DEFAULT_SIM = "maze"; public static void main(String[] args) { - Simulation simulation; - - simulation = getMazeSimulation(); - //simulation = getDaedalusSimulation(); + String simName = args.length > 0 ? args[0] : DEFAULT_SIM; + Simulation simulation = SimulationGenerator.getSimulation(simName); System.out.print("Launching " + NUMBER_OF_SIMULATIONS + " simulations..."); Map> results = @@ -45,68 +34,6 @@ public class Console { printStats(results); } - /** - * Creates a {@code MazeSimulation} suitable for statistics. - *

- * Note that there should be only ONE animal of each kind in the - * corresponding {@code Maze}. - * - * @return A {@code MazeSimulation} suitable for statistics - */ - public static Simulation getMazeSimulation() { - int[][] labyrinth = LabyrinthGenerator.getMedium(); - Maze m = new Maze(labyrinth); - Simulation simulation = new MazeSimulation(m); - - // adds a Mouse - m.addAnimal(new Mouse(m.getStart())); - - // adds a Monkey - m.addAnimal(new Monkey(m.getStart())); - - // adds a Hamster - m.addAnimal(new Hamster(m.getStart())); - - // adds a Bear (if this bonus is coded) - // m.addAnimal(new Bear(m.getStart())); - - // adds a Panda - m.addAnimal(new Panda(m.getStart())); - - return simulation; - } - - /** - * Creates a {@code DaedalusSimulation} suitable for statistics. - *

- * Note that there should be only ONE animal of each kind in the - * corresponding {@code Daedalus}. - * - * @return A {@code DaedalusSimulation} suitable for statistics - */ - public static Simulation getDaedalusSimulation() { - int[][] labyrinth = LabyrinthGenerator.getPacMan(); - Daedalus d = new Daedalus(labyrinth); - Simulation simulation = new DaedalusSimulation(d); - - // adds Pac-Man - d.addPrey(new PacMan(new Vector2D(9, 15))); - - // adds Blinky - d.addPredator(new Blinky(new Vector2D(17, 1))); - - // adds Pinky - d.addPredator(new Pinky(new Vector2D(1, 1))); - - // adds Inky - d.addPredator(new Inky(new Vector2D(17, 17))); - - // adds Clyde - d.addPredator(new Clyde(new Vector2D(1, 17))); - - return simulation; - } - /** * Pretty-prints the statistics computed in the parameters. * diff --git a/src/ch/epfl/maze/main/Program.java b/src/ch/epfl/maze/main/Program.java index 347eace..4692d2a 100644 --- a/src/ch/epfl/maze/main/Program.java +++ b/src/ch/epfl/maze/main/Program.java @@ -1,18 +1,8 @@ package ch.epfl.maze.main; import ch.epfl.maze.graphics.Display; -import ch.epfl.maze.physical.Daedalus; -import ch.epfl.maze.physical.Maze; -import ch.epfl.maze.physical.pacman.*; -import ch.epfl.maze.physical.zoo.Hamster; -import ch.epfl.maze.physical.zoo.Monkey; -import ch.epfl.maze.physical.zoo.Mouse; -import ch.epfl.maze.physical.zoo.Panda; -import ch.epfl.maze.simulation.DaedalusSimulation; -import ch.epfl.maze.simulation.MazeSimulation; import ch.epfl.maze.simulation.Simulation; -import ch.epfl.maze.util.LabyrinthGenerator; -import ch.epfl.maze.util.Vector2D; +import ch.epfl.maze.util.SimulationGenerator; /** * Mini-project main program that will run the simulations on a {@code Display}. @@ -21,79 +11,20 @@ import ch.epfl.maze.util.Vector2D; */ public class Program { + public static final String DEFAULT_SIM = "maze"; + /** * Runs one of the two available simulations * - * @see #getMazeSimulation() - * @see #getDaedalusSimulation() + * @see ch.epfl.maze.util.SimulationGenerator#getMazeSimulation() + * @see ch.epfl.maze.util.SimulationGenerator#getMultiPreyDaedalusSimulation() */ public static void main(String[] args) { - Simulation simulation; - - simulation = getMazeSimulation(); - //simulation = getDaedalusSimulation(); + String simName = args.length > 0 ? args[0] : DEFAULT_SIM; + Simulation simulation = SimulationGenerator.getSimulation(simName); Display display = new Display(simulation); display.run(); } - /** - * Creates a {@code MazeSimulation} with every animal implementations. - * - * @return A {@code MazeSimulation} to display - */ - public static Simulation getMazeSimulation() { - int[][] labyrinth = LabyrinthGenerator.getMedium(); - Maze m = new Maze(labyrinth); - Simulation simulation = new MazeSimulation(m); - - // adds a Mouse - m.addAnimal(new Mouse(m.getStart())); - - // adds a Monkey - m.addAnimal(new Monkey(m.getStart())); - - // adds a Hamster - m.addAnimal(new Hamster(m.getStart())); - - // adds a Bear (if this bonus is coded) - //m.addAnimal(new Bear(m.getStart())); - - // adds a Panda - m.addAnimal(new Panda(m.getStart())); - - return simulation; - } - - /** - * Creates a {@code DaedalusSimulation} with every ghost implementation and - * 3 Pac-Mans. - * - * @return A {@code DaedalusSimulation} to display - */ - public static Simulation getDaedalusSimulation() { - int[][] labyrinth = LabyrinthGenerator.getPacMan(); - Daedalus d = new Daedalus(labyrinth); - Simulation simulation = new DaedalusSimulation(d); - - // adds Pac-Mans - d.addPrey(new PacMan(new Vector2D(9, 15))); - d.addPrey(new PacMan(new Vector2D(10, 15))); - d.addPrey(new PacMan(new Vector2D(8, 15))); - - // adds Blinky - d.addPredator(new Blinky(new Vector2D(17, 1))); - - // adds Pinky - d.addPredator(new Pinky(new Vector2D(1, 1))); - - // adds Inky - d.addPredator(new Inky(new Vector2D(17, 17))); - - // adds Clyde - d.addPredator(new Clyde(new Vector2D(1, 17))); - - return simulation; - } - } diff --git a/src/ch/epfl/maze/util/SimulationGenerator.java b/src/ch/epfl/maze/util/SimulationGenerator.java new file mode 100644 index 0000000..6d7bbcb --- /dev/null +++ b/src/ch/epfl/maze/util/SimulationGenerator.java @@ -0,0 +1,128 @@ +package ch.epfl.maze.util; + +import ch.epfl.maze.physical.Daedalus; +import ch.epfl.maze.physical.Maze; +import ch.epfl.maze.physical.pacman.*; +import ch.epfl.maze.physical.zoo.*; +import ch.epfl.maze.simulation.DaedalusSimulation; +import ch.epfl.maze.simulation.MazeSimulation; +import ch.epfl.maze.simulation.Simulation; + +/** + * Generates a set of pre-computed labyrinth structures + * + * @author EPFL + * @author Pacien TRAN-GIRARD + */ +public final class SimulationGenerator { + + /** + * Returns the simulation with the given name. + * + * @param name The name + * @return The Simulation + */ + public static Simulation getSimulation(String name) { + switch (name) { + case "maze": + return SimulationGenerator.getMazeSimulation(); + case "daedalus": + return SimulationGenerator.getDaedalusSimulation(); + case "multi-daedalus": + return SimulationGenerator.getMultiPreyDaedalusSimulation(); + default: + return null; + } + } + + /** + * Creates a {@code MazeSimulation} with every animal implementations. + * + * @return A {@code MazeSimulation} to display + */ + public static Simulation getMazeSimulation() { + int[][] labyrinth = LabyrinthGenerator.getMedium(); + Maze m = new Maze(labyrinth); + Simulation simulation = new MazeSimulation(m); + + // adds a Mouse + m.addAnimal(new Mouse(m.getStart())); + + // adds a Monkey + m.addAnimal(new Monkey(m.getStart())); + + // adds a Hamster + m.addAnimal(new Hamster(m.getStart())); + + // adds a Bear (if this bonus is coded) + m.addAnimal(new Bear(m.getStart())); + + // adds a Panda + m.addAnimal(new Panda(m.getStart())); + + return simulation; + } + + /** + * Creates a {@code DaedalusSimulation} suitable for statistics. + *

+ * Note that there should be only ONE animal of each kind in the + * corresponding {@code Daedalus}. + * + * @return A {@code DaedalusSimulation} suitable for statistics + */ + public static Simulation getDaedalusSimulation() { + int[][] labyrinth = LabyrinthGenerator.getPacMan(); + Daedalus d = new Daedalus(labyrinth); + Simulation simulation = new DaedalusSimulation(d); + + // adds Pac-Man + d.addPrey(new PacMan(new Vector2D(9, 15))); + + // adds Blinky + d.addPredator(new Blinky(new Vector2D(17, 1))); + + // adds Pinky + d.addPredator(new Pinky(new Vector2D(1, 1))); + + // adds Inky + d.addPredator(new Inky(new Vector2D(17, 17))); + + // adds Clyde + d.addPredator(new Clyde(new Vector2D(1, 17))); + + return simulation; + } + + /** + * Creates a {@code DaedalusSimulation} with every ghost implementation and + * 3 Pac-Mans. + * + * @return A {@code DaedalusSimulation} to display + */ + public static Simulation getMultiPreyDaedalusSimulation() { + int[][] labyrinth = LabyrinthGenerator.getPacMan(); + Daedalus d = new Daedalus(labyrinth); + Simulation simulation = new DaedalusSimulation(d); + + // adds Pac-Mans + d.addPrey(new PacMan(new Vector2D(9, 15))); + d.addPrey(new PacMan(new Vector2D(10, 15))); + d.addPrey(new PacMan(new Vector2D(8, 15))); + + // adds Blinky + d.addPredator(new Blinky(new Vector2D(17, 1))); + + // adds Pinky + d.addPredator(new Pinky(new Vector2D(1, 1))); + + // adds Inky + d.addPredator(new Inky(new Vector2D(17, 17))); + + // adds Clyde + d.addPredator(new Clyde(new Vector2D(1, 17))); + + return simulation; + } + +} -- cgit v1.2.3