From 2e521cc8863796355666ce1dde69d5e513073954 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 22:10:46 +0200 Subject: Modularize player priority management --- src/ch/epfl/xblast/server/GameState.java | 57 +-------------- .../epfl/xblast/server/PlayerPriorityManager.java | 82 ++++++++++++++++++++++ .../epfl/xblast/server/debug/GameStatePrinter.java | 3 +- 3 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 src/ch/epfl/xblast/server/PlayerPriorityManager.java (limited to 'src') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 7c56153..50b0ff2 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -17,11 +17,6 @@ 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(); - /** * The list of bonuses to choose randomly from. */ @@ -79,15 +74,6 @@ public final class GameState { this.blasts = Lists.immutableList(Objects.requireNonNull(blasts)); } - /** - * 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())); - } - /** * Returns a randomly chosen bonus Block with an equal probability distribution. * @@ -582,47 +568,6 @@ public final class GameState { return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1); } - /** - * 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) { - if (claimants == null || claimants.isEmpty()) return null; - - return this.currentPlayerPriorityOrder().stream() - .filter(claimants::contains) - .findFirst().orElse(null); - } - - /** - * Resolves a conflict according to the current priority order. - * - * @param claimants the list of claimants - * @return the highest priority player - */ - public Player resolvePlayerConflict(List claimants) { - if (claimants == null || claimants.isEmpty()) return null; - - Map claimantsMap = claimants.stream() - .collect(Collectors.toMap(Player::id, Function.identity())); - - List claimantsIDs = claimants.stream() - .map(Player::id).collect(Collectors.toList()); - - return claimantsMap.get(this.resolveConflict(claimantsIDs)); - } /** * Returns a mapping of players from their location. @@ -648,7 +593,7 @@ public final class GameState { return this.mapPlayersCells(players).entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, - e -> resolvePlayerConflict(e.getValue()))); + e -> PlayerPriorityManager.resolvePlayerConflict(e.getValue(), this.ticks()))); } /** diff --git a/src/ch/epfl/xblast/server/PlayerPriorityManager.java b/src/ch/epfl/xblast/server/PlayerPriorityManager.java new file mode 100644 index 0000000..ce6e12a --- /dev/null +++ b/src/ch/epfl/xblast/server/PlayerPriorityManager.java @@ -0,0 +1,82 @@ +package ch.epfl.xblast.server; + +import ch.epfl.xblast.Lists; +import ch.epfl.xblast.PlayerID; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * The player priority manager providing a sequential priority conflict resolution method. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public final class PlayerPriorityManager { + + /** + * The list of player priority order permutations. + */ + private static final List> PLAYER_PRIORITY_ORDERS = buildPlayerPriorityOrderList(); + + private PlayerPriorityManager() { + // Static class + } + + /** + * 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())); + } + + /** + * Returns the current player priority order permutation. + * + * @param ticks the current GameState ticks + * @return the player priority order + */ + private static List getCurrentPlayerPriorityOrder(int ticks) { + int priorityIndex = ticks % PLAYER_PRIORITY_ORDERS.size(); + return PLAYER_PRIORITY_ORDERS.get(priorityIndex); + } + + /** + * Resolves a conflict according to the current priority order. + * + * @param claimants the list of claimants + * @param ticks the current GameState ticks + * @return the highest priority player + */ + private static PlayerID resolveConflict(List claimants, int ticks) { + if (claimants == null || claimants.isEmpty()) return null; + + return getCurrentPlayerPriorityOrder(ticks).stream() + .filter(claimants::contains) + .findFirst().orElse(null); + } + + /** + * Resolves a conflict according to the current priority order. + * + * @param claimants the list of claimants + * @param ticks the current GameState ticks + * @return the highest priority player + */ + public static Player resolvePlayerConflict(List claimants, int ticks) { + if (claimants == null || claimants.isEmpty()) return null; + + Map claimantsMap = claimants.stream() + .collect(Collectors.toMap(Player::id, Function.identity())); + + List claimantsIDs = claimants.stream() + .map(Player::id).collect(Collectors.toList()); + + return claimantsMap.get(resolveConflict(claimantsIDs, ticks)); + } + +} diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java index c3d9338..bb02843 100644 --- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java +++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java @@ -5,6 +5,7 @@ import ch.epfl.xblast.Direction; import ch.epfl.xblast.server.Block; import ch.epfl.xblast.server.GameState; import ch.epfl.xblast.server.Player; +import ch.epfl.xblast.server.PlayerPriorityManager; import java.util.List; import java.util.Map; @@ -45,7 +46,7 @@ public final class GameStatePrinter { if (s.bombedCells().containsKey(c)) return ANSIColor.RED.coloredText("@@"); - Player p = s.resolvePlayerConflict(pl.get(c)); + Player p = PlayerPriorityManager.resolvePlayerConflict(pl.get(c), s.ticks()); if (p != null) return GameStatePrinter.stringForPlayer(p); -- cgit v1.2.3