diff options
Diffstat (limited to 'src/ch/epfl/maze/simulation/MazeSimulation.java')
-rw-r--r-- | src/ch/epfl/maze/simulation/MazeSimulation.java | 381 |
1 files changed, 189 insertions, 192 deletions
diff --git a/src/ch/epfl/maze/simulation/MazeSimulation.java b/src/ch/epfl/maze/simulation/MazeSimulation.java index d70f735..3fd0e52 100644 --- a/src/ch/epfl/maze/simulation/MazeSimulation.java +++ b/src/ch/epfl/maze/simulation/MazeSimulation.java | |||
@@ -1,10 +1,5 @@ | |||
1 | package ch.epfl.maze.simulation; | 1 | package ch.epfl.maze.simulation; |
2 | 2 | ||
3 | import java.util.LinkedList; | ||
4 | import java.util.List; | ||
5 | import java.util.Map; | ||
6 | import java.util.TreeMap; | ||
7 | |||
8 | import ch.epfl.maze.graphics.Animation; | 3 | import ch.epfl.maze.graphics.Animation; |
9 | import ch.epfl.maze.physical.Animal; | 4 | import ch.epfl.maze.physical.Animal; |
10 | import ch.epfl.maze.physical.Maze; | 5 | import ch.epfl.maze.physical.Maze; |
@@ -13,199 +8,201 @@ import ch.epfl.maze.util.Action; | |||
13 | import ch.epfl.maze.util.Direction; | 8 | import ch.epfl.maze.util.Direction; |
14 | import ch.epfl.maze.util.Vector2D; | 9 | import ch.epfl.maze.util.Vector2D; |
15 | 10 | ||
11 | import java.util.LinkedList; | ||
12 | import java.util.List; | ||
13 | import java.util.Map; | ||
14 | import java.util.TreeMap; | ||
15 | |||
16 | /** | 16 | /** |
17 | * Simulation of a maze solver. Handles the next move of each animal, as well as | 17 | * Simulation of a maze solver. Handles the next move of each animal, as well as |
18 | * the animation by notifying the changes to it. The simulation finishes when | 18 | * the animation by notifying the changes to it. The simulation finishes when |
19 | * every animal has found the exit. | 19 | * every animal has found the exit. |
20 | * | ||
21 | */ | 20 | */ |
22 | 21 | ||
23 | public final class MazeSimulation implements Simulation { | 22 | public final class MazeSimulation implements Simulation { |
24 | 23 | ||
25 | /* limit to the step counter, over which the animals are considered lost */ | 24 | /* limit to the step counter, over which the animals are considered lost */ |
26 | public static final int COUNTER_LIMIT = 10000; | 25 | public static final int COUNTER_LIMIT = 10000; |
27 | 26 | ||
28 | /* simulation components */ | 27 | /* simulation components */ |
29 | private Maze mMaze; | 28 | private Maze mMaze; |
30 | private Map<Integer, List<Animal>> mArrivalTimes; | 29 | private Map<Integer, List<Animal>> mArrivalTimes; |
31 | private int mStepCounter; | 30 | private int mStepCounter; |
32 | 31 | ||
33 | /** | 32 | /** |
34 | * Constructs a simulation with a {@code Maze} to simulate. | 33 | * Constructs a simulation with a {@code Maze} to simulate. |
35 | * | 34 | * |
36 | * @param maze | 35 | * @param maze The maze to simulate |
37 | * The maze to simulate | 36 | */ |
38 | */ | 37 | |
39 | 38 | public MazeSimulation(Maze maze) { | |
40 | public MazeSimulation(Maze maze) { | 39 | mMaze = maze; |
41 | mMaze = maze; | 40 | mArrivalTimes = new TreeMap<Integer, List<Animal>>(); |
42 | mArrivalTimes = new TreeMap<Integer, List<Animal>>(); | 41 | mStepCounter = 0; |
43 | mStepCounter = 0; | 42 | } |
44 | } | 43 | |
45 | 44 | @Override | |
46 | @Override | 45 | public void move(Animation listener) { |
47 | public void move(Animation listener) { | 46 | if (isOver()) { |
48 | if (isOver()) { | 47 | return; |
49 | return; | 48 | } |
50 | } | 49 | |
51 | 50 | // increments counter | |
52 | // increments counter | 51 | mStepCounter++; |
53 | mStepCounter++; | 52 | |
54 | 53 | // if counter exceeded limit, it considers animals lost | |
55 | // if counter exceeded limit, it considers animals lost | 54 | if (mStepCounter > COUNTER_LIMIT) { |
56 | if (mStepCounter > COUNTER_LIMIT) { | 55 | List<Animal> animals = mMaze.getAnimals(); |
57 | List<Animal> animals = mMaze.getAnimals(); | 56 | List<Animal> lostAnimals = new LinkedList<Animal>(); |
58 | List<Animal> lostAnimals = new LinkedList<Animal>(); | 57 | for (Animal animal : animals) { |
59 | for (Animal animal : animals) { | 58 | mMaze.removeAnimal(animal); |
60 | mMaze.removeAnimal(animal); | 59 | lostAnimals.add(animal); |
61 | lostAnimals.add(animal); | 60 | } |
62 | } | 61 | |
63 | 62 | mArrivalTimes.put(Integer.MAX_VALUE, lostAnimals); // infinite | |
64 | mArrivalTimes.put(Integer.MAX_VALUE, lostAnimals); // infinite | 63 | return; |
65 | return; | 64 | } |
66 | } | 65 | |
67 | 66 | // asks animals to move | |
68 | // asks animals to move | 67 | moveAnimals(listener); |
69 | moveAnimals(listener); | 68 | |
70 | 69 | // notifies animation that all the changes are done | |
71 | // notifies animation that all the changes are done | 70 | if (listener != null) { |
72 | if (listener != null) { | 71 | listener.doneUpdating(); |
73 | listener.doneUpdating(); | 72 | } |
74 | } | 73 | } |
75 | } | 74 | |
76 | 75 | @Override | |
77 | @Override | 76 | public boolean isOver() { |
78 | public boolean isOver() { | 77 | return mMaze.isSolved(); |
79 | return mMaze.isSolved(); | 78 | } |
80 | } | 79 | |
81 | 80 | @Override | |
82 | @Override | 81 | public World getWorld() { |
83 | public World getWorld() { | 82 | return mMaze; |
84 | return mMaze; | 83 | } |
85 | } | 84 | |
86 | 85 | @Override | |
87 | @Override | 86 | public int getSteps() { |
88 | public int getSteps() { | 87 | return mStepCounter; |
89 | return mStepCounter; | 88 | } |
90 | } | 89 | |
91 | 90 | public Map<Integer, List<Animal>> getArrivalTimes() { | |
92 | public Map<Integer, List<Animal>> getArrivalTimes() { | 91 | return new TreeMap<Integer, List<Animal>>(mArrivalTimes); |
93 | return new TreeMap<Integer, List<Animal>>(mArrivalTimes); | 92 | } |
94 | } | 93 | |
95 | 94 | public String getRecordTable() { | |
96 | public String getRecordTable() { | 95 | String recordTable = ""; |
97 | String recordTable = ""; | 96 | int position = 1; |
98 | int position = 1; | 97 | for (Map.Entry<Integer, List<Animal>> entry : mArrivalTimes.entrySet()) { |
99 | for (Map.Entry<Integer, List<Animal>> entry : mArrivalTimes.entrySet()) { | 98 | // only returns the 10 first |
100 | // only returns the 10 first | 99 | if (position > 10) { |
101 | if (position > 10) { | 100 | return recordTable; |
102 | return recordTable; | 101 | } |
103 | } | 102 | |
104 | 103 | for (Animal animal : entry.getValue()) { | |
105 | for (Animal animal : entry.getValue()) { | 104 | if (entry.getKey() == Integer.MAX_VALUE) { |
106 | if (entry.getKey() == Integer.MAX_VALUE) { | 105 | recordTable += "-- "; |
107 | recordTable += "-- "; | 106 | recordTable += animal.getClass().getSimpleName(); |
108 | recordTable += animal.getClass().getSimpleName(); | 107 | recordTable += " - never finished\n"; |
109 | recordTable += " - never finished\n"; | 108 | } else { |
110 | } else { | 109 | recordTable += position + ". "; |
111 | recordTable += position + ". "; | 110 | recordTable += animal.getClass().getSimpleName(); |
112 | recordTable += animal.getClass().getSimpleName(); | 111 | recordTable += " - " + entry.getKey() + " steps\n"; |
113 | recordTable += " - " + entry.getKey() + " steps\n"; | 112 | } |
114 | } | 113 | } |
115 | } | 114 | position += entry.getValue().size(); |
116 | position += entry.getValue().size(); | 115 | } |
117 | } | 116 | |
118 | 117 | return recordTable; | |
119 | return recordTable; | 118 | } |
120 | } | 119 | |
121 | 120 | @Override | |
122 | @Override | 121 | public void restart() { |
123 | public void restart() { | 122 | mMaze.reset(); |
124 | mMaze.reset(); | 123 | mArrivalTimes.clear(); |
125 | mArrivalTimes.clear(); | 124 | mStepCounter = 0; |
126 | mStepCounter = 0; | 125 | } |
127 | } | 126 | |
128 | 127 | @Override | |
129 | @Override | 128 | public void stop() { |
130 | public void stop() { | 129 | List<Animal> forgottenAnimals = new LinkedList<Animal>(); |
131 | List<Animal> forgottenAnimals = new LinkedList<Animal>(); | 130 | for (Animal animal : mMaze.getAnimals()) { |
132 | for (Animal animal : mMaze.getAnimals()) { | 131 | forgottenAnimals.add(animal); |
133 | forgottenAnimals.add(animal); | 132 | mMaze.removeAnimal(animal); |
134 | mMaze.removeAnimal(animal); | 133 | } |
135 | } | 134 | mArrivalTimes.put(Integer.MAX_VALUE, forgottenAnimals); |
136 | mArrivalTimes.put(Integer.MAX_VALUE, forgottenAnimals); | 135 | } |
137 | } | 136 | |
138 | 137 | /** | |
139 | /** | 138 | * Moves the animals in the maze. |