diff options
author | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
commit | 655ac88f4e73b2df532a451aedf5a561ea1b0d2c (patch) | |
tree | ef6f914a465575f313e2b280bf0639d87a4cbd58 /src/ch/epfl/maze/tests | |
parent | 56279eb59ccdea48b18daa027a5095d861b4e2f4 (diff) | |
download | maze-solver-655ac88f4e73b2df532a451aedf5a561ea1b0d2c.tar.gz |
Import project structure
Diffstat (limited to 'src/ch/epfl/maze/tests')
-rw-r--r-- | src/ch/epfl/maze/tests/AnimalTest.java | 100 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/Competition.java | 163 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/DaedalusTest.java | 123 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/GhostsTest.java | 149 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/MazeTest.java | 91 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/WorldTest.java | 330 | ||||
-rw-r--r-- | src/ch/epfl/maze/tests/ZooTest.java | 209 |
7 files changed, 1165 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/tests/AnimalTest.java b/src/ch/epfl/maze/tests/AnimalTest.java new file mode 100644 index 0000000..c4c9220 --- /dev/null +++ b/src/ch/epfl/maze/tests/AnimalTest.java | |||
@@ -0,0 +1,100 @@ | |||
1 | package ch.epfl.maze.tests; | ||
2 | |||
3 | import junit.framework.TestCase; | ||
4 | |||
5 | import org.junit.Test; | ||
6 | |||
7 | import ch.epfl.maze.physical.Animal; | ||
8 | import ch.epfl.maze.util.Direction; | ||
9 | import ch.epfl.maze.util.Vector2D; | ||
10 | |||
11 | /** | ||
12 | * Test case for {@code Animal} implementation. | ||
13 | * | ||
14 | */ | ||
15 | |||
16 | public class AnimalTest extends TestCase { | ||
17 | |||
18 | /** | ||
19 | * Test case for {@code getPosition()}. | ||
20 | */ | ||
21 | |||
22 | @Test | ||
23 | public void testGetPosition() { | ||
24 | Animal animal = new MockAnimal(new Vector2D(2, 1)); | ||
25 | |||
26 | // checks getPosition() | ||
27 | assertEquals(new Vector2D(2, 1), animal.getPosition()); | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Test case for {@code setPosition(Vector2D position)}. | ||
32 | */ | ||
33 | |||
34 | @Test | ||
35 | public void testSetPosition() { | ||
36 | Animal animal = new MockAnimal(new Vector2D(2, 1)); | ||
37 | |||
38 | // checks setPosition(Vector2D position) | ||
39 | animal.setPosition(new Vector2D(3, 5)); | ||
40 | assertEquals(new Vector2D(3, 5), animal.getPosition()); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Test case for {@code update(Direction dir)}. | ||
45 | */ | ||
46 | |||
47 | @Test | ||
48 | public void testUpdate() { | ||
49 | Animal animal = new MockAnimal(new Vector2D(2, 1)); | ||
50 | |||
51 | // checks update(Direction dir) with NONE | ||
52 | animal.update(Direction.NONE); | ||
53 | assertEquals(new Vector2D(2, 1), animal.getPosition()); | ||
54 | |||
55 | // checks update(Direction dir) with DOWN | ||
56 | animal.update(Direction.DOWN); | ||
57 | assertEquals(new Vector2D(2, 2), animal.getPosition()); | ||
58 | |||
59 | // checks update(Direction dir) with UP | ||
60 | animal.update(Direction.UP); | ||
61 | assertEquals(new Vector2D(2, 1), animal.getPosition()); | ||
62 | |||
63 | // checks update(Direction dir) with RIGHT | ||
64 | animal.update(Direction.RIGHT); | ||
65 | assertEquals(new Vector2D(3, 1), animal.getPosition()); | ||
66 | |||
67 | // checks update(Direction dir) with LEFT | ||
68 | animal.update(Direction.LEFT); | ||
69 | assertEquals(new Vector2D(2, 1), animal.getPosition()); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Mock class that makes {@code Animal} concrete. | ||
74 | * | ||
75 | */ | ||
76 | |||
77 | private class MockAnimal extends Animal { | ||
78 | |||
79 | /** | ||
80 | * Creates a concrete instance of the {@code Animal} class. | ||
81 | * | ||
82 | * @param labyrinth | ||
83 | * Actual maze | ||
84 | */ | ||
85 | |||
86 | public MockAnimal(Vector2D position) { | ||
87 | super(position); | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public Direction move(Direction[] choices) { | ||
92 | return null; | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public Animal copy() { | ||
97 | return null; | ||
98 | } | ||
99 | } | ||
100 | } | ||
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 @@ | |||
1 | package ch.epfl.maze.tests; | ||
2 | |||
3 | import static org.junit.Assert.assertTrue; | ||
4 | |||
5 | import java.util.HashMap; | ||
6 | import java.util.List; | ||
7 | import java.util.Map; | ||
8 | |||
9 | import org.junit.BeforeClass; | ||
10 | import org.junit.Test; | ||
11 | |||
12 | import ch.epfl.maze.physical.Animal; | ||
13 | import ch.epfl.maze.physical.Maze; | ||
14 | import ch.epfl.maze.physical.zoo.Bear; | ||
15 | import ch.epfl.maze.physical.zoo.Hamster; | ||
16 | import ch.epfl.maze.physical.zoo.Monkey; | ||
17 | import ch.epfl.maze.physical.zoo.Mouse; | ||
18 | import ch.epfl.maze.physical.zoo.Panda; | ||
19 | import ch.epfl.maze.physical.zoo.SpaceInvader; | ||
20 | import ch.epfl.maze.simulation.MazeSimulation; | ||
21 | import ch.epfl.maze.simulation.Simulation; | ||
22 | import ch.epfl.maze.util.LabyrinthGenerator; | ||
23 | |||
24 | /** | ||
25 | * Competition of the {@code SpaceInvader} against the other animals. | ||
26 | * <p> | ||
27 | * The rules are the following : | ||
28 | * <ul> | ||
29 | * <li>The {@code SpaceInvader} will be confronted to every animal at the same | ||
30 | * time in one simulation.</li> | ||
31 | * <li>The result will be the outcomes of 1,000 rounds.</li> | ||
32 | * <li>If the {@code SpaceInvader} exits the maze earlier than one animal, it | ||
33 | * will earn 1 point in the rivalry against it. If it exits later, then it will | ||
34 | * lose 1 point in this rivalry. In case of draw, it will earn no points.</li> | ||
35 | * <li>After the 1,000 simulations, the {@code SpaceInvader} will be declared | ||
36 | * winner against one animal if it obtained a positive score in the rivalry | ||
37 | * against it.</li> | ||
38 | * </ul> | ||
39 | * | ||
40 | */ | ||
41 | |||
42 | public class Competition { | ||
43 | |||
44 | static final String COMPETITION_MAZE_FILE = "labyrinth.txt"; | ||
45 | static final int NUMBER_OF_ROUNDS = 1000; | ||
46 | static HashMap<String, Integer> rivalries; | ||
47 | |||
48 | /** | ||
49 | * Launches the competition between the {@code SpaceInvader} and the other | ||
50 | * animals. | ||
51 | */ | ||
52 | |||
53 | @BeforeClass | ||
54 | public static void setUpClass() { | ||
55 | int[][] labyrinth = LabyrinthGenerator.readFromFile(COMPETITION_MAZE_FILE); | ||
56 | Maze m = new Maze(labyrinth); | ||
57 | Simulation simulation = new MazeSimulation(m); | ||
58 | |||
59 | // adds a Mouse | ||
60 | m.addAnimal(new Mouse(m.getStart())); | ||
61 | |||
62 | // adds a Monkey | ||
63 | m.addAnimal(new Monkey(m.getStart())); | ||
64 | |||
65 | // adds a Hamster | ||
66 | m.addAnimal(new Hamster(m.getStart())); | ||
67 | |||
68 | // adds a Bear (if coded) | ||
69 | // m.addAnimal(new Bear(m.getStart())); | ||
70 | |||
71 | // adds a Panda | ||
72 | m.addAnimal(new Panda(m.getStart())); | ||
73 | |||
74 | // adds a Space Invader | ||
75 | m.addAnimal(new SpaceInvader(m.getStart())); | ||
76 | |||
77 | rivalries = new HashMap<String, Integer>(); | ||
78 | rivalries.put("Mouse", 0); | ||
79 | rivalries.put("Hamster", 0); | ||
80 | rivalries.put("Monkey", 0); | ||
81 | //rivalries.put("Bear", 0); // if coded | ||
82 | rivalries.put("Panda", 0); | ||
83 | |||
84 | System.out.print("Launching competition, please wait... "); | ||
85 | for (int i = 0; i < NUMBER_OF_ROUNDS; i++) { | ||
86 | simulation.restart(); | ||
87 | while (!simulation.isOver()) { | ||
88 | simulation.move(null); | ||
89 | } | ||
90 | |||
91 | int result = MazeSimulation.COUNTER_LIMIT; | ||
92 | Map<Integer, List<Animal>> arrivalTimes = simulation.getArrivalTimes(); | ||
93 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { | ||
94 | for (Animal a : entry.getValue()) { | ||
95 | if (a.getClass() == SpaceInvader.class) { | ||
96 | result = entry.getKey(); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { | ||
102 | for (Animal a : entry.getValue()) { | ||
103 | String animalName = a.getClass().getSimpleName(); | ||
104 | if (!"SpaceInvader".equals(animalName)) { | ||
105 | int score = rivalries.get(animalName); | ||
106 | int adversary = entry.getKey(); | ||
107 | if (adversary < result) { | ||
108 | rivalries.put(animalName, --score); | ||
109 | } else if (adversary > result) { | ||
110 | rivalries.put(animalName, ++score); | ||