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/simulation | |
parent | 655ac88f4e73b2df532a451aedf5a561ea1b0d2c (diff) | |
download | maze-solver-90bd766f361083f6fd9e39ff080c83fcc606832f.tar.gz |
Reformat imported code
Diffstat (limited to 'src/ch/epfl/maze/simulation')
-rw-r--r-- | src/ch/epfl/maze/simulation/DaedalusSimulation.java | 681 | ||||
-rw-r--r-- | src/ch/epfl/maze/simulation/MazeSimulation.java | 381 | ||||
-rw-r--r-- | src/ch/epfl/maze/simulation/Simulation.java | 108 |
3 files changed, 575 insertions, 595 deletions
diff --git a/src/ch/epfl/maze/simulation/DaedalusSimulation.java b/src/ch/epfl/maze/simulation/DaedalusSimulation.java index 2082772..8b60b57 100644 --- a/src/ch/epfl/maze/simulation/DaedalusSimulation.java +++ b/src/ch/epfl/maze/simulation/DaedalusSimulation.java | |||
@@ -1,364 +1,349 @@ | |||
1 | package ch.epfl.maze.simulation; | 1 | package ch.epfl.maze.simulation; |
2 | 2 | ||
3 | import java.util.ArrayList; | ||
4 | import java.util.Collections; | ||
5 | import java.util.HashMap; | ||
6 | import java.util.LinkedList; | ||
7 | import java.util.List; | ||
8 | import java.util.Map; | ||
9 | import java.util.TreeMap; | ||
10 | |||
11 | import ch.epfl.maze.graphics.Animation; | 3 | import ch.epfl.maze.graphics.Animation; |
12 | import ch.epfl.maze.physical.Animal; | 4 | import ch.epfl.maze.physical.*; |
13 | import ch.epfl.maze.physical.Daedalus; | ||
14 | import ch.epfl.maze.physical.Predator; | ||
15 | import ch.epfl.maze.physical.Prey; | ||
16 | import ch.epfl.maze.physical.World; | ||
17 | import ch.epfl.maze.util.Action; | 5 | import ch.epfl.maze.util.Action; |
18 | import ch.epfl.maze.util.Direction; | 6 | import ch.epfl.maze.util.Direction; |
19 | import ch.epfl.maze.util.Vector2D; | 7 | import ch.epfl.maze.util.Vector2D; |
20 | 8 | ||
9 | import java.util.*; | ||
10 | |||
21 | /** | 11 | /** |
22 | * Simulation of a predation environment. Handles the next moves of every | 12 | * Simulation of a predation environment. Handles the next moves of every |
23 | * predator and prey in a Daedalus, as well as the animation by notifying | 13 | * predator and prey in a Daedalus, as well as the animation by notifying |
24 | * changes to it. The simulation finishes when every prey has been caught. | 14 | * changes to it. The simulation finishes when every prey has been caught. |
25 | * | ||
26 | */ | 15 | */ |
27 | 16 | ||
28 | public final class DaedalusSimulation implements Simulation { | 17 | public final class DaedalusSimulation implements Simulation { |
29 | 18 | ||
30 | /* limit to the step counter, over which the animals are considered lost */ | 19 | /* limit to the step counter, over which the animals are considered lost */ |
31 | public static final int COUNTER_LIMIT = 10000; | 20 | public static final int COUNTER_LIMIT = 10000; |
32 | 21 | ||
33 | /* simulation components */ | 22 | /* simulation components */ |
34 | private Daedalus mDaedalus; | 23 | private Daedalus mDaedalus; |
35 | private Map<Integer, List<Prey>> mArrivalTimes; | 24 | private Map<Integer, List<Prey>> mArrivalTimes; |
36 | private int mStepCounter; | 25 | private int mStepCounter; |
37 | 26 | ||
38 | /* collision check variables */ | 27 | /* collision check variables */ |
39 | private Map<Prey, List<Vector2D>> mPreyMoves; | 28 | private Map<Prey, List<Vector2D>> mPreyMoves; |
40 | private Map<Predator, List<Vector2D>> mPredatorMoves; | 29 | private Map<Predator, List<Vector2D>> mPredatorMoves; |
41 | 30 | ||
42 | /** | 31 | /** |
43 | * Constructs a simulation with a {@code Daedalus} to simulate. | 32 | * Constructs a simulation with a {@code Daedalus} to simulate. |
44 | * | 33 | * |
45 | * @param daedalus | 34 | * @param daedalus The daedalus to simulate |
46 | * The daedalus to simulate | 35 | */ |
47 | */ | 36 | |
48 | 37 | public DaedalusSimulation(Daedalus daedalus) { | |
49 | public DaedalusSimulation(Daedalus daedalus) { | 38 | mDaedalus = daedalus; |
50 | mDaedalus = daedalus; | 39 | mArrivalTimes = new TreeMap<Integer, List<Prey>>(Collections.reverseOrder()); |
51 | mArrivalTimes = new TreeMap<Integer, List<Prey>>(Collections.reverseOrder()); | 40 | mStepCounter = 0; |
52 | mStepCounter = 0; | 41 | mPreyMoves = new HashMap<Prey, List<Vector2D>>(); |
53 | mPreyMoves = new HashMap<Prey, List<Vector2D>>(); | 42 | mPredatorMoves = new HashMap<Predator, List<Vector2D>>(); |
54 | mPredatorMoves = new HashMap<Predator, List<Vector2D>>(); | 43 | } |
55 | } | 44 | |
56 | 45 | @Override | |
57 | @Override | 46 | public void move(Animation listener) { |
58 | public void move(Animation listener) { | 47 | if (isOver()) { |
59 | if (isOver()) { | 48 | return; |
60 | return; | 49 | } |
61 | } | 50 | |
62 | 51 | // clears moves maps | |
63 | // clears moves maps | 52 | mPreyMoves.clear(); |
64 | mPreyMoves.clear(); | 53 | mPredatorMoves.clear(); |
65 | mPredatorMoves.clear(); | 54 | |
66 | 55 | // increments counter | |
67 | // increments counter | 56 | mStepCounter++; |
68 | mStepCounter++; | 57 | |
69 | 58 | // if counter exceeded the limit, it considers preys safe | |
70 | // if counter exceeded the limit, it considers preys safe | 59 | if (mStepCounter > COUNTER_LIMIT) { |
71 | if (mStepCounter > COUNTER_LIMIT) { | 60 | List<Prey> preys = mDaedalus.getPreys(); |
72 | List<Prey> preys = mDaedalus.getPreys(); | 61 | List<Prey> safePreys = new LinkedList<Prey>(); |
73 | List<Prey> safePreys = new LinkedList<Prey>(); | 62 | for (Prey prey : preys) { |
74 | for (Prey prey : preys) { | 63 | mDaedalus.removePrey(prey); |
75 | mDaedalus.removePrey(prey); | 64 | safePreys.add(prey); |
76 | safePreys.add(prey); | 65 | } |
77 | } | 66 | |
78 | 67 | mArrivalTimes.put(Integer.MAX_VALUE, safePreys); // infinite | |
79 | mArrivalTimes.put(Integer.MAX_VALUE, safePreys); // infinite | 68 | return; |
80 | return; | 69 | } |
81 | } | 70 | |
82 | 71 | // asks predators and preys to move | |
83 | // asks predators and preys to move | 72 | movePredators(listener); |
84 | movePredators(listener); | 73 | movePreys(listener); |
85 | movePreys(listener); | 74 | |
86 | 75 | // checks collisions | |
87 | // checks collisions | 76 | checkCollisions(listener); |
88 | checkCollisions(listener); | 77 | |
89 | 78 | // notifies animation that all the changes are done | |
90 | // notifies animation that all the changes are done | 79 | if (listener != null) { |
91 | if (listener != null) { | 80 | listener.doneUpdating(); |
92 | listener.doneUpdating(); | 81 | } |
93 | } | 82 | } |
94 | } | 83 | |
95 | 84 | @Override | |
96 | @Override | 85 | public boolean isOver() { |
97 | public boolean isOver() { | 86 | return mDaedalus.isSolved(); |
98 | return mDaedalus.isSolved(); | 87 | } |
99 | } | 88 | |
100 | 89 | @Override | |
101 | @Override | 90 | public World getWorld() { |
102 | public World getWorld() { | 91 | return mDaedalus; |
103 | return mDaedalus; | 92 | } |
104 | } | 93 | |
105 | 94 | @Override | |
106 | @Override | 95 | public int getSteps() { |
107 | public int getSteps() { | 96 | return mStepCounter; |
108 | return mStepCounter; | 97 | } |
109 | } | 98 | |
110 | 99 | @Override | |
111 | @Override | 100 | public Map<Integer, List<Animal>> getArrivalTimes() { |
112 | public Map<Integer, List<Animal>> getArrivalTimes() { | 101 | TreeMap<Integer, List<Animal>> arrivalTimes = new TreeMap<Integer, List<Animal>>(); |
113 | TreeMap<Integer, List<Animal>> arrivalTimes = new TreeMap<Integer, List<Animal>>(); | 102 | for (Map.Entry<Integer, List<Prey>> entry : mArrivalTimes.entrySet()) { |
114 | for (Map.Entry<Integer, List<Prey>> entry : mArrivalTimes.entrySet()) { | 103 | int time = entry.getKey(); |
115 | int time = entry.getKey(); | 104 | List<Animal> animals = new ArrayList<Animal>(entry.getValue()); |
116 | List<Animal> animals = new ArrayList<Animal>(entry.getValue()); | 105 | arrivalTimes.put(time, animals); |
117 | arrivalTimes.put(time, animals); | 106 | } |
118 | } | 107 | |
119 | 108 | return arrivalTimes; | |
120 | return arrivalTimes; | 109 | } |
121 | } | 110 | |
122 | 111 | @Override | |
123 | @Override | 112 | public String getRecordTable() { |
124 | public String getRecordTable() { | 113 | String recordTable = ""; |
125 | String recordTable = ""; | 114 | int position = 1; |
126 | int position = 1; | 115 | for (Map.Entry<Integer, List<Prey>> entry : mArrivalTimes.entrySet()) { |
127 | for (Map.Entry<Integer, List<Prey>> entry : mArrivalTimes.entrySet()) { | 116 | // only returns the 10 first |
128 | // only returns the 10 first | 117 | if (position > 10) { |
129 | if (position > 10) { | 118 | return recordTable; |
130 | return recordTable; | 119 | } |
131 | } | 120 | |
132 | 121 | for (Prey prey : entry.getValue()) { | |
133 | for (Prey prey : entry.getValue()) { | 122 | if (entry.getKey() == Integer.MIN_VALUE) { |
134 | if (entry.getKey() == Integer.MIN_VALUE) |