diff options
author | Pacien TRAN-GIRARD | 2015-11-21 10:44:42 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 10:44:42 +0100 |
commit | 90bd766f361083f6fd9e39ff080c83fcc606832f (patch) | |
tree | df81a931c9565a4b227068680087f58fdace1859 /src/ch/epfl/maze/tests/Competition.java | |
parent | 655ac88f4e73b2df532a451aedf5a561ea1b0d2c (diff) | |
download | maze-solver-90bd766f361083f6fd9e39ff080c83fcc606832f.tar.gz |
Reformat imported code
Diffstat (limited to 'src/ch/epfl/maze/tests/Competition.java')
-rw-r--r-- | src/ch/epfl/maze/tests/Competition.java | 263 |
1 files changed, 128 insertions, 135 deletions
diff --git a/src/ch/epfl/maze/tests/Competition.java b/src/ch/epfl/maze/tests/Competition.java index 4ed42b6..ccbbe73 100644 --- a/src/ch/epfl/maze/tests/Competition.java +++ b/src/ch/epfl/maze/tests/Competition.java | |||
@@ -1,25 +1,19 @@ | |||
1 | package ch.epfl.maze.tests; | 1 | package ch.epfl.maze.tests; |
2 | 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; | 3 | import ch.epfl.maze.physical.Animal; |
13 | import ch.epfl.maze.physical.Maze; | 4 | import ch.epfl.maze.physical.Maze; |
14 | import ch.epfl.maze.physical.zoo.Bear; | 5 | import ch.epfl.maze.physical.zoo.*; |
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; | 6 | import ch.epfl.maze.simulation.MazeSimulation; |
21 | import ch.epfl.maze.simulation.Simulation; | 7 | import ch.epfl.maze.simulation.Simulation; |
22 | import ch.epfl.maze.util.LabyrinthGenerator; | 8 | import ch.epfl.maze.util.LabyrinthGenerator; |
9 | import org.junit.BeforeClass; | ||
10 | import org.junit.Test; | ||
11 | |||
12 | import java.util.HashMap; | ||
13 | import java.util.List; | ||
14 | import java.util.Map; | ||
15 | |||
16 | import static org.junit.Assert.assertTrue; | ||
23 | 17 | ||
24 | /** | 18 | /** |
25 | * Competition of the {@code SpaceInvader} against the other animals. | 19 | * Competition of the {@code SpaceInvader} against the other animals. |
@@ -36,128 +30,127 @@ import ch.epfl.maze.util.LabyrinthGenerator; | |||
36 | * winner against one animal if it obtained a positive score in the rivalry | 30 | * winner against one animal if it obtained a positive score in the rivalry |
37 | * against it.</li> | 31 | * against it.</li> |
38 | * </ul> | 32 | * </ul> |
39 | * | ||
40 | */ | 33 | */ |
41 | 34 | ||
42 | public class Competition { | 35 | public class Competition { |
43 | 36 | ||
44 | static final String COMPETITION_MAZE_FILE = "labyrinth.txt"; | 37 | static final String COMPETITION_MAZE_FILE = "labyrinth.txt"; |
45 | static final int NUMBER_OF_ROUNDS = 1000; | 38 | static final int NUMBER_OF_ROUNDS = 1000; |
46 | static HashMap<String, Integer> rivalries; | 39 | static HashMap<String, Integer> rivalries; |
47 | 40 | ||
48 | /** | 41 | /** |
49 | * Launches the competition between the {@code SpaceInvader} and the other | 42 | * Launches the competition between the {@code SpaceInvader} and the other |
50 | * animals. | 43 | * animals. |
51 | */ | 44 | */ |
52 | 45 | ||
53 | @BeforeClass | 46 | @BeforeClass |
54 | public static void setUpClass() { | 47 | public static void setUpClass() { |
55 | int[][] labyrinth = LabyrinthGenerator.readFromFile(COMPETITION_MAZE_FILE); | 48 | int[][] labyrinth = LabyrinthGenerator.readFromFile(COMPETITION_MAZE_FILE); |
56 | Maze m = new Maze(labyrinth); | 49 | Maze m = new Maze(labyrinth); |
57 | Simulation simulation = new MazeSimulation(m); | 50 | Simulation simulation = new MazeSimulation(m); |
58 | 51 | ||
59 | // adds a Mouse | 52 | // adds a Mouse |
60 | m.addAnimal(new Mouse(m.getStart())); | 53 | m.addAnimal(new Mouse(m.getStart())); |
61 | 54 | ||
62 | // adds a Monkey | 55 | // adds a Monkey |
63 | m.addAnimal(new Monkey(m.getStart())); | 56 | m.addAnimal(new Monkey(m.getStart())); |
64 | 57 | ||
65 | // adds a Hamster | 58 | // adds a Hamster |
66 | m.addAnimal(new Hamster(m.getStart())); | 59 | m.addAnimal(new Hamster(m.getStart())); |
67 | 60 | ||
68 | // adds a Bear (if coded) | 61 | // adds a Bear (if coded) |
69 | // m.addAnimal(new Bear(m.getStart())); | 62 | // m.addAnimal(new Bear(m.getStart())); |
70 | 63 | ||
71 | // adds a Panda | 64 | // adds a Panda |
72 | m.addAnimal(new Panda(m.getStart())); | 65 | m.addAnimal(new Panda(m.getStart())); |
73 | 66 | ||
74 | // adds a Space Invader | 67 | // adds a Space Invader |
75 | m.addAnimal(new SpaceInvader(m.getStart())); | 68 | m.addAnimal(new SpaceInvader(m.getStart())); |
76 | 69 | ||
77 | rivalries = new HashMap<String, Integer>(); | 70 | rivalries = new HashMap<String, Integer>(); |
78 | rivalries.put("Mouse", 0); | 71 | rivalries.put("Mouse", 0); |
79 | rivalries.put("Hamster", 0); | 72 | rivalries.put("Hamster", 0); |
80 | rivalries.put("Monkey", 0); | 73 | rivalries.put("Monkey", 0); |
81 | //rivalries.put("Bear", 0); // if coded | 74 | //rivalries.put("Bear", 0); // if coded |
82 | rivalries.put("Panda", 0); | 75 | rivalries.put("Panda", 0); |
83 | 76 | ||
84 | System.out.print("Launching competition, please wait... "); | 77 | System.out.print("Launching competition, please wait... "); |
85 | for (int i = 0; i < NUMBER_OF_ROUNDS; i++) { | 78 | for (int i = 0; i < NUMBER_OF_ROUNDS; i++) { |
86 | simulation.restart(); | 79 | simulation.restart(); |
87 | while (!simulation.isOver()) { | 80 | while (!simulation.isOver()) { |
88 | simulation.move(null); | 81 | simulation.move(null); |
89 | } | 82 | } |
90 | 83 | ||
91 | int result = MazeSimulation.COUNTER_LIMIT; | 84 | int result = MazeSimulation.COUNTER_LIMIT; |
92 | Map<Integer, List<Animal>> arrivalTimes = simulation.getArrivalTimes(); | 85 | Map<Integer, List<Animal>> arrivalTimes = simulation.getArrivalTimes(); |
93 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { | 86 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { |
94 | for (Animal a : entry.getValue()) { | 87 | for (Animal a : entry.getValue()) { |
95 | if (a.getClass() == SpaceInvader.class) { | 88 | if (a.getClass() == SpaceInvader.class) { |
96 | result = entry.getKey(); | 89 | result = entry.getKey(); |
97 | } | 90 | } |
98 | } | 91 | } |
99 | } | 92 | } |
100 | 93 | ||
101 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { | 94 | for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { |
102 | for (Animal a : entry.getValue()) { | 95 | for (Animal a : entry.getValue()) { |
103 | String animalName = a.getClass().getSimpleName(); | 96 | String animalName = a.getClass().getSimpleName(); |
104 | if (!"SpaceInvader".equals(animalName)) { | 97 | if (!"SpaceInvader".equals(animalName)) { |
105 | int score = rivalries.get(animalName); | 98 | int score = rivalries.get(animalName); |
106 | int adversary = entry.getKey(); | 99 | int adversary = entry.getKey(); |
107 | if (adversary < result) { | 100 | if (adversary < result) { |
108 | rivalries.put(animalName, --score); | 101 | rivalries.put(animalName, --score); |
109 | } else if (adversary > result) { | 102 | } else if (adversary > result) { |
110 | rivalries.put(animalName, ++score); | 103 | rivalries.put(animalName, ++score); |
111 | } | 104 | } |
112 | } | 105 | } |
113 | } | 106 | } |
114 | } | 107 | } |
115 | } | 108 | } |
116 | System.out.println("done !"); | 109 | System.out.println("done !"); |
117 | } | 110 | } |
118 | 111 | ||
119 | /** | 112 | /** |
120 | * Determines if the {@code SpaceInvader} has beaten the {@code Mouse}. | 113 | * Determines if the {@code SpaceInvader} has beaten the {@code Mouse}. |
121 | */ | 114 | */ |
122 | 115 | ||
123 | @Test | 116 | @Test |
124 | public void testVSMouse() { | 117 | public void testVSMouse() { |
125 | assertTrue("The SpaceInvader has not beaten the Mouse", rivalries.get("Mouse") > 0); | 118 | assertTrue("The SpaceInvader has not beaten the Mouse", rivalries.get("Mouse") > 0); |
126 | } | 119 | } |
127 | 120 | ||
128 | /** | 121 | /** |
129 | * Determines if the {@code SpaceInvader} has beaten the {@code Hamster}. | 122 | * Determines if the {@code SpaceInvader} has beaten the {@code Hamster}. |
130 | */ | 123 | */ |
131 | 124 | ||
132 | @Test | 125 | @Test |
133 | public void testVSHamster() { | 126 | public void testVSHamster() { |
134 | assertTrue("The SpaceInvader has not beaten the Hamster", rivalries.get("Hamster") > 0); | 127 | assertTrue("The SpaceInvader has not beaten the Hamster", rivalries.get("Hamster") > 0); |
135 | } | 128 | } |
136 | 129 | ||
137 | /** | 130 | /** |
138 | * Determines if the {@code SpaceInvader} has beaten the {@code Monkey}. | 131 | * Determines if the {@code SpaceInvader} has beaten the {@code Monkey}. |
139 | */ | 132 | */ |
140 | 133 | ||
141 | @Test | 134 | @Test |
142 | public void testVSMonkey() { | 135 | public void testVSMonkey() { |
143 | assertTrue("The SpaceInvader has not beaten the Monkey", rivalries.get("Monkey") > 0); | 136 |