aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/server/Board.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/server/Board.java')
-rw-r--r--src/ch/epfl/xblast/server/Board.java145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java
new file mode 100644
index 0000000..5e03671
--- /dev/null
+++ b/src/ch/epfl/xblast/server/Board.java
@@ -0,0 +1,145 @@
1package ch.epfl.xblast.server;
2
3import ch.epfl.cs108.Sq;
4import ch.epfl.xblast.Cell;
5import ch.epfl.xblast.Lists;
6
7import java.util.ArrayList;
8import java.util.List;
9
10/**
11 * A two-dimensional Board in which the game takes place.
12 *
13 * @author Pacien TRAN-GIRARD (261948)
14 * @author Timothée FLOURE (257420)
15 */
16public final class Board {
17
18 private static final int BLOCKS_LIST_SIZE = 195;
19 private static final int BOARD_ROWS = 13;
20 private static final int BOARD_COLUMNS = 15;
21 private static final int INNER_BOARD_ROWS = BOARD_ROWS - 2;
22 private static final int INNER_BOARD_COLUMNS = BOARD_COLUMNS - 2;
23 private static final int QUADRANT_ROWS = 6;
24 private static final int QUADRANT_COLUMNS = 7;
25
26 /**
27 * Throw an exception if the matrix does not have the given number of rows/columns.
28 *
29 * @param matrix the tested matrix
30 * @param rows the expected number of rows
31 * @param columns the expected number of columns
32 * @throws IllegalArgumentException if the matrix does not comply with the given sizes.
33 */
34 private static void checkBlockMatrix(List<List<Block>> matrix, int rows, int columns) {
35 if (matrix == null || matrix.size() != rows)
36 throw new IllegalArgumentException();
37
38 for (int i = 0; i < rows; i++)
39 if (matrix.get(i).size() != columns)
40 throw new IllegalArgumentException();
41 }
42
43 /**
44 * List containing all the blocks of the board.
45 */
46 private List<Sq<Block>> blocks;
47
48 /**
49 * Instantiates a new Board with the given sequence of blocks.
50 *
51 * @param blocks sequence containing all the blocks of the Boards
52 * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements
53 */
54 public Board(List<Sq<Block>> blocks) {
55 if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE)
56 throw new IllegalArgumentException();
57
58 this.blocks = blocks;
59 }
60
61 /**
62 * Build a new Board with the given Matrix.
63 *
64 * @param rows list containing all the rows
65 * @return a new Board built with given rows
66 * @throws IllegalArgumentException if rows is not BOARD_ROWS * BOARD_COLUMNS
67 */
68 public static Board ofRows(List<List<Block>> rows) {
69 checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS);
70 List<Sq<Block>> blocksSequence = new ArrayList<>();
71
72 for (List<Block> row : rows)
73 for (Block aRow : row)
74 blocksSequence.add(Sq.constant(aRow));
75
76 return new Board(blocksSequence);
77 }
78
79 /**
80 * Build a walled board filled with the given inner rows
81 *
82 * @param innerBlocks lists of the internal rows
83 * @return a new walled board filled with the given rows
84 * @throws IllegalArgumentException if innerBlocks is not INNER_BOARD_ROWS * INNER_BOARD_COLUMNS
85 */
86 public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) {
87 checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS);
88 List<List<Block>> rowsList = new ArrayList<>();
89 List<Block> wallLine = new ArrayList<>();
90
91 for (int i = 0; i < BOARD_COLUMNS; i++)
92 wallLine.add(Block.INDESTRUCTIBLE_WALL);
93
94 for (List<Block> row : innerBlocks) {
95 row.add(0, Block.INDESTRUCTIBLE_WALL);
96 row.add(Block.INDESTRUCTIBLE_WALL);
97
98 rowsList.add(row);
99 }
100
101 rowsList.add(0, wallLine);
102 rowsList.add(wallLine);
103
104 return ofRows(rowsList);
105 }
106
107 /**
108 * Build a symmetric walled board from the NWB quadrant.
109 *
110 * @param quadrantNWBlocks the NW quadrant of the board (inner blocks only!)
111 * @return a new walled board symmetrically filled with the given NW quadrant
112 * @throws IllegalArgumentException if quadrantNWBlocks is not QUADRANT_ROWS * QUADRANT_COLUMNS
113 */
114 public static Board ofQuadrantNWBlocksWalled(List<List<Block>> quadrantNWBlocks) {
115 checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS);
116 List<List<Block>> rowsList = new ArrayList<>();
117 List<List<Block>> halfInnerBoard = Lists.mirrored(quadrantNWBlocks);
118
119 for (List<Block> aHalfInnerBoard : halfInnerBoard)
120 rowsList.add(Lists.mirrored(aHalfInnerBoard));
121
122 return ofInnerBlocksWalled(rowsList);
123 }
124
125 /**
126 * Return the sequence of blocks related to the given cell.
127 *
128 * @param c cell
129 * @return the sequence of blocks related to the given cell.
130 */
131 public Sq<Block> blocksAt(Cell c) {
132 return blocks.get(c.rowMajorIndex());
133 }
134
135 /**
136 * Returns the block related to the given cell.
137 *
138 * @param c cell
139 * @return the first block of the sequence related to the given cell
140 */
141 public Block blockAt(Cell c) {
142 return blocksAt(c).head();
143 }
144
145}