From 552085b0fc886d93680bde42af14d1e161b33380 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 7 Apr 2016 16:14:59 +0200 Subject: Make nextBoard great again --- src/ch/epfl/xblast/server/Board.java | 9 ------ src/ch/epfl/xblast/server/GameState.java | 51 ++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java index d7ca706..bff3ea8 100644 --- a/src/ch/epfl/xblast/server/Board.java +++ b/src/ch/epfl/xblast/server/Board.java @@ -143,13 +143,4 @@ public final class Board { return this.blocksAt(c).head(); } - /** - * Return the blocks of the Board. - * - * @return a list of the Sequences of blocks of the board. - */ - public List> getBlocks() { - List> immutableBlocks = Collections.unmodifiableList(new ArrayList<>(blocks)); - return immutableBlocks; - } } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 36709e9..10b648b 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -120,29 +120,36 @@ public final class GameState { * @return the next board */ private static Board nextBoard(Board board0, Set consumedBonuses, Set blastedCells1) { - List> blocks0 = board0.getBlocks(); - List> blocks1 = new ArrayList<>(); - - int i = 0; - for (Sq blockSq : blocks0) { - int cellId = blocks0.get(i).hashCode(); - Block block = blockSq.head(); - if (consumedBonuses.contains(cellId) && block.isBonus()) { - blocks1.add(Sq.constant(Block.FREE)); - } else if (blastedCells1.contains(cellId) && (block == Block.DESTRUCTIBLE_WALL || block.isBonus())) { - if (block == Block.DESTRUCTIBLE_WALL) { - Block bonus = randomBonus(); - blocks1.add(Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL).concat(Sq.constant(bonus))); - } else { - blocks1.add(Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, block).concat(Sq.constant(Block.FREE))); - } - } else { - blocks1.add(blockSq.tail()); - } - i++; - } + return new Board(Cell.ROW_MAJOR_ORDER.stream() + .map(c -> GameState.nextBlockSeq(c, board0.blocksAt(c), consumedBonuses, blastedCells1)) + .collect(Collectors.toList())); + } + + /** + * Returns the next Block sequence for the given cell according to the current state and given events. + * + * @param c the Cell + * @param bs0 the previous Block sequence + * @param consumedBonuses the bonus consumption event + * @param blastedCells1 the new Cell blast events + * @return the new Block sequence + */ + private static Sq nextBlockSeq(Cell c, Sq bs0, Set consumedBonuses, Set blastedCells1) { + Block b = bs0.head(); + + if (consumedBonuses.contains(c) && b.isBonus()) + return Sq.constant(Block.FREE); + + if (blastedCells1.contains(c)) + if (b == Block.DESTRUCTIBLE_WALL) + return Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL) + .concat(Sq.constant(GameState.randomBonus())); + + else if (b.isBonus()) + return Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, b) + .concat(Sq.constant(Block.FREE)); - return new Board(blocks1); + return bs0.tail(); } /** -- cgit v1.2.3