From d0df7bd419bda6cf1a0389630ce09e9810ee4bff Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 9 Apr 2016 22:58:43 +0200 Subject: Fix matrix recurrent alterations using immutable block streams --- src/ch/epfl/xblast/Lists.java | 12 +++++++++ src/ch/epfl/xblast/server/Board.java | 52 ++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 29 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index bdcfb6a..1f2e1c7 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -57,6 +57,18 @@ public final class Lists { return r; } + /** + * Returns a copy of the given list surrounded with element e. + * + * @param l the list + * @param e the element to insert + * @param the type of the list's elements + * @return a copy of the list with the element inserted at start and end + */ + public static List surrounded(List l, T e) { + return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e); + } + /** * Returns all the permutations of the elements of the given list * diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java index e449a00..bcb94a2 100644 --- a/src/ch/epfl/xblast/server/Board.java +++ b/src/ch/epfl/xblast/server/Board.java @@ -5,7 +5,10 @@ import ch.epfl.xblast.Cell; import ch.epfl.xblast.Lists; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * A two-dimensional Board in which the game takes place. @@ -40,8 +43,8 @@ public final class Board { if (matrix == null || matrix.size() != rows) throw new IllegalArgumentException(); - for (int i = 0; i < rows; i++) - if (matrix.get(i).size() != columns) + for (List row : matrix) + if (row.size() != columns) throw new IllegalArgumentException(); } @@ -71,14 +74,14 @@ public final class Board { * @throws IllegalArgumentException if rows is not BOARD_ROWS * BOARD_COLUMNS */ public static Board ofRows(List> rows) { - checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); - List> blocksSequence = new ArrayList<>(); + Board.checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); - for (List row : rows) - for (Block aRow : row) - blocksSequence.add(Sq.constant(aRow)); + List> blockSequence = rows.stream() + .flatMap(Collection::stream) + .map(Sq::constant) + .collect(Collectors.toList()); - return new Board(blocksSequence); + return new Board(blockSequence); } /** @@ -89,24 +92,16 @@ public final class Board { * @throws IllegalArgumentException if innerBlocks is not INNER_BOARD_ROWS * INNER_BOARD_COLUMNS */ public static Board ofInnerBlocksWalled(List> innerBlocks) { - checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); - List> rowsList = new ArrayList<>(); - List wallLine = new ArrayList<>(); + Board.checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); - for (int i = 0; i < BOARD_COLUMNS; i++) - wallLine.add(Block.INDESTRUCTIBLE_WALL); + List> innerRows = innerBlocks.stream() + .map(r -> Lists.surrounded(r, Block.INDESTRUCTIBLE_WALL)) + .collect(Collectors.toList()); - for (List row : innerBlocks) { - row.add(0, Block.INDESTRUCTIBLE_WALL); - row.add(Block.INDESTRUCTIBLE_WALL); + List> completeMatrix = Lists.surrounded( + innerRows, Collections.nCopies(BOARD_COLUMNS, Block.INDESTRUCTIBLE_WALL)); - rowsList.add(row); - } - - rowsList.add(0, wallLine); - rowsList.add(wallLine); - - return ofRows(rowsList); + return Board.ofRows(completeMatrix); } /** @@ -117,14 +112,13 @@ public final class Board { * @throws IllegalArgumentException if quadrantNWBlocks is not QUADRANT_ROWS * QUADRANT_COLUMNS */ public static Board ofQuadrantNWBlocksWalled(List> quadrantNWBlocks) { - checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS); - List> rowsList = new ArrayList<>(); - List> halfInnerBoard = Lists.mirrored(quadrantNWBlocks); + Board.checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS); - for (List aHalfInnerBoard : halfInnerBoard) - rowsList.add(Lists.mirrored(aHalfInnerBoard)); + List> innerRows = Lists.mirrored(quadrantNWBlocks.stream() + .map(Lists::mirrored) + .collect(Collectors.toList())); - return Board.ofInnerBlocksWalled(rowsList); + return Board.ofInnerBlocksWalled(innerRows); } /** -- cgit v1.2.3