diff options
author | Pacien TRAN-GIRARD | 2016-04-09 22:58:43 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-04-09 22:58:43 +0200 |
commit | d0df7bd419bda6cf1a0389630ce09e9810ee4bff (patch) | |
tree | 7d723452837989bd92e338a644736d34279e4c33 | |
parent | 0355436f731a4cd6c63bc7d776a391bf43e19a41 (diff) | |
download | xblast-d0df7bd419bda6cf1a0389630ce09e9810ee4bff.tar.gz |
Fix matrix recurrent alterations using immutable block streams
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 12 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Board.java | 52 |
2 files changed, 35 insertions, 29 deletions
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 | |||
@@ -58,6 +58,18 @@ public final class Lists { | |||
58 | } | 58 | } |
59 | 59 | ||
60 | /** | 60 | /** |
61 | * Returns a copy of the given list surrounded with element e. | ||
62 | * | ||
63 | * @param l the list | ||
64 | * @param e the element to insert | ||
65 | * @param <T> the type of the list's elements | ||
66 | * @return a copy of the list with the element inserted at start and end | ||
67 | */ | ||
68 | public static <T> List<T> surrounded(List<T> l, T e) { | ||
69 | return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e); | ||
70 | } | ||
71 | |||
72 | /** | ||
61 | * Returns all the permutations of the elements of the given list | 73 | * Returns all the permutations of the elements of the given list |
62 | * | 74 | * |
63 | * @param l given list | 75 | * @param l 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; | |||
5 | import ch.epfl.xblast.Lists; | 5 | import ch.epfl.xblast.Lists; |
6 | 6 | ||
7 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
8 | import java.util.Collection; | ||
9 | import java.util.Collections; | ||
8 | import java.util.List; | 10 | import java.util.List; |
11 | import java.util.stream.Collectors; | ||
9 | 12 | ||
10 | /** | 13 | /** |
11 | * A two-dimensional Board in which the game takes place. | 14 | * A two-dimensional Board in which the game takes place. |
@@ -40,8 +43,8 @@ public final class Board { | |||
40 | if (matrix == null || matrix.size() != rows) | 43 | if (matrix == null || matrix.size() != rows) |
41 | throw new IllegalArgumentException(); | 44 | throw new IllegalArgumentException(); |
42 | 45 | ||
43 | for (int i = 0; i < rows; i++) | 46 | for (List<Block> row : matrix) |
44 | if (matrix.get(i).size() != columns) | 47 | if (row.size() != columns) |
45 | throw new IllegalArgumentException(); | 48 | throw new IllegalArgumentException(); |
46 | } | 49 | } |
47 | 50 | ||
@@ -71,14 +74,14 @@ public final class Board { | |||
71 | * @throws IllegalArgumentException if rows is not BOARD_ROWS * BOARD_COLUMNS | 74 | * @throws IllegalArgumentException if rows is not BOARD_ROWS * BOARD_COLUMNS |
72 | */ | 75 | */ |
73 | public static Board ofRows(List<List<Block>> rows) { | 76 | public static Board ofRows(List<List<Block>> rows) { |
74 | checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); | 77 | Board.checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); |
75 | List<Sq<Block>> blocksSequence = new ArrayList<>(); | ||
76 | 78 | ||
77 | for (List<Block> row : rows) | 79 | List<Sq<Block>> blockSequence = rows.stream() |
78 | for (Block aRow : row) | 80 | .flatMap(Collection::stream) |
79 | blocksSequence.add(Sq.constant(aRow)); | 81 | .map(Sq::constant) |
82 | .collect(Collectors.toList()); | ||
80 | 83 | ||
81 | return new Board(blocksSequence); | 84 | return new Board(blockSequence); |
82 | } | 85 | } |
83 | 86 | ||
84 | /** | 87 | /** |
@@ -89,24 +92,16 @@ public final class Board { | |||
89 | * @throws IllegalArgumentException if innerBlocks is not INNER_BOARD_ROWS * INNER_BOARD_COLUMNS | 92 | * @throws IllegalArgumentException if innerBlocks is not INNER_BOARD_ROWS * INNER_BOARD_COLUMNS |
90 | */ | 93 | */ |
91 | public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) { | 94 | public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) { |
92 | checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); | 95 | Board.checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); |
93 | List<List<Block>> rowsList = new ArrayList<>(); | ||
94 | List<Block> wallLine = new ArrayList<>(); | ||
95 | 96 | ||
96 | for (int i = 0; i < BOARD_COLUMNS; i++) | 97 | List<List<Block>> innerRows = innerBlocks.stream() |
97 | wallLine.add(Block.INDESTRUCTIBLE_WALL); | 98 | .map(r -> Lists.surrounded(r, Block.INDESTRUCTIBLE_WALL)) |
99 | .collect(Collectors.toList()); | ||
98 | 100 | ||
99 | for (List<Block> row : innerBlocks) { | 101 | List<List<Block>> completeMatrix = Lists.surrounded( |
100 | row.add(0, Block.INDESTRUCTIBLE_WALL); | 102 | innerRows, Collections.nCopies(BOARD_COLUMNS, Block.INDESTRUCTIBLE_WALL)); |
101 | row.add(Block.INDESTRUCTIBLE_WALL); | ||
102 | 103 | ||
103 | rowsList.add(row); | 104 | return Board.ofRows(completeMatrix); |
104 | } | ||
105 | |||
106 | rowsList.add(0, wallLine); | ||
107 | rowsList.add(wallLine); | ||
108 | |||
109 | return ofRows(rowsList); | ||
110 | } | 105 | } |
111 | 106 | ||
112 | /** | 107 | /** |
@@ -117,14 +112,13 @@ public final class Board { | |||
117 | * @throws IllegalArgumentException if quadrantNWBlocks is not QUADRANT_ROWS * QUADRANT_COLUMNS | 112 | * @throws IllegalArgumentException if quadrantNWBlocks is not QUADRANT_ROWS * QUADRANT_COLUMNS |
118 | */ | 113 | */ |
119 | public static Board ofQuadrantNWBlocksWalled(List<List<Block>> quadrantNWBlocks) { | 114 | public static Board ofQuadrantNWBlocksWalled(List<List<Block>> quadrantNWBlocks) { |
120 | checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS); | 115 | Board.checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS); |
121 | List<List<Block>> rowsList = new ArrayList<>(); | ||
122 | List<List<Block>> halfInnerBoard = Lists.mirrored(quadrantNWBlocks); | ||
123 | 116 | ||
124 | for (List<Block> aHalfInnerBoard : halfInnerBoard) | 117 | List<List<Block>> innerRows = Lists.mirrored(quadrantNWBlocks.stream() |
125 | rowsList.add(Lists.mirrored(aHalfInnerBoard)); | 118 | .map(Lists::mirrored) |
119 | .collect(Collectors.toList())); | ||
126 | 120 | ||
127 | return Board.ofInnerBlocksWalled(rowsList); | 121 | return Board.ofInnerBlocksWalled(innerRows); |
128 | } | 122 | } |
129 | 123 | ||
130 | /** | 124 | /** |