aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTimothée Floure2016-03-01 17:20:57 +0100
committerTimothée Floure2016-03-01 17:20:57 +0100
commite63054be16b46155968a748c83a600f28ac8c4aa (patch)
treeeb6f802209e5745005a70f49c572781307c943f9 /src
parent80143bb3522007a903f2a34967e0cf5b0d6b20e0 (diff)
downloadxblast-e63054be16b46155968a748c83a600f28ac8c4aa.tar.gz
Board Class : remove magic numbers + get ride of duplicated code
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/xblast/server/Board.java60
1 files changed, 32 insertions, 28 deletions
diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java
index 2cbf380..d47bdd3 100644
--- a/src/ch/epfl/xblast/server/Board.java
+++ b/src/ch/epfl/xblast/server/Board.java
@@ -14,6 +14,33 @@ import java.util.List;
14 * @author Timothée FLOURE (257420) 14 * @author Timothée FLOURE (257420)
15 */ 15 */
16public final class Board { 16public final class Board {
17 private static final int BLOCKS_LIST_SIZE = 195;
18 private static final int BOARD_ROWS = 13;
19 private static final int BOARD_COLUMNS = 15;
20 private static final int INNER_BOARD_ROWS = BOARD_ROWS - 2;
21 private static final int INNER_BOARD_COLUMNS = BOARD_COLUMNS - 2;
22 private static final int QUADRANT_ROWS = 6;
23 private static final int QUADRANT_COLUMNS = 7;
24
25 /**
26 * Throw an exeption if the matrix does not have the given number of rows/columns.
27 *
28 * @param matrix the tested matrix
29 * @param rows the expected number of rows
30 * @param columns the expected number of columns
31 * @throws IllegalArgumentException if the matrix does not comply with the given sizes.
32 */
33 private static void checkBlockMatrix(List<List<Block>> matrix, int rows, int columns) {
34 if (matrix.size() != rows) {
35 throw new IllegalArgumentException();
36 } else {
37 for (int i = 0; i < rows; i++) {
38 if (matrix.get(i).size() != columns) {
39 throw new IllegalArgumentException();
40 }
41 }
42 }
43 }
17 44
18 /** 45 /**
19 * List containing all the blocks of the board. 46 * List containing all the blocks of the board.
@@ -27,7 +54,7 @@ public final class Board {
27 * @throws IllegalArgumentException if the blocks is not composed of 195 elements 54 * @throws IllegalArgumentException if the blocks is not composed of 195 elements
28 */ 55 */
29 public Board(List<Sq<Block>> blocks) { 56 public Board(List<Sq<Block>> blocks) {
30 if (blocks.size() != 195) { 57 if (blocks.size() != BLOCKS_LIST_SIZE) {
31 throw new IllegalArgumentException(); 58 throw new IllegalArgumentException();
32 } 59 }
33 this.blocks = blocks; 60 this.blocks = blocks;
@@ -41,18 +68,10 @@ public final class Board {
41 * @throws IllegalArgumentException if rows is not 13*15 68 * @throws IllegalArgumentException if rows is not 13*15
42 */ 69 */
43 public static Board ofRows(List<List<Block>> rows) { 70 public static Board ofRows(List<List<Block>> rows) {
44 if (rows.size() != 13) { 71 checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS);
45 throw new IllegalArgumentException();
46 }
47
48 List<Sq<Block>> blocksSequence = new ArrayList<>(); 72 List<Sq<Block>> blocksSequence = new ArrayList<>();
49 73
50 for (int i = 0; i < rows.size(); i++) { 74 for (int i = 0; i < rows.size(); i++) {
51
52 if (rows.get(i).size() != 15) {
53 throw new IllegalArgumentException();
54 }
55
56 for (int j = 0; j < rows.get(i).size(); j++) { 75 for (int j = 0; j < rows.get(i).size(); j++) {
57 blocksSequence.add(Sq.constant(rows.get(i).get(j))); 76 blocksSequence.add(Sq.constant(rows.get(i).get(j)));
58 } 77 }
@@ -69,22 +88,15 @@ public final class Board {
69 * @throws IllegalArgumentException if innerbLocks is not 11*13 88 * @throws IllegalArgumentException if innerbLocks is not 11*13
70 */ 89 */
71 public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) { 90 public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) {
72 if (innerBlocks.size() != 11) { 91 checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS);
73 throw new IllegalArgumentException();
74 }
75
76 List<List<Block>> rowsList = new ArrayList<>(); 92 List<List<Block>> rowsList = new ArrayList<>();
77 List<Block> wallLine = new ArrayList<>(); 93 List<Block> wallLine = new ArrayList<>();
78 94
79 for (int i = 0; i < 15; i++) { 95 for (int i = 0; i < BOARD_COLUMNS; i++) {
80 wallLine.add(Block.INDESTRUCTIBLE_WALL); 96 wallLine.add(Block.INDESTRUCTIBLE_WALL);
81 } 97 }
82 98
83 for (int i = 0; i < innerBlocks.size(); i++) { 99 for (int i = 0; i < innerBlocks.size(); i++) {
84 if (innerBlocks.get(i).size() != 13) {
85 throw new IllegalArgumentException();
86 }
87
88 List<Block> row = innerBlocks.get(i); 100 List<Block> row = innerBlocks.get(i);
89 row.add(0, Block.INDESTRUCTIBLE_WALL); 101 row.add(0, Block.INDESTRUCTIBLE_WALL);
90 row.add(Block.INDESTRUCTIBLE_WALL); 102 row.add(Block.INDESTRUCTIBLE_WALL);
@@ -106,19 +118,11 @@ public final class Board {
106 * @throws IllegalArgumentException if quadrantNWBlocks is not 6*7 118 * @throws IllegalArgumentException if quadrantNWBlocks is not 6*7
107 */ 119 */
108 public static Board ofQuadrantNWBlocksWalled(List<List<Block>> quadrantNWBlocks) { 120 public static Board ofQuadrantNWBlocksWalled(List<List<Block>> quadrantNWBlocks) {
109 if (quadrantNWBlocks.size() != 6) { 121 checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS);
110 throw new IllegalArgumentException();
111 }
112
113 List<List<Block>> rowsList = new ArrayList<>(); 122 List<List<Block>> rowsList = new ArrayList<>();
114
115 List<List<Block>> halfInnerBoard = Lists.mirrored(quadrantNWBlocks); 123 List<List<Block>> halfInnerBoard = Lists.mirrored(quadrantNWBlocks);
116 124
117 for (int i = 0; i < halfInnerBoard.size(); i++) { 125 for (int i = 0; i < halfInnerBoard.size(); i++) {
118 if (halfInnerBoard.get(i).size() != 7) {
119 throw new IllegalArgumentException();
120 }
121
122 rowsList.add(Lists.mirrored(halfInnerBoard.get(i))); 126 rowsList.add(Lists.mirrored(halfInnerBoard.get(i)));
123 } 127 }
124 return ofInnerBlocksWalled(rowsList); 128 return ofInnerBlocksWalled(rowsList);