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