From e63054be16b46155968a748c83a600f28ac8c4aa Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Tue, 1 Mar 2016 17:20:57 +0100 Subject: Board Class : remove magic numbers + get ride of duplicated code --- src/ch/epfl/xblast/server/Board.java | 60 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 28 deletions(-) (limited to 'src/ch') 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; * @author Timothée FLOURE (257420) */ public final class Board { + private static final int BLOCKS_LIST_SIZE = 195; + private static final int BOARD_ROWS = 13; + private static final int BOARD_COLUMNS = 15; + private static final int INNER_BOARD_ROWS = BOARD_ROWS - 2; + private static final int INNER_BOARD_COLUMNS = BOARD_COLUMNS - 2; + private static final int QUADRANT_ROWS = 6; + private static final int QUADRANT_COLUMNS = 7; + + /** + * Throw an exeption if the matrix does not have the given number of rows/columns. + * + * @param matrix the tested matrix + * @param rows the expected number of rows + * @param columns the expected number of columns + * @throws IllegalArgumentException if the matrix does not comply with the given sizes. + */ + private static void checkBlockMatrix(List> matrix, int rows, int columns) { + if (matrix.size() != rows) { + throw new IllegalArgumentException(); + } else { + for (int i = 0; i < rows; i++) { + if (matrix.get(i).size() != columns) { + throw new IllegalArgumentException(); + } + } + } + } /** * List containing all the blocks of the board. @@ -27,7 +54,7 @@ public final class Board { * @throws IllegalArgumentException if the blocks is not composed of 195 elements */ public Board(List> blocks) { - if (blocks.size() != 195) { + if (blocks.size() != BLOCKS_LIST_SIZE) { throw new IllegalArgumentException(); } this.blocks = blocks; @@ -41,18 +68,10 @@ public final class Board { * @throws IllegalArgumentException if rows is not 13*15 */ public static Board ofRows(List> rows) { - if (rows.size() != 13) { - throw new IllegalArgumentException(); - } - + checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); List> blocksSequence = new ArrayList<>(); for (int i = 0; i < rows.size(); i++) { - - if (rows.get(i).size() != 15) { - throw new IllegalArgumentException(); - } - for (int j = 0; j < rows.get(i).size(); j++) { blocksSequence.add(Sq.constant(rows.get(i).get(j))); } @@ -69,22 +88,15 @@ public final class Board { * @throws IllegalArgumentException if innerbLocks is not 11*13 */ public static Board ofInnerBlocksWalled(List> innerBlocks) { - if (innerBlocks.size() != 11) { - throw new IllegalArgumentException(); - } - + checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); List> rowsList = new ArrayList<>(); List wallLine = new ArrayList<>(); - for (int i = 0; i < 15; i++) { + for (int i = 0; i < BOARD_COLUMNS; i++) { wallLine.add(Block.INDESTRUCTIBLE_WALL); } for (int i = 0; i < innerBlocks.size(); i++) { - if (innerBlocks.get(i).size() != 13) { - throw new IllegalArgumentException(); - } - List row = innerBlocks.get(i); row.add(0, Block.INDESTRUCTIBLE_WALL); row.add(Block.INDESTRUCTIBLE_WALL); @@ -106,19 +118,11 @@ public final class Board { * @throws IllegalArgumentException if quadrantNWBlocks is not 6*7 */ public static Board ofQuadrantNWBlocksWalled(List> quadrantNWBlocks) { - if (quadrantNWBlocks.size() != 6) { - throw new IllegalArgumentException(); - } - + checkBlockMatrix(quadrantNWBlocks, QUADRANT_ROWS, QUADRANT_COLUMNS); List> rowsList = new ArrayList<>(); - List> halfInnerBoard = Lists.mirrored(quadrantNWBlocks); for (int i = 0; i < halfInnerBoard.size(); i++) { - if (halfInnerBoard.get(i).size() != 7) { - throw new IllegalArgumentException(); - } - rowsList.add(Lists.mirrored(halfInnerBoard.get(i))); } return ofInnerBlocksWalled(rowsList); -- cgit v1.2.3