diff options
Diffstat (limited to 'src/ch/epfl/maze/main')
-rw-r--r-- | src/ch/epfl/maze/main/Console.java | 162 | ||||
-rw-r--r-- | src/ch/epfl/maze/main/Program.java | 106 |
2 files changed, 268 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/main/Console.java b/src/ch/epfl/maze/main/Console.java new file mode 100644 index 0000000..edad4b2 --- /dev/null +++ b/src/ch/epfl/maze/main/Console.java | |||
@@ -0,0 +1,162 @@ | |||
1 | package ch.epfl.maze.main; | ||
2 | |||
3 | import java.util.Collections; | ||
4 | import java.util.List; | ||
5 | import java.util.Map; | ||
6 | |||
7 | import ch.epfl.maze.physical.Daedalus; | ||
8 | import ch.epfl.maze.physical.Maze; | ||
9 | import ch.epfl.maze.physical.pacman.Blinky; | ||
10 | import ch.epfl.maze.physical.pacman.Clyde; | ||
11 | import ch.epfl.maze.physical.pacman.Inky; | ||
12 | import ch.epfl.maze.physical.pacman.PacMan; | ||
13 | import ch.epfl.maze.physical.pacman.Pinky; | ||
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.simulation.DaedalusSimulation; | ||
20 | import ch.epfl.maze.simulation.MazeSimulation; | ||
21 | import ch.epfl.maze.simulation.Simulation; | ||
22 | import ch.epfl.maze.util.LabyrinthGenerator; | ||
23 | import ch.epfl.maze.util.Statistics; | ||
24 | import ch.epfl.maze.util.Vector2D; | ||
25 | |||
26 | /** | ||
27 | * Mini-project main program that will run the simulations multiple times and | ||
28 | * show statistics on the console. | ||
29 | * | ||
30 | */ | ||
31 | |||
32 | public class Console { | ||
33 | |||
34 | /** Number of simulations launched. */ | ||
35 | public static final int NUMBER_OF_SIMULATIONS = 1000; | ||
36 | |||
37 | public static void main(String[] args) { | ||
38 | Simulation simulation; | ||
39 | |||
40 | simulation = getMazeSimulation(); | ||
41 | //simulation = getDaedalusSimulation(); | ||
42 | |||
43 | System.out.print("Launching " + NUMBER_OF_SIMULATIONS + " simulations..."); | ||
44 | Map<String, List<Integer>> results = | ||
45 | Statistics.computeStatistics(simulation, NUMBER_OF_SIMULATIONS); | ||
46 | System.out.println(" done !"); | ||
47 | |||
48 | printStats(results); | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Creates a {@code MazeSimulation} suitable for statistics. | ||
53 | * <p> | ||
54 | * Note that there should be only <b>ONE</b> animal of each kind in the | ||
55 | * corresponding {@code Maze}. | ||
56 | * | ||
57 | * @return A {@code MazeSimulation} suitable for statistics | ||
58 | */ | ||
59 | |||
60 | public static Simulation getMazeSimulation() { | ||
61 | int[][] labyrinth = LabyrinthGenerator.getMedium(); | ||
62 | Maze m = new Maze(labyrinth); | ||
63 | Simulation simulation = new MazeSimulation(m); | ||
64 | |||
65 | // adds a Mouse | ||
66 | m.addAnimal(new Mouse(m.getStart())); | ||
67 | |||
68 | // adds a Monkey | ||
69 | m.addAnimal(new Monkey(m.getStart())); | ||
70 | |||
71 | // adds a Hamster | ||
72 | m.addAnimal(new Hamster(m.getStart())); | ||
73 | |||
74 | // adds a Bear (if this bonus is coded) | ||
75 | // m.addAnimal(new Bear(m.getStart())); | ||
76 | |||
77 | // adds a Panda | ||
78 | m.addAnimal(new Panda(m.getStart())); | ||
79 | |||
80 | return simulation; | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Creates a {@code DaedalusSimulation} suitable for statistics. | ||
85 | * <p> | ||
86 | * Note that there should be only <b>ONE</b> animal of each kind in the | ||
87 | * corresponding {@code Daedalus}. | ||
88 | * | ||
89 | * @return A {@code DaedalusSimulation} suitable for statistics | ||
90 | */ | ||
91 | |||
92 | public static Simulation getDaedalusSimulation() { | ||
93 | int[][] labyrinth = LabyrinthGenerator.getPacMan(); | ||
94 | Daedalus d = new Daedalus(labyrinth); | ||
95 | Simulation simulation = new DaedalusSimulation(d); | ||
96 | |||
97 | // adds Pac-Man | ||
98 | d.addPrey(new PacMan(new Vector2D(9, 15))); | ||
99 | |||
100 | // adds Blinky | ||
101 | d.addPredator(new Blinky(new Vector2D(17, 1))); | ||
102 | |||
103 | // adds Pinky | ||
104 | d.addPredator(new Pinky(new Vector2D(1, 1))); | ||
105 | |||
106 | // adds Inky | ||
107 | d.addPredator(new Inky(new Vector2D(17, 17))); | ||
108 | |||
109 | // adds Clyde | ||
110 | d.addPredator(new Clyde(new Vector2D(1, 17))); | ||
111 | |||
112 | return simulation; | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Pretty-prints the statistics computed in the parameters. | ||
117 | * | ||
118 | * @param results | ||
119 | * Statistics of arrival times for every animals/preys | ||
120 | */ | ||
121 | |||
122 | public static void printStats(Map<String, List<Integer>> results) { | ||
123 | // computes statistics | ||
124 | for (Map.Entry<String, List<Integer>> entry : results.entrySet()) { | ||
125 | String name = entry.getKey(); | ||
126 | List<Integer> list = entry.getValue(); | ||
127 | if (list.isEmpty()) { | ||
128 | continue; | ||
129 | } | ||
130 | Collections.sort(list); | ||
131 | |||
132 | String max, min, std, mean, median, total; | ||
133 | // handles infinite values | ||
134 | if (Statistics.total(list) == Integer.MAX_VALUE) { | ||
135 | total = "Infinite"; | ||
136 | mean = "Infinite"; | ||
137 | std = "Infinite"; | ||
138 | max = "Infinite"; | ||
139 | } else { | ||
140 | total = Integer.toString(Statistics.total(list)); | ||
141 | mean = Integer.toString(Statistics.mean(list)); | ||
142 | std = Double.toString(Statistics.std(list)); | ||
143 | max = Integer.toString(list.get(list.size() - 1)); | ||
144 | } | ||
145 | // min and median are special | ||
146 | min = (list.get(0) == Integer.MAX_VALUE) ? | ||
147 | "Infinite" : Integer.toString(list.get(0)); | ||
148 | median = (list.get(list.size() / 2) == Integer.MAX_VALUE) ? | ||
149 | "Infinite" : Integer.toString(list.get(list.size() / 2)); | ||
150 | |||
151 | System.out.println("\n\n========== " + name + " ==========\n"); | ||
152 | System.out.println(" * total number of steps : " + total); | ||
153 | System.out.println(" * average steps : " + mean); | ||
154 | System.out.println(" * median steps : " + median); | ||
155 | System.out.println(" * standard deviation : " + std); | ||
156 | System.out.println(" * minimum steps : " + min); | ||
157 | System.out.println(" * maximum steps : " + max); | ||
158 | System.out.println("\nDistribution :"); | ||
159 | Statistics.printDistribution(list); | ||
160 | } | ||
161 | } | ||
162 | } | ||
diff --git a/src/ch/epfl/maze/main/Program.java b/src/ch/epfl/maze/main/Program.java new file mode 100644 index 0000000..a295ae2 --- /dev/null +++ b/src/ch/epfl/maze/main/Program.java | |||
@@ -0,0 +1,106 @@ | |||
1 | package ch.epfl.maze.main; | ||
2 | |||
3 | import ch.epfl.maze.graphics.Display; | ||
4 | import ch.epfl.maze.physical.Daedalus; | ||
5 | import ch.epfl.maze.physical.Maze; | ||
6 | import ch.epfl.maze.physical.pacman.Blinky; | ||
7 | import ch.epfl.maze.physical.pacman.Clyde; | ||
8 | import ch.epfl.maze.physical.pacman.Inky; | ||
9 | import ch.epfl.maze.physical.pacman.PacMan; | ||
10 | import ch.epfl.maze.physical.pacman.Pinky; | ||
11 | import ch.epfl.maze.physical.zoo.Bear; | ||
12 | import ch.epfl.maze.physical.zoo.Hamster; | ||
13 | import ch.epfl.maze.physical.zoo.Monkey; | ||
14 | import ch.epfl.maze.physical.zoo.Mouse; | ||
15 | import ch.epfl.maze.physical.zoo.Panda; | ||
16 | import ch.epfl.maze.simulation.DaedalusSimulation; | ||
17 | import ch.epfl.maze.simulation.MazeSimulation; | ||
18 | import ch.epfl.maze.simulation.Simulation; | ||
19 | import ch.epfl.maze.util.LabyrinthGenerator; | ||
20 | import ch.epfl.maze.util.Vector2D; | ||
21 | |||
22 | /** | ||
23 | * Mini-project main program that will run the simulations on a {@code Display}. | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | public class Program { | ||
28 | |||
29 | /** | ||
30 | * Runs one of the two available simulations | ||
31 | * | ||
32 | * @see #getMazeSimulation() | ||
33 | * @see #getDaedalusSimulation() | ||
34 | */ | ||
35 | |||
36 | public static void main(String[] args) { | ||
37 | Simulation simulation; | ||
38 | |||
39 | simulation = getMazeSimulation(); | ||
40 | //simulation = getDaedalusSimulation(); | ||
41 | |||
42 | Display display = new Display(simulation); | ||
43 | display.run(); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Creates a {@code MazeSimulation} with every animal implementations. | ||
48 | * | ||
49 | * @return A {@code MazeSimulation} to display | ||
50 | */ | ||
51 | |||
52 | public static Simulation getMazeSimulation() { | ||
53 | int[][] labyrinth = LabyrinthGenerator.getMedium(); | ||
54 | Maze m = new Maze(labyrinth); | ||
55 | Simulation simulation = new MazeSimulation(m); | ||
56 | |||
57 | // adds a Mouse | ||
58 | m.addAnimal(new Mouse(m.getStart())); | ||
59 | |||
60 | // adds a Monkey | ||
61 | m.addAnimal(new Monkey(m.getStart())); | ||
62 | |||