From 74e7fe38193d28d58f41d23ccd41634b988394ef Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 14:13:35 +0100 Subject: Simplify code --- src/ch/epfl/xblast/server/Block.java | 4 +- src/ch/epfl/xblast/server/GameState.java | 78 +++++++++++--------------------- 2 files changed, 28 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index c90a469..5a139ff 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -48,14 +48,14 @@ public enum Block { /** * Main builder, used by the bonus blocks */ - private Block(Bonus maybeAssociatedBonus) { + Block(Bonus maybeAssociatedBonus) { this.maybeAssociatedBonus = maybeAssociatedBonus; } /** * Default builder, used by the non-bonus blocks */ - private Block() { + Block() { this.maybeAssociatedBonus = null; } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index e7e6ca7..7e93812 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * GameState representing the current game state. @@ -29,22 +30,20 @@ public final class GameState { /** * Compute the next state of a blast. * - * @param blasts0 existing particles - * @param board0 the game's board - * @param explosions0 active explosions + * @param blasts existing particles + * @param board the game's board + * @param explosions active explosions * @return the position of the explosion's particles for the next state. */ - private static List> nextBlasts(List> blasts0, Board board0, List>> explosions0) { - List> blasts1 = new ArrayList<>(); - for (Sq blastSeq : blasts0) { - if (!blastSeq.tail().isEmpty() && board0.blockAt(blastSeq.head()).isFree()) { - blasts1.add(blastSeq.tail()); - } - } - for (Sq> explosion0 : explosions0) { - blasts1.add(explosion0.head()); - } - return blasts1; + private static List> nextBlasts(List> blasts, Board board, List>> explosions) { + return Stream.concat( + blasts.stream() + .filter(blastSeq -> !blastSeq.tail().isEmpty()) + .filter(blastSeq -> board.blockAt(blastSeq.head()).isFree()) + .map(Sq::tail), + explosions.stream() + .map(Sq::head) + ).collect(Collectors.toList()); } /** @@ -62,11 +61,10 @@ public final class GameState { public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { this.ticks = ArgumentChecker.requireNonNegative(ticks); this.board = Objects.requireNonNull(board); - if (players.size() == 4) { - this.players = players; - } else { - throw new IllegalArgumentException(); - } + + if (players.size() != 4) throw new IllegalArgumentException(); + this.players = players; + this.bombs = Objects.requireNonNull(bombs); this.explosions = Objects.requireNonNull(explosions); this.blasts = Objects.requireNonNull(blasts); @@ -79,14 +77,7 @@ public final class GameState { * @param players list of the players */ public GameState(Board board, List players) { - this(0, - board, - players, - new ArrayList(), - new ArrayList>>(), - new ArrayList>() - ); - + this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); } /** @@ -102,17 +93,7 @@ public final class GameState { * @return true if all the players are dead or if the game reached the time limit */ public boolean isGameOver() { - int alivePlayerCount = 0; - for (Player player : this.players) { - if (player.isAlive()) { - alivePlayerCount += 1; - } - } - if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { - return true; - } else { - return false; - } + return this.alivePlayers().isEmpty() || this.ticks >= Ticks.TOTAL_TICKS; } /** @@ -130,12 +111,10 @@ public final class GameState { * @return the ID of the player who won the game. */ public Optional winner() { - if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { - PlayerID winnerId = this.players.get(0).id(); - return Optional.of(winnerId); - } else { + if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) + return Optional.of(this.players.get(0).id()); + else return Optional.empty(); - } } /** @@ -158,15 +137,10 @@ public final class GameState { * @return a list of the alive players */ public List alivePlayers() { - List alivePlayers = new ArrayList<>(); - - for (Player player : this.players) { - if (player.isAlive()) { - alivePlayers.add(player); - } - } - - return alivePlayers; + return this.players + .stream() + .filter(Player::isAlive) + .collect(Collectors.toList()); } } -- cgit v1.2.3