From 655ac88f4e73b2df532a451aedf5a561ea1b0d2c Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 10:36:18 +0100 Subject: Import project structure --- src/ch/epfl/maze/tests/Competition.java | 163 ++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/ch/epfl/maze/tests/Competition.java (limited to 'src/ch/epfl/maze/tests/Competition.java') diff --git a/src/ch/epfl/maze/tests/Competition.java b/src/ch/epfl/maze/tests/Competition.java new file mode 100644 index 0000000..4ed42b6 --- /dev/null +++ b/src/ch/epfl/maze/tests/Competition.java @@ -0,0 +1,163 @@ +package ch.epfl.maze.tests; + +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.BeforeClass; +import org.junit.Test; + +import ch.epfl.maze.physical.Animal; +import ch.epfl.maze.physical.Maze; +import ch.epfl.maze.physical.zoo.Bear; +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.physical.zoo.SpaceInvader; +import ch.epfl.maze.simulation.MazeSimulation; +import ch.epfl.maze.simulation.Simulation; +import ch.epfl.maze.util.LabyrinthGenerator; + +/** + * Competition of the {@code SpaceInvader} against the other animals. + *

+ * The rules are the following : + *

+ * + */ + +public class Competition { + + static final String COMPETITION_MAZE_FILE = "labyrinth.txt"; + static final int NUMBER_OF_ROUNDS = 1000; + static HashMap rivalries; + + /** + * Launches the competition between the {@code SpaceInvader} and the other + * animals. + */ + + @BeforeClass + public static void setUpClass() { + int[][] labyrinth = LabyrinthGenerator.readFromFile(COMPETITION_MAZE_FILE); + 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 coded) + // m.addAnimal(new Bear(m.getStart())); + + // adds a Panda + m.addAnimal(new Panda(m.getStart())); + + // adds a Space Invader + m.addAnimal(new SpaceInvader(m.getStart())); + + rivalries = new HashMap(); + rivalries.put("Mouse", 0); + rivalries.put("Hamster", 0); + rivalries.put("Monkey", 0); + //rivalries.put("Bear", 0); // if coded + rivalries.put("Panda", 0); + + System.out.print("Launching competition, please wait... "); + for (int i = 0; i < NUMBER_OF_ROUNDS; i++) { + simulation.restart(); + while (!simulation.isOver()) { + simulation.move(null); + } + + int result = MazeSimulation.COUNTER_LIMIT; + Map> arrivalTimes = simulation.getArrivalTimes(); + for (Map.Entry> entry : arrivalTimes.entrySet()) { + for (Animal a : entry.getValue()) { + if (a.getClass() == SpaceInvader.class) { + result = entry.getKey(); + } + } + } + + for (Map.Entry> entry : arrivalTimes.entrySet()) { + for (Animal a : entry.getValue()) { + String animalName = a.getClass().getSimpleName(); + if (!"SpaceInvader".equals(animalName)) { + int score = rivalries.get(animalName); + int adversary = entry.getKey(); + if (adversary < result) { + rivalries.put(animalName, --score); + } else if (adversary > result) { + rivalries.put(animalName, ++score); + } + } + } + } + } + System.out.println("done !"); + } + + /** + * Determines if the {@code SpaceInvader} has beaten the {@code Mouse}. + */ + + @Test + public void testVSMouse() { + assertTrue("The SpaceInvader has not beaten the Mouse", rivalries.get("Mouse") > 0); + } + + /** + * Determines if the {@code SpaceInvader} has beaten the {@code Hamster}. + */ + + @Test + public void testVSHamster() { + assertTrue("The SpaceInvader has not beaten the Hamster", rivalries.get("Hamster") > 0); + } + + /** + * Determines if the {@code SpaceInvader} has beaten the {@code Monkey}. + */ + + @Test + public void testVSMonkey() { + assertTrue("The SpaceInvader has not beaten the Monkey", rivalries.get("Monkey") > 0); + } + + /** + * Determines if the {@code SpaceInvader} has beaten the {@code Bear}. + */ + + @Test + public void testVSBear() { + assertTrue("The SpaceInvader has not beaten the Bear", rivalries.get("Bear") > 0); + } + + /** + * Determines if the {@code SpaceInvader} has beaten the {@code Panda}. + */ + + @Test + public void testVSPanda() { + assertTrue("The SpaceInvader has not beaten the Panda", rivalries.get("Panda") > 0); + } +} -- cgit v1.2.3