diff options
-rw-r--r-- | src/ch/epfl/xblast/Cell.java | 13 | ||||
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 62 | ||||
-rw-r--r-- | src/ch/epfl/xblast/PlayerAction.java | 19 | ||||
-rw-r--r-- | src/ch/epfl/xblast/SubCell.java | 13 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/GameState.java | 15 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/GameStateDeserializer.java | 59 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 47 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/XBlastComponent.java | 178 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/painter/ScorePainter.java | 4 | ||||
-rw-r--r-- | test/ch/epfl/xblast/ListsTest.java | 47 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/ConsoleSimulation.java | 23 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 86 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/RandomSimulation.java | 80 | ||||
-rw-r--r-- | test/ch/epfl/xblast/simulation/Simulation.java | 54 |
14 files changed, 602 insertions, 98 deletions
diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java index 9720281..3f41e5c 100644 --- a/src/ch/epfl/xblast/Cell.java +++ b/src/ch/epfl/xblast/Cell.java | |||
@@ -10,7 +10,7 @@ import java.util.List; | |||
10 | * @author Pacien TRAN-GIRARD (261948) | 10 | * @author Pacien TRAN-GIRARD (261948) |
11 | * @author Timothée FLOURE (257420) | 11 | * @author Timothée FLOURE (257420) |
12 | */ | 12 | */ |
13 | public final class Cell { | 13 | public final class Cell implements Comparable<Cell> { |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * The width of the board (number of columns). | 16 | * The width of the board (number of columns). |
@@ -194,4 +194,15 @@ public final class Cell { | |||
194 | return String.format("(%d,%d)", this.x, this.y); | 194 | return String.format("(%d,%d)", this.x, this.y); |
195 | } | 195 | } |
196 | 196 | ||
197 | /** | ||
198 | * Compares two Cells relatively to their row major indexes. | ||
199 | * | ||
200 | * @param cell the other Cell to compare | ||
201 | * @return the comparison result | ||
202 | */ | ||
203 | @Override | ||
204 | public int compareTo(Cell cell) { | ||
205 | return Integer.compare(this.rowMajorIndex(), cell.rowMajorIndex()); | ||
206 | } | ||
207 | |||
197 | } | 208 | } |
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index 526ebf4..777fab2 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java | |||
@@ -2,6 +2,7 @@ package ch.epfl.xblast; | |||
2 | 2 | ||
3 | import java.util.*; | 3 | import java.util.*; |
4 | import java.util.stream.Collectors; | 4 | import java.util.stream.Collectors; |
5 | import java.util.stream.IntStream; | ||
5 | import java.util.stream.Stream; | 6 | import java.util.stream.Stream; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -60,6 +61,20 @@ public final class Lists { | |||
60 | } | 61 | } |
61 | 62 | ||
62 | /** | 63 | /** |
64 | * Returns an immutable copy of the given list, sorted using the supplied comparator. | ||
65 | * | ||
66 | * @param l the list to sort | ||
67 | * @param cmp the Comparator to use | ||
68 | * @param <T> the type of the list's elements | ||
69 | * @return the sorted list | ||
70 | */ | ||
71 | public static <T> List<T> sorted(List<T> l, Comparator<T> cmp) { | ||
72 | List<T> r = new ArrayList<>(l); | ||
73 | Collections.sort(r, cmp); | ||
74 | return Collections.unmodifiableList(r); | ||
75 | } | ||
76 | |||
77 | /** | ||
63 | * Returns a copy of the given list surrounded with element e. | 78 | * Returns a copy of the given list surrounded with element e. |
64 | * | 79 | * |
65 | * @param l the list | 80 | * @param l the list |
@@ -179,4 +194,51 @@ public final class Lists { | |||
179 | return r; | 194 | return r; |
180 | } | 195 | } |
181 | 196 | ||
197 | /** | ||
198 | * Maps linearly two lists. | ||
199 | * | ||
200 | * @param kl the keys list | ||
201 | * @param vl the values list | ||
202 | * @param <K> the key type | ||
203 | * @param <V> the value type | ||
204 | * @return the map | ||
205 | */ | ||
206 | public static <K, V> Map<K, V> linearMap(List<K> kl, List<V> vl) { | ||
207 | if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) | ||
208 | throw new IllegalArgumentException(); | ||
209 | |||
210 | return Collections.unmodifiableMap(IntStream | ||
211 | .range(0, kl.size()).mapToObj(i -> i) | ||
212 | .filter(i -> Objects.nonNull(vl.get(i))) | ||
213 | .collect(Collectors.toMap(kl::get, vl::get))); | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * Merges two maps Map<K1, IK> and Map<IK, V> -> Map<K1, V>. | ||
218 | * | ||
219 | * @param m1 key maps | ||
220 | * @param m2 values maps | ||
221 | * @param <K> the keys type | ||
222 | * @param <IK> the intermediate keys type | ||
223 | * @param <V> the values type | ||
224 | * @return the traversing map | ||
225 | */ | ||
226 | public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) { | ||
227 | return Collections.unmodifiableMap(m1.entrySet().stream() | ||
228 | .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue())))); | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * Returns an inverted copy of a bijective map. | ||
233 | * | ||
234 | * @param m the map to invert | ||
235 | * @param <K> the keys type | ||
236 | * @param <V> the values type | ||
237 | * @return the inverted map | ||
238 | */ | ||
239 | public static <K, V> Map<K, V> invertMap(Map<V, K> m) { | ||
240 | return Collections.unmodifiableMap(m.entrySet().stream() | ||
241 | .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); | ||
242 | } | ||
243 | |||
182 | } | 244 | } |
diff --git a/src/ch/epfl/xblast/PlayerAction.java b/src/ch/epfl/xblast/PlayerAction.java new file mode 100644 index 0000000..703bc9d --- /dev/null +++ b/src/ch/epfl/xblast/PlayerAction.java | |||
@@ -0,0 +1,19 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | /** | ||
4 | * The player action enum. | ||
5 | * | ||
6 | * @author Timothée FLOURE (257420) | ||
7 | * @author Pacien TRAN-GIRARD (261948) | ||
8 | */ | ||
9 | public enum PlayerAction { | ||
10 | |||
11 | JOIN_GAME, | ||
12 | MOVE_N, | ||
13 | MOVE_E, | ||
14 | MOVE_S, | ||
15 | MOVE_W, | ||
16 | STOP, | ||
17 | DROP_BOMB | ||
18 | |||
19 | } | ||
diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java index 4278fab..fd9db0b 100644 --- a/src/ch/epfl/xblast/SubCell.java +++ b/src/ch/epfl/xblast/SubCell.java | |||
@@ -6,7 +6,7 @@ package ch.epfl.xblast; | |||
6 | * @author Pacien TRAN-GIRARD (261948) | 6 | * @author Pacien TRAN-GIRARD (261948) |
7 | * @author Timothée FLOURE (257420) | 7 | * @author Timothée FLOURE (257420) |
8 | */ | 8 | */ |
9 | public final class SubCell { | 9 | public final class SubCell implements Comparable<SubCell> { |
10 | 10 | ||
11 | /** | 11 | /** |
12 | * The number of x-subdivisions of each Cell. | 12 | * The number of x-subdivisions of each Cell. |
@@ -165,4 +165,15 @@ public final class SubCell { | |||
165 | return this.y * SUB_COLUMNS + this.x; | 165 | return this.y * SUB_COLUMNS + this.x; |
166 | } | 166 | } |
167 | 167 | ||
168 | /** | ||
169 | * Compares two SubCells relatively to their row major indexes. | ||
170 | * | ||
171 | * @param subCell the other SubCell to compare | ||
172 | * @return the comparison result | ||
173 | */ | ||
174 | @Override | ||
175 | public int compareTo(SubCell subCell) { | ||
176 | return Integer.compare(this.rowMajorIndex(), subCell.rowMajorIndex()); | ||
177 | } | ||
178 | |||
168 | } | 179 | } |
diff --git a/src/ch/epfl/xblast/client/GameState.java b/src/ch/epfl/xblast/client/GameState.java index 7652cd5..480c6a1 100644 --- a/src/ch/epfl/xblast/client/GameState.java +++ b/src/ch/epfl/xblast/client/GameState.java | |||
@@ -8,8 +8,11 @@ import ch.epfl.xblast.client.painter.ScorePainter; | |||
8 | import ch.epfl.xblast.client.painter.TimeLinePainter; | 8 | import ch.epfl.xblast.client.painter.TimeLinePainter; |
9 | 9 | ||
10 | import java.awt.*; | 10 | import java.awt.*; |
11 | import java.util.Collections; | ||
11 | import java.util.List; | 12 | import java.util.List; |
13 | import java.util.Map; | ||
12 | import java.util.Objects; | 14 | import java.util.Objects; |
15 | import java.util.stream.Collectors; | ||
13 | 16 | ||
14 | /** | 17 | /** |
15 | * The client representation of a game state. | 18 | * The client representation of a game state. |
@@ -41,7 +44,7 @@ public final class GameState { | |||
41 | this.id = id; | 44 | this.id = id; |
42 | this.lives = lives; | 45 | this.lives = lives; |
43 | this.position = Objects.requireNonNull(position); | 46 | this.position = Objects.requireNonNull(position); |
44 | this.image = Objects.requireNonNull(image); | 47 | this.image = image; |
45 | } | 48 | } |
46 | 49 | ||
47 | /** | 50 | /** |
@@ -131,7 +134,7 @@ public final class GameState { | |||
131 | } | 134 | } |
132 | 135 | ||
133 | /** | 136 | /** |
134 | * @return list containing all the images composing the actual score | 137 | * @return list containing all the images composing the score |
135 | */ | 138 | */ |
136 | public List<Image> scores() { | 139 | public List<Image> scores() { |
137 | return this.scores; | 140 | return this.scores; |
@@ -144,4 +147,12 @@ public final class GameState { | |||
144 | return this.ticks; | 147 | return this.ticks; |
145 | } | 148 | } |
146 | 149 | ||
150 | /** | ||
151 | * @return map of players' scores | ||
152 | */ | ||
153 | Map<PlayerID, Integer> playerScores() { | ||
154 | return Collections.unmodifiableMap(this.players.stream() | ||
155 | .collect(Collectors.toMap(Player::id, Player::lives))); | ||
156 | } | ||
157 | |||
147 | } | 158 | } |
diff --git a/src |