From 746aafc3f0e8212282a5fbf31de7a83d2a618dc8 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Sat, 27 Feb 2016 12:04:39 +0100 Subject: Add the Lists and Board Class + the Ticks Interface. Add the tests related to the '02_Board' part of the project. --- src/ch/epfl/xblast/Lists.java | 38 +++++++++++ src/ch/epfl/xblast/server/Block.java | 4 +- src/ch/epfl/xblast/server/Board.java | 123 +++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/ch/epfl/xblast/Lists.java create mode 100644 src/ch/epfl/xblast/server/Board.java (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java new file mode 100644 index 0000000..096ceba --- /dev/null +++ b/src/ch/epfl/xblast/Lists.java @@ -0,0 +1,38 @@ +package ch.epfl.xblast; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +/** + * @author Pacien TRAN-GIRARD (261948) + * @author Timothée FLOURE (257420) + */ +public final class Lists { + /** + * Return a sysmetric version of the list. + * + * @param l the input list + * @throws IllegalArgumentException if the given list is empty + * @return mirroredList the mirrored ilist + */ + public static List mirrored(List l) { + if (l.size() == 0) { + throw new IllegalArgumentException(); + } + + List mirroredList = new ArrayList(); + List rightSide = new ArrayList(); + + for (int i=0;i < l.size()-1;i++) { + rightSide.add(l.get(i)); + } + + Collections.reverse(rightSide); + + mirroredList.addAll(l); + mirroredList.addAll(rightSide); + + return mirroredList; + } +} diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index 75925a3..8268f33 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -45,7 +45,7 @@ public enum Block { * * @returns T(this block cast a shadow) */ - public boolean castsSahdow() { - return this == INDESTRUCTIBLE_WALL || this == DESTRUCTILE_WALL || this == CRUMBLING_WALL; + public boolean castsShadow() { + return this == INDESTRUCTIBLE_WALL || this == DESTRUCTIBLE_WALL || this == CRUMBLING_WALL; } } diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java new file mode 100644 index 0000000..c8d3e01 --- /dev/null +++ b/src/ch/epfl/xblast/server/Board.java @@ -0,0 +1,123 @@ +package ch.epfl.xblast.server; + +import java.util.List; +import java.util.ArrayList; +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.Lists; + +/** + * @author Pacien TRAN-GIRARD (261948) + * @author Timothée FLOURE (257420) + */ + +public final class Board { + /** + * List containing all the blocks of the board. + */ + private List> blocks; + + /** + * Instanciates a new Board with the given sequence of blocks. + * + * @throws IllegalArgumentEception if the blocks is not composed of 195 elements + * @param blocks sequence conataining all the blocks of the Boards + */ + public Board (List> blocks) { + if (blocks.size() != 195) { + throw new IllegalArgumentException(); + } + this.blocks = blocks; + } + + /** + * Build a new Board with the given Matrix. + * + * @param rows list containing all the rows + * @throws IllegalArgumentException if rows is not 13*15 + * @return a new Board built with given rows + */ + public static Board ofRows(List> rows) { + if (rows.size() != 13) { + throw new IllegalArgumentException(); + } + + 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))); + } + } + + return new Board(blocksSequence); + } + + /** + * Build a walled board filled with the given inner rows + * + * @param innerBlocks lists of the internal rows + * @throws IllegalArgumentException if innerbLocks is not 11*13 + * @return a new walled board filled with the given rows + */ + public static Board ofInnerBlocksWalled(List> innerBlocks) { + if (innerBlocks.size() != 11) { + throw new IllegalArgumentException(); + } + + List> rowsList = new ArrayList<>(); + List wallLine = new ArrayList<>(); + + for (int i = 0; i < 15;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); + + rowsList.add(row); + } + + rowsList.add(0,wallLine); + rowsList.add(wallLine); + + return ofRows(rowsList); + } + + /** + * Build a symetric walled board from the NWB quadrant. + * + * @param quadrantNWBlocks the NW quadrant of the board (inner blocks only!) + * @throws IllegalArgumentException if quadrantNWBlocks is not 6*7 + * @return a new walled board simmetricaly filled with the given NW quadrant + */ + public static Board ofQuadrantNWBlocksWalled(List> quadrantNWBlocks) { + if (quadrantNWBlocks.size() != 6) { + throw new IllegalArgumentException(); + } + + 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