diff options
author | Adam NAILI | 2018-01-11 20:07:28 +0100 |
---|---|---|
committer | Adam NAILI | 2018-01-11 20:07:28 +0100 |
commit | 1347f30afbed7ee9c149f77534f5e75fbe819b98 (patch) | |
tree | 92d68934e527a2a9b7faa91e048d0ccd0bd65da3 | |
parent | e5f4426230fc485feac96fd5db6151faad24591d (diff) | |
download | wallj-1347f30afbed7ee9c149f77534f5e75fbe819b98.tar.gz |
Implementing BoardConverter and test failed
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/BoardConverter.java | 40 | ||||
-rw-r--r-- | src/test/java/fr/umlv/java/wallj/board/BoardConverterTest.java | 46 |
2 files changed, 84 insertions, 2 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java index 9f5736a..0f8359b 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java | |||
@@ -1,5 +1,43 @@ | |||
1 | package fr.umlv.java.wallj.board; | 1 | package fr.umlv.java.wallj.board; |
2 | 2 | ||
3 | public class BoardConverter { | 3 | import fr.umlv.java.wallj.model.*; |
4 | |||
5 | import java.util.ArrayList; | ||
6 | import java.util.List; | ||
7 | |||
8 | /** | ||
9 | * @author Adam NAILI | ||
10 | */ | ||
11 | public final class BoardConverter { | ||
4 | //TODO | 12 | //TODO |
13 | private BoardConverter() { | ||
14 | } | ||
15 | |||
16 | public static Board worldToBoard(List<Block> blocks) { | ||
17 | int width = blocks.stream().map(Block::getTile).mapToInt(TileVec2::getRow).max().orElse(-1) + 1; | ||
18 | int height = blocks.stream().map(Block::getTile).mapToInt(TileVec2::getCol).max().orElse(-1) + 1; | ||
19 | |||
20 | Board.Builder builder = new Board.Builder(width, height); | ||
21 | for (Block block : blocks) { | ||
22 | builder.setBlockTypeAt(block.getTile(), block.getType()); | ||
23 | } | ||
24 | return builder.build(); | ||
25 | } | ||
26 | |||
27 | public static List<Block> boardToWorld(Board board) { | ||
28 | ArrayList<Block> blocks = new ArrayList<>(); | ||
29 | int nbRow = board.getDim().getRow(); | ||
30 | int nbCol = board.getDim().getCol(); | ||
31 | for (int i = 0; i < nbRow; i++) { | ||
32 | for (int j = 0; i < nbCol; j++) { | ||
33 | Block block; | ||
34 | TileVec2 location = TileVec2.of(i, j); | ||
35 | block = BlockFactory.build(board.getBlockTypeAt(location), location); | ||
36 | if (block != null) { | ||
37 | blocks.add(block); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | return blocks; | ||
42 | } | ||
5 | } | 43 | } |
diff --git a/src/test/java/fr/umlv/java/wallj/board/BoardConverterTest.java b/src/test/java/fr/umlv/java/wallj/board/BoardConverterTest.java index 1ed74d1..9574adf 100644 --- a/src/test/java/fr/umlv/java/wallj/board/BoardConverterTest.java +++ b/src/test/java/fr/umlv/java/wallj/board/BoardConverterTest.java | |||
@@ -1,4 +1,48 @@ | |||
1 | package fr.umlv.java.wallj.board; | 1 | package fr.umlv.java.wallj.board; |
2 | 2 | ||
3 | public class BoardConverterTest { | 3 | import fr.umlv.java.wallj.model.*; |
4 | import org.jbox2d.common.Vec2; | ||
5 | import org.junit.jupiter.api.Assertions; | ||
6 | import org.junit.jupiter.api.Test; | ||
7 | |||
8 | import java.util.ArrayList; | ||
9 | import java.util.LinkedList; | ||
10 | import java.util.List; | ||
11 | |||
12 | /** | ||
13 | * @author Adam NAILI | ||
14 | */ | ||
15 | final class BoardConverterTest { | ||
16 | |||
17 | @Test | ||
18 | void testWorldToBoard(){ | ||
19 | List<Block> blocks = new LinkedList<>(); | ||
20 | Board.Builder builder = new Board.Builder(5,1); | ||
21 | TileVec2 t0 = TileVec2.of(0,0); | ||
22 | TileVec2 t1 = TileVec2.of(1,0); | ||
23 | TileVec2 t2 = TileVec2.of(2,0); | ||
24 | TileVec2 t3 = TileVec2.of(3,0); | ||
25 | TileVec2 t4 = TileVec2.of(4,0); | ||
26 | |||
27 | builder.setBlockTypeAt(t0,BlockType.WALL); | ||
28 | builder.setBlockTypeAt(t1,BlockType.BOMB); | ||
29 | builder.setBlockTypeAt(t2,BlockType.GARBAGE); | ||
30 | builder.setBlockTypeAt(t3,BlockType.ROBOT); | ||
31 | builder.setBlockTypeAt(t4,BlockType.TRASH); | ||
32 | |||
33 | Board certifiedBoard = builder.build(); | ||
34 | |||
35 | blocks.add(BlockFactory.build(BlockType.WALL,t0)); | ||
36 | blocks.add(BlockFactory.build(BlockType.BOMB,t1)); | ||
37 | blocks.add(BlockFactory.build(BlockType.GARBAGE,t2)); | ||
38 | blocks.add(BlockFactory.build(BlockType.ROBOT,t3)); | ||
39 | blocks.add(BlockFactory.build(BlockType.TRASH,t4)); | ||
40 | |||
41 | Board board = BoardConverter.worldToBoard(blocks); | ||
42 | |||
43 | Assertions.assertEquals(certifiedBoard,board); | ||
44 | |||
45 | } | ||
46 | |||
4 | } | 47 | } |
48 | |||