diff options
author | Pacien TRAN-GIRARD | 2016-03-15 22:56:27 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-03-15 22:56:27 +0100 |
commit | aa317f43ad2c714cecbae5202bcfc2c2ab68172e (patch) | |
tree | d8cf7d4800a0e82b59c74d3432f2fa41cb2d257b /src/ch/epfl | |
parent | 1e6848b2a40b331cad4fea9e89e29df621402222 (diff) | |
download | xblast-aa317f43ad2c714cecbae5202bcfc2c2ab68172e.tar.gz |
Implement conflict resolution protocol
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/server/GameState.java | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index e15f0bf..6e7f8c4 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java | |||
@@ -1,10 +1,7 @@ | |||
1 | package ch.epfl.xblast.server; | 1 | package ch.epfl.xblast.server; |
2 | 2 | ||
3 | import ch.epfl.cs108.Sq; | 3 | import ch.epfl.cs108.Sq; |
4 | import ch.epfl.xblast.ArgumentChecker; | 4 | import ch.epfl.xblast.*; |
5 | import ch.epfl.xblast.Cell; | ||
6 | import ch.epfl.xblast.Direction; | ||
7 | import ch.epfl.xblast.PlayerID; | ||
8 | 5 | ||
9 | import java.util.*; | 6 | import java.util.*; |
10 | import java.util.function.Function; | 7 | import java.util.function.Function; |
@@ -20,6 +17,20 @@ import java.util.stream.Stream; | |||
20 | public final class GameState { | 17 | public final class GameState { |
21 | 18 | ||
22 | /** | 19 | /** |
20 | * The list of player priority order permutations. | ||
21 | */ | ||
22 | private static final List<List<PlayerID>> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList(); | ||
23 | |||
24 | /** | ||
25 | * Builds and returns the player priority order permutations. | ||
26 | * | ||
27 | * @return the list of player priority orders | ||
28 | */ | ||
29 | private static List<List<PlayerID>> buildPlayerPriorityOrderList() { | ||
30 | return Lists.permutations(Arrays.asList(PlayerID.values())); | ||
31 | } | ||
32 | |||
33 | /** | ||
23 | * Filters the given list of players and returns the lively ones. | 34 | * Filters the given list of players and returns the lively ones. |
24 | * | 35 | * |
25 | * @param players a list of players | 36 | * @param players a list of players |
@@ -261,4 +272,28 @@ public final class GameState { | |||
261 | return null; // TODO | 272 | return null; // TODO |
262 | } | 273 | } |
263 | 274 | ||
275 | /** | ||
276 | * Returns the current player priority order permutation. | ||
277 | * | ||
278 | * @return the player priority order | ||
279 | */ | ||
280 | private List<PlayerID> currentPlayerPriorityOrder() { | ||
281 | int priorityIndex = this.ticks % GameState.PLAYER_PRIORITY_ORDERS.size(); | ||
282 | return GameState.PLAYER_PRIORITY_ORDERS.get(priorityIndex); | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * Resolves a conflict according to the current priority order. | ||
287 | * | ||
288 | * @param claimants the list of claimants | ||
289 | * @return the highest priority player | ||
290 | */ | ||
291 | private PlayerID resolveConflict(List<PlayerID> claimants) { | ||
292 | return this.currentPlayerPriorityOrder() | ||
293 | .stream() | ||
294 | .filter(claimants::contains) | ||
295 | .findFirst() | ||
296 | .get(); | ||
297 | } | ||
298 | |||
264 | } | 299 | } |