From 47de23b97f65b9c97e7bce4bb2f4a496e5190bea Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Mar 2016 20:36:43 +0100 Subject: Make filters and mappers static for easier future state computation --- src/ch/epfl/xblast/server/GameState.java | 51 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index cbe29b5..d750732 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -27,7 +27,42 @@ public final class GameState { private final List> blasts; /** - * Compute the next state of a blast. + * Filters the given list of players and returns the lively ones. + * + * @param players a list of players + * @return the list of alive players + */ + private static List alivePlayers(List players) { + return players.stream() + .filter(Player::isAlive) + .collect(Collectors.toList()); + } + + /** + * Maps the given bombs to their position. + * + * @param bombs a list of bombs + * @return a map of positions and their bombs + */ + private static Map bombedCells(List bombs) { + return bombs.stream() + .collect(Collectors.toMap(Bomb::position, Function.identity())); + } + + /** + * Returns a set of cells that contains at least one blast in the given blasts sequences. + * + * @param blasts the list of blast sequences + * @return the set of blasted cells + */ + private static Set blastedCells(List> blasts) { + return blasts.stream() + .map(Sq::head) + .collect(Collectors.toSet()); + } + + /** + * Computes the next state of a blast. * * @param blasts0 existing particles * @param board0 the game's board @@ -194,10 +229,7 @@ public final class GameState { * @return a list of the alive players */ public List alivePlayers() { - return this.players - .stream() - .filter(Player::isAlive) - .collect(Collectors.toList()); + return GameState.alivePlayers(this.players); } /** @@ -206,9 +238,7 @@ public final class GameState { * @return the map of bombs */ public Map bombedCells() { - return this.bombs - .stream() - .collect(Collectors.toMap(Bomb::position, Function.identity())); + return GameState.bombedCells(this.bombs); } /** @@ -217,10 +247,7 @@ public final class GameState { * @return the set of blasted cells */ public Set blastedCells() { - return this.blasts - .stream() - .map(Sq::head) - .collect(Collectors.toSet()); + return GameState.blastedCells(this.blasts); } /** -- cgit v1.2.3