summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/tests/Competition.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/tests/Competition.java')
-rw-r--r--src/ch/epfl/maze/tests/Competition.java163
1 files changed, 163 insertions, 0 deletions
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 @@
1package ch.epfl.maze.tests;
2
3import static org.junit.Assert.assertTrue;
4
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import org.junit.BeforeClass;
10import org.junit.Test;
11
12import ch.epfl.maze.physical.Animal;
13import ch.epfl.maze.physical.Maze;
14import ch.epfl.maze.physical.zoo.Bear;
15import ch.epfl.maze.physical.zoo.Hamster;
16import ch.epfl.maze.physical.zoo.Monkey;
17import ch.epfl.maze.physical.zoo.Mouse;
18import ch.epfl.maze.physical.zoo.Panda;
19import ch.epfl.maze.physical.zoo.SpaceInvader;
20import ch.epfl.maze.simulation.MazeSimulation;
21import ch.epfl.maze.simulation.Simulation;
22import 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
42public 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);
111 }
112 }
113 }
114 }
115 }
116 System.out.println("done !");
117 }
118
119 /**
120 * Determines if the {@code SpaceInvader} has beaten the {@code Mouse}.
121 */
122
123 @Test
124 public void testVSMouse() {
125 assertTrue("The SpaceInvader has not beaten the Mouse", rivalries.get("Mouse") > 0);
126 }
127
128 /**
129 * Determines if the {@code SpaceInvader} has beaten the {@code Hamster}.
130 */
131
132 @Test
133 public void testVSHamster() {
134 assertTrue("The SpaceInvader has not beaten the Hamster", rivalries.get("Hamster") > 0);
135 }
136
137 /**
138 * Determines if the {@code SpaceInvader} has beaten the {@code Monkey}.
139 */
140
141 @Test
142 public void testVSMonkey() {
143 assertTrue("The SpaceInvader has not beaten the Monkey", rivalries.get("Monkey") > 0);
144 }
145
146 /**
147 * Determines if the {@code SpaceInvader} has beaten the {@code Bear}.
148 */
149
150 @Test
151 public void testVSBear() {
152 assertTrue("The SpaceInvader has not beaten the Bear", rivalries.get("Bear") > 0);
153 }
154
155 /**
156 * Determines if the {@code SpaceInvader} has beaten the {@code Panda}.
157 */
158
159 @Test
160 public void testVSPanda() {
161 assertTrue("The SpaceInvader has not beaten the Panda", rivalries.get("Panda") > 0);
162 }
163}