From aa317f43ad2c714cecbae5202bcfc2c2ab68172e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 22:56:27 +0100 Subject: Implement conflict resolution protocol --- src/ch/epfl/xblast/server/GameState.java | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src') 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 @@ package ch.epfl.xblast.server; import ch.epfl.cs108.Sq; -import ch.epfl.xblast.ArgumentChecker; -import ch.epfl.xblast.Cell; -import ch.epfl.xblast.Direction; -import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.*; import java.util.*; import java.util.function.Function; @@ -19,6 +16,20 @@ import java.util.stream.Stream; */ public final class GameState { + /** + * The list of player priority order permutations. + */ + private static final List> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList(); + + /** + * Builds and returns the player priority order permutations. + * + * @return the list of player priority orders + */ + private static List> buildPlayerPriorityOrderList() { + return Lists.permutations(Arrays.asList(PlayerID.values())); + } + /** * Filters the given list of players and returns the lively ones. * @@ -261,4 +272,28 @@ public final class GameState { return null; // TODO } + /** + * Returns the current player priority order permutation. + * + * @return the player priority order + */ + private List currentPlayerPriorityOrder() { + int priorityIndex = this.ticks % GameState.PLAYER_PRIORITY_ORDERS.size(); + return GameState.PLAYER_PRIORITY_ORDERS.get(priorityIndex); + } + + /** + * Resolves a conflict according to the current priority order. + * + * @param claimants the list of claimants + * @return the highest priority player + */ + private PlayerID resolveConflict(List claimants) { + return this.currentPlayerPriorityOrder() + .stream() + .filter(claimants::contains) + .findFirst() + .get(); + } + } -- cgit v1.2.3