From a33c9df30d209e8ba24b51507ffdeab17008dbe8 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 6 Apr 2016 23:06:07 +0200 Subject: Make newlyDroppedBombs more readable --- src/ch/epfl/xblast/server/GameState.java | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 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 c58a911..3495af1 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -240,30 +240,27 @@ public final class GameState { /** * 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. * * @param players0 the previous player states * @param bombDropEvents the bomb drop events * @param bombs0 the previous bomb state * @return the newly dropped bombs */ - private static List newlyDroppedBombs( - List players0, - Set bombDropEvents, - List bombs0) { + private static List newlyDroppedBombs(List players0, Set bombDropEvents, List bombs0) { + Set bombedCells = GameState.bombedCells(bombs0).keySet(); + List bombs1 = new ArrayList<>(); - Set containingBombCells = new HashSet<>(); - for (Bomb bomb : bombs0) { - containingBombCells.add(bomb.position()); - } + for (Player p : GameState.alivePlayers(players0)) { + if (!bombDropEvents.contains(p.id())) continue; + if (bombedCells.contains(p.position().containingCell())) continue; - for (Player player : players0) { - if (bombDropEvents.contains(player.id()) && player.isAlive() && !containingBombCells.contains(player.position().containingCell())) { - Bomb newBomb = new Bomb(player.id(), player.position().containingCell(), Ticks.BOMB_FUSE_TICKS, player.bombRange()); - containingBombCells.add(newBomb.position()); - bombs1.add(newBomb); - } + Bomb b = new Bomb(p.id(), p.position().containingCell(), Ticks.BOMB_FUSE_TICKS, p.bombRange()); + bombedCells.add(b.position()); + bombs1.add(b); } + return bombs1; } -- cgit v1.2.3