From 492ca4fe5b9b193948274419d6361c2829eb10bf Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 8 Apr 2016 11:35:08 +0200 Subject: Make player collisions great again --- src/ch/epfl/xblast/server/GameState.java | 48 +++++++++++++++------- .../epfl/xblast/server/debug/GameStatePrinter.java | 29 +++++++------ 2 files changed, 47 insertions(+), 30 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index c51afe7..4ed1af5 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -473,13 +473,29 @@ public final class GameState { * @return the highest priority player */ private PlayerID resolveConflict(List claimants) { - if (claimants == null || claimants.isEmpty()) throw new IllegalArgumentException(); + if (claimants == null || claimants.isEmpty()) return null; - return this.currentPlayerPriorityOrder() - .stream() + return this.currentPlayerPriorityOrder().stream() .filter(claimants::contains) - .findFirst() - .get(); + .findFirst().get(); + } + + /** + * 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)); } /** @@ -488,11 +504,11 @@ public final class GameState { * @param players a list of players to map * @return the location->player mapping */ - private Map> mapPlayersCells(List players) { + public Map> mapPlayersCells(List players) { return players.stream() .collect( Collectors.groupingBy(p -> p.position().containingCell(), - Collectors.mapping(Player::id, Collectors.toList()))); + Collectors.mapping(Function.identity(), Collectors.toList()))); } /** @@ -502,11 +518,11 @@ public final class GameState { * @param players a list of players to map * @return the location->top-priority player mapping */ - private Map mapTopPriorityPlayerCells(List players) { + private Map mapTopPriorityPlayerCells(List players) { return this.mapPlayersCells(players).entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, - e -> resolveConflict(e.getValue()))); + e -> resolvePlayerConflict(e.getValue()))); } /** @@ -519,7 +535,9 @@ public final class GameState { this.alivePlayers().stream() .filter(p -> p.position().isCentral()) .filter(p -> this.board.blockAt(p.position().containingCell()).isBonus()) - .collect(Collectors.toList())); + .collect(Collectors.toList())) + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().id())); } /** @@ -542,13 +560,13 @@ public final class GameState { * @return the conflict-free set of bomb drop events */ private Set discardConflictingBombDropEvents(Set bombDropEvents) { - Map bombDropMap = this.mapTopPriorityPlayerCells( + return this.mapTopPriorityPlayerCells( this.alivePlayers().stream() .filter(p -> bombDropEvents.contains(p.id())) - .collect(Collectors.toList()) - ); - - return bombDropMap.isEmpty() ? EnumSet.noneOf(PlayerID.class) : EnumSet.copyOf(bombDropMap.values()); + .collect(Collectors.toList())) + .values().stream() + .map(Player::id) + .collect(Collectors.toSet()); } @Override diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java index d81c6b9..575b09a 100644 --- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java +++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java @@ -1,13 +1,12 @@ package ch.epfl.xblast.server.debug; import ch.epfl.xblast.Cell; -import ch.epfl.xblast.server.*; +import ch.epfl.xblast.server.Block; +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.Player; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; /** * Game state printer utility class that outputs the board to the terminal. @@ -40,36 +39,36 @@ public final class GameStatePrinter { public static void printGameState(GameState s) { printStats(s); - printBoard(s.board(), s.alivePlayers(), s.bombedCells(), s.blastedCells()); + printBoard(s); } private static void printStats(GameState s) { System.out.println(s); } - private static void printBoard(Board b, List ps, Map bc, Set blastedCells) { - Map playerMap = ps.stream() - .collect(Collectors.toMap(p -> p.position().containingCell(), Function.identity())); + private static void printBoard(GameState s) { + Map> playerMap = s.mapPlayersCells(s.alivePlayers()); for (int y = 0; y < Cell.ROWS; ++y) { for (int x = 0; x < Cell.COLUMNS; ++x) - System.out.print(GameStatePrinter.stringForCellContent(new Cell(x, y), b, bc, playerMap, blastedCells)); + System.out.print(GameStatePrinter.stringForCellContent(s, new Cell(x, y), playerMap)); System.out.println(); } } - private static String stringForCellContent(Cell c, Board b, Map bc, Map p, Set blastedCells) { - if (bc.containsKey(c)) + private static String stringForCellContent(GameState s, Cell c, Map> pl) { + if (s.bombedCells().containsKey(c)) return ANSIColor.RED.coloredText("@@"); - if (p.containsKey(c)) - return GameStatePrinter.stringForPlayer(p.get(c)); + Player p = s.resolvePlayerConflict(pl.get(c)); + if (p != null) + return GameStatePrinter.stringForPlayer(p); - if (blastedCells.contains(c)) + if (s.blastedCells().contains(c)) return ANSIColor.MAGENTA.coloredText("**"); - return GameStatePrinter.stringForBlock(b.blockAt(c)); + return GameStatePrinter.stringForBlock(s.board().blockAt(c)); } private static String stringForPlayer(Player p) { -- cgit v1.2.3