From 3f322fd077aae6323ac162d5b6af5b1cfeda7d67 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 7 Apr 2016 22:12:35 +0200 Subject: Make explosions great again --- src/ch/epfl/xblast/server/GameState.java | 52 ++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index c9a23ac..31bb902 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -244,6 +244,33 @@ public final class GameState { .collect(Collectors.toList()); } + /** + * Returns a list of exploding bombs (reached by a blast or consumed fuse). + * + * @param bombs the list of bombs + * @param blastedCells the set of blasted cells + * @return the list of exploding bombs + */ + private static List explodingBombs(List bombs, Set blastedCells) { + return bombs.stream() + .filter(b -> blastedCells.contains(b.position()) || b.fuseLength() == 0) + .collect(Collectors.toList()); + } + + /** + * Computes the consumption of the bombs. + * + * @param bombs0 the list of bombs + * @param explodingBombs the list of exploding bombs + * @return the new bombs states + */ + private static List nextBombs(List bombs0, List explodingBombs) { + return bombs0.stream() + .filter(b -> !explodingBombs.contains(b)) + .map(b -> new Bomb(b.ownerId(), b.position(), b.fuseLengths().tail(), b.range())) + .collect(Collectors.toList()); + } + /** * Computes and returns the list of newly dropped bombs by players according to the given player states and events. * Given bomb drop events must be conflict-free. @@ -255,7 +282,7 @@ public final class GameState { */ private static List newlyDroppedBombs(List players0, Set bombDropEvents, List bombs0) { HashSet bombedCells = new HashSet<>(GameState.bombedCells(bombs0).keySet()); - List bombs1 = new ArrayList<>(); + List bombs1 = new ArrayList<>(bombs0); for (Player p : GameState.alivePlayers(players0)) { if (!bombDropEvents.contains(p.id())) continue; @@ -409,23 +436,23 @@ public final class GameState { // 4.1. existing bombs evolution Set blastedCells0 = this.blastedCells(); - List explodingBombs = this.filterExplodingBombs(this.bombs, blastedCells0); - List bombs = this.bombs.stream().filter(b -> !explodingBombs.contains(b)).collect(Collectors.toList()); + List explodingBombs = GameState.explodingBombs(this.bombs, blastedCells0); + List existingBombs = GameState.nextBombs(this.bombs, explodingBombs); // 4.2. subsequent explosions addition explodingBombs.forEach(b -> explosions1.addAll(b.explosion())); // 4.3. newly dropped bombs addition Set topPriorityBombDropEvents = discardConflictingBombDropEvents(bombDropEvents); - bombs.addAll(GameState.newlyDroppedBombs(this.players, topPriorityBombDropEvents, this.bombs)); + List bombs1 = GameState.newlyDroppedBombs(this.players, topPriorityBombDropEvents, existingBombs); // 5. players evolution Map playerBonuses = mapPlayersToBonuses(consumedBonuses); - Set bombedCells1 = GameState.bombedCells(bombs).keySet(); + Set bombedCells1 = GameState.bombedCells(bombs1).keySet(); List players1 = GameState.nextPlayers( this.players, playerBonuses, bombedCells1, board1, blastedCells1, speedChangeEvents); - return new GameState(this.ticks, board1, players1, bombs, explosions1, blasts1); + return new GameState(this.ticks, board1, players1, bombs1, explosions1, blasts1); } /** @@ -523,19 +550,6 @@ public final class GameState { return bombDropMap.isEmpty() ? EnumSet.noneOf(PlayerID.class) : EnumSet.copyOf(bombDropMap.values()); } - /** - * Returns a list of exploding bombs (reached by a blast or consumed fuse). - * - * @param bombs the list of bombs - * @param blastedCells the set of blasted cells - * @return the list of exploding bombs - */ - private List filterExplodingBombs(List bombs, Set blastedCells) { - return bombs.stream() - .filter(b -> blastedCells.contains(b.position()) || b.fuseLength() == 0) - .collect(Collectors.toList()); - } - @Override public String toString() { return "GameState{" + -- cgit v1.2.3