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 ++++++++++++++++++++++ test/ch/epfl/xblast/ListsTest.java | 32 ++++++ test/ch/epfl/xblast/namecheck/NameCheck02.java | 60 +++++++++++ test/ch/epfl/xblast/server/BlockTest.java | 35 +++++++ test/ch/epfl/xblast/server/BoardTest.java | 137 +++++++++++++++++++++++++ 7 files changed, 427 insertions(+), 2 deletions(-) create mode 100644 src/ch/epfl/xblast/Lists.java create mode 100644 src/ch/epfl/xblast/server/Board.java create mode 100644 test/ch/epfl/xblast/ListsTest.java create mode 100644 test/ch/epfl/xblast/namecheck/NameCheck02.java create mode 100644 test/ch/epfl/xblast/server/BlockTest.java create mode 100644 test/ch/epfl/xblast/server/BoardTest.java 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); + } +} diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java new file mode 100644 index 0000000..6a98b3f --- /dev/null +++ b/test/ch/epfl/xblast/ListsTest.java @@ -0,0 +1,32 @@ +package ch.epfl.xblast; + +import java.util.List; +import java.util.Arrays; +import static org.junit.Assert.*; +import org.junit.Test; + +/** +* @author Pacien TRAN-GIRARD (261948) +* @author Timothée FLOURE (257420) +*/ +public class ListsTest { + @Test(expected=IllegalArgumentException.class) + public void isEmptyListThrowingException() { + List sampleList = Arrays.asList(); + Lists.mirrored(sampleList); + } + + @Test(expected=IllegalArgumentException.class) + public void isNullListThrowingException() { + List sampleList = Arrays.asList(); + Lists.mirrored(sampleList); + } + + @Test + public void isListMirrored() { + List sampleList = Arrays.asList(1,2,3,4,5); + List expected = Arrays.asList(1,2,3,4,5,4,3,2,1); + + assertEquals(expected, Lists.mirrored(sampleList)); + } +} diff --git a/test/ch/epfl/xblast/namecheck/NameCheck02.java b/test/ch/epfl/xblast/namecheck/NameCheck02.java new file mode 100644 index 0000000..978526e --- /dev/null +++ b/test/ch/epfl/xblast/namecheck/NameCheck02.java @@ -0,0 +1,60 @@ +package ch.epfl.xblast.namecheck; + +import java.util.List; + +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.Lists; +import ch.epfl.xblast.server.Block; +import ch.epfl.xblast.server.Board; +import ch.epfl.xblast.server.Ticks; + +/** + * Classe abstraite utilisant tous les éléments de l'étape 2, pour essayer de + * garantir que ceux-ci ont le bon nom et les bons types. Attention, ceci n'est + * pas un test unitaire, et n'a pas pour but d'être exécuté! + */ + +abstract class NameCheck02 { + void checkTicks() { + int x = Ticks.PLAYER_DYING_TICKS + + Ticks.PLAYER_INVULNERABLE_TICKS + + Ticks.BOMB_FUSE_TICKS + + Ticks.EXPLOSION_TICKS + + Ticks.WALL_CRUMBLING_TICKS + + Ticks.BONUS_DISAPPEARING_TICKS; + System.out.println(x); + } + + void checkBlock() { + Block b = Block.FREE; + b = Block.INDESTRUCTIBLE_WALL; + b = Block.DESTRUCTIBLE_WALL; + b = Block.CRUMBLING_WALL; + boolean d = b.isFree() && b.canHostPlayer() && b.castsShadow(); + System.out.println(b + "/" + d); + } + + void checkLists() { + List l1 = null; + List l2 = null; + List> l3 = null; + List l1m = Lists.mirrored(l1); + List l2m = Lists.mirrored(l2); + List> l3m = Lists.>mirrored(l3); + System.out.println("" + l1m + l2m + l3m); + } + + void checkBoard() { + List> q = null; + Board b = Board.ofQuadrantNWBlocksWalled(q); + b = Board.ofInnerBlocksWalled(q); + b = Board.ofRows(q); + List> cells = null; + b = new Board(cells); + Cell c = null; + Sq bs = b.blocksAt(c); + Block l = b.blockAt(c); + System.out.println(bs + "/" + l); + } +} diff --git a/test/ch/epfl/xblast/server/BlockTest.java b/test/ch/epfl/xblast/server/BlockTest.java new file mode 100644 index 0000000..bbe4376 --- /dev/null +++ b/test/ch/epfl/xblast/server/BlockTest.java @@ -0,0 +1,35 @@ +package ch.epfl.xblast.server; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Timothée FLOURE (257420) + * @author pacien TRAN-GIRARD (261948) + */ +public class BlockTest { + @Test + public void free() { + assertFalse(Block.INDESTRUCTIBLE_WALL.isFree()); + assertFalse(Block.DESTRUCTIBLE_WALL.isFree()); + assertFalse(Block.CRUMBLING_WALL.isFree()); + assertTrue(Block.FREE.isFree()); + } + + @Test + public void canHostPlayer() { + assertFalse(Block.INDESTRUCTIBLE_WALL.canHostPlayer()); + assertFalse(Block.DESTRUCTIBLE_WALL.canHostPlayer()); + assertFalse(Block.CRUMBLING_WALL.canHostPlayer()); + assertTrue(Block.FREE.canHostPlayer()); + } + + @Test + public void castSadhow() { + assertTrue(Block.INDESTRUCTIBLE_WALL.castsShadow()); + assertTrue(Block.DESTRUCTIBLE_WALL.castsShadow()); + assertTrue(Block.CRUMBLING_WALL.castsShadow()); + assertFalse(Block.FREE.castsShadow()); + } +} diff --git a/test/ch/epfl/xblast/server/BoardTest.java b/test/ch/epfl/xblast/server/BoardTest.java new file mode 100644 index 0000000..7868e70 --- /dev/null +++ b/test/ch/epfl/xblast/server/BoardTest.java @@ -0,0 +1,137 @@ +package ch.epfl.xblast.server; + +import java.util.List; +import java.util.ArrayList; +import org.junit.Test; +import ch.epfl.cs108.Sq; + +import static org.junit.Assert.*; + +/** + * @author Timothée FLOURE (257420) + * @author Pacien TRAN-GIRARD (261948) + */ +public class BoardTest { + + @Test(expected=IllegalArgumentException.class) + public void isBoardBuilderEmptyInputThrowingException() { + List> blocks = new ArrayList>(); + new Board(blocks); + + } + + + @Test(expected=IllegalArgumentException.class) + public void isBoardBuilderIllegalInputThrowingException() { + List> blocks = new ArrayList>(); + + for (int i = 0;i<8;i++) { + blocks.add(Sq.constant(Block.FREE)); + } + + new Board(blocks); + + } + + @Test(expected=IllegalArgumentException.class) + public void isOfRowsEmptyInputMatrixThrowingException() { + List> rowsList = new ArrayList>(); + Board.ofRows(rowsList); + } + + @Test(expected=IllegalArgumentException.class) + public void isOfRowsIllegalInputThrowingException() { + List> rowsList = new ArrayList>(); + List sampleRow = new ArrayList(); + + for (int i = 0;i < 8 ;i++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + + assertTrue(Board.ofRows(rowsList) instanceof Board); + } + + @Test + public void buildBoardFromRowsMatrix() { + List> rowsList = new ArrayList>() ; + + for (int i = 0; i < 13; i++) { + List sampleRow = new ArrayList(); + for (int j = 0; j < 15; j++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + } + + assertTrue(Board.ofRows(rowsList) instanceof Board); + } + + @Test(expected=IllegalArgumentException.class) + public void isOfInnerBlocksWalledEmptyInputMatrixThrowingException() { + List> rowsList = new ArrayList>(); + Board.ofInnerBlocksWalled(rowsList); + } + + @Test(expected=IllegalArgumentException.class) + public void isOfInnerBlocksWalledIllegalInputThrowingException() { + List> rowsList = new ArrayList>(); + List sampleRow = new ArrayList(); + + for (int i = 0;i < 8 ;i++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + + assertTrue(Board.ofInnerBlocksWalled(rowsList) instanceof Board); + } + + @Test + public void buildBoardFromInnerBlocks() { + List> rowsList = new ArrayList>() ; + + for (int i = 0; i < 11; i++) { + List sampleRow = new ArrayList(); + for (int j = 0; j < 13; j++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + } + + assertTrue(Board.ofInnerBlocksWalled(rowsList) instanceof Board); + } + + @Test(expected=IllegalArgumentException.class) + public void isBuildWithEmptyQuadrantThrowingException() { + List> rowsList = new ArrayList>(); + Board.ofQuadrantNWBlocksWalled(rowsList); + } + + @Test(expected=IllegalArgumentException.class) + public void isBuildWithIllegalQuadrantThrowingException() { + List> rowsList = new ArrayList>(); + List sampleRow = new ArrayList(); + + for (int i = 0;i < 8 ;i++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + + assertTrue(Board.ofQuadrantNWBlocksWalled(rowsList) instanceof Board); + } + + @Test + public void buildBoardFromNWQuadrant() { + List> rowsList = new ArrayList>() ; + + for (int i = 0; i < 6; i++) { + List sampleRow = new ArrayList(); + for (int j = 0; j < 7; j++) { + sampleRow.add(Block.FREE); + } + rowsList.add(sampleRow); + } + + assertTrue(Board.ofQuadrantNWBlocksWalled(rowsList) instanceof Board); + } +} -- cgit v1.2.3