aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-03-15 22:56:27 +0100
committerPacien TRAN-GIRARD2016-03-15 22:56:27 +0100
commitaa317f43ad2c714cecbae5202bcfc2c2ab68172e (patch)
treed8cf7d4800a0e82b59c74d3432f2fa41cb2d257b
parent1e6848b2a40b331cad4fea9e89e29df621402222 (diff)
downloadxblast-aa317f43ad2c714cecbae5202bcfc2c2ab68172e.tar.gz
Implement conflict resolution protocol
-rw-r--r--src/ch/epfl/xblast/server/GameState.java43
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 @@
1package ch.epfl.xblast.server; 1package ch.epfl.xblast.server;
2 2
3import ch.epfl.cs108.Sq; 3import ch.epfl.cs108.Sq;
4import ch.epfl.xblast.ArgumentChecker; 4import ch.epfl.xblast.*;
5import ch.epfl.xblast.Cell;
6import ch.epfl.xblast.Direction;
7import ch.epfl.xblast.PlayerID;
8 5
9import java.util.*; 6import java.util.*;
10import java.util.function.Function; 7import java.util.function.Function;
@@ -20,6 +17,20 @@ import java.util.stream.Stream;
20public final class GameState { 17public 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}