diff options
author | pacien | 2018-01-08 17:49:05 +0100 |
---|---|---|
committer | pacien | 2018-01-08 17:49:05 +0100 |
commit | 7733b7dffe5a70c578713c34bdbabafee060d80a (patch) | |
tree | e6649fa8e73ee39f78b149740c510060accf571b | |
parent | f9d35e2c5e4aa1aef14ae49a0fe1a5117ab67115 (diff) | |
download | wallj-7733b7dffe5a70c578713c34bdbabafee060d80a.tar.gz |
Implement board parser and its tests
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/Board.java | 4 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/BoardParser.java | 59 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/utils/Matrix.java | 32 | ||||
-rw-r--r-- | src/test/java/fr/umlv/java/wallj/board/BoardParserTest.java | 41 | ||||
-rw-r--r-- | src/test/resources/maps/nonRectangular.txt | 3 | ||||
-rw-r--r-- | src/test/resources/maps/smallValid.txt | 3 |
6 files changed, 134 insertions, 8 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/Board.java b/src/main/java/fr/umlv/java/wallj/board/Board.java index dfcec88..fbe6ad9 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java | |||
@@ -44,8 +44,8 @@ public final class Board { | |||
44 | 44 | ||
45 | private Board(BlockType[][] map) { | 45 | private Board(BlockType[][] map) { |
46 | int w = Matrix.getWidth(map), h = Matrix.getHeight(map); | 46 | int w = Matrix.getWidth(map), h = Matrix.getHeight(map); |
47 | this.map = new BlockType[w][h]; | 47 | this.map = new BlockType[h][w]; |
48 | for (int row = 0; row < w; ++row) System.arraycopy(map[row], 0, this.map[row], 0, h); | 48 | for (int row = 0; row < h; ++row) System.arraycopy(map[row], 0, this.map[row], 0, w); |
49 | } | 49 | } |
50 | 50 | ||
51 | /** | 51 | /** |
diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardParser.java b/src/main/java/fr/umlv/java/wallj/board/BoardParser.java index 77d836f..b7db602 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardParser.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardParser.java | |||
@@ -1,6 +1,59 @@ | |||
1 | package fr.umlv.java.wallj.board; | 1 | package fr.umlv.java.wallj.board; |
2 | 2 | ||
3 | public class BoardParser { | 3 | import fr.umlv.java.wallj.model.BlockType; |
4 | //TODO | 4 | import fr.umlv.java.wallj.utils.Matrix; |
5 | } | 5 | |
6 | import java.io.IOException; | ||
7 | import java.nio.file.Files; | ||
8 | import java.nio.file.Path; | ||
9 | import java.util.List; | ||
10 | import java.util.ListIterator; | ||
11 | import java.util.stream.Collectors; | ||
12 | |||
13 | public final class BoardParser { | ||
14 | |||
15 | private static Board buildBoard(List<List<BlockType>> map) { | ||
16 | if (!Matrix.isShapeValid(map)) throw new IllegalArgumentException("Board must be rectangular."); | ||
17 | |||
18 | Board.Builder b = new Board.Builder(Matrix.getWidth(map), Matrix.getHeight(map)); | ||
19 | for (ListIterator<List<BlockType>> line = map.listIterator(); line.hasNext(); ) | ||
20 | for (ListIterator<BlockType> block = line.next().listIterator(); block.hasNext(); ) | ||
21 | b.setBlockTypeAt(TileVec2.of(line.previousIndex(), block.nextIndex()), block.next()); | ||
22 | |||
23 | return b.build(); | ||
24 | } | ||
6 | 25 | ||
26 | private static BlockType parseChar(int c) { | ||
27 | switch (c) { | ||
28 | case ' ': | ||
29 | return BlockType.FREE; | ||
30 | case 'W': | ||
31 | return BlockType.wALL; | ||
32 | case 'T': | ||
33 | return BlockType.TRASH; | ||
34 | case 'G': | ||
35 | return BlockType.GARBAGE; | ||
36 | default: | ||
37 | return null; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | private static List<BlockType> parseLine(String line) { | ||
42 | return line.chars() | ||
43 | .mapToObj(BoardParser::parseChar) | ||
44 | .collect(Collectors.toList()); | ||
45 | } | ||
46 | |||
47 | public static Board parse(Path filePath) throws IOException { | ||
48 | return buildBoard(Files.lines(filePath) | ||
49 | .filter(s -> !s.isEmpty()) | ||
50 | .map(String::trim) | ||
51 | .map(BoardParser::parseLine) | ||
52 | .collect(Collectors.toList())); | ||
53 | } | ||
54 | |||
55 | private BoardParser() { | ||
56 | // static class | ||
57 | } | ||
58 | |||
59 | } | ||
diff --git a/src/main/java/fr/umlv/java/wallj/utils/Matrix.java b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java index e23cb2a..e3702bf 100644 --- a/src/main/java/fr/umlv/java/wallj/utils/Matrix.java +++ b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java | |||
@@ -1,12 +1,14 @@ | |||
1 | package fr.umlv.java.wallj.utils; | 1 | package fr.umlv.java.wallj.utils; |
2 | 2 | ||
3 | import java.util.List; | ||
4 | |||
3 | /** | 5 | /** |
4 | * Utility functions for two dimension arrays. | 6 | * Utility functions for two dimension arrays and lists. |
5 | */ | 7 | */ |
6 | public final class Matrix { | 8 | public final class Matrix { |
7 | 9 | ||
8 | /** | 10 | /** |
9 | * @param m the matrix | 11 | * @param m the matrix (2D array) |
10 | * @return the width of the matrix (0 if null) | 12 | * @return the width of the matrix (0 if null) |
11 | */ | 13 | */ |
12 | public static int getWidth(Object[][] m) { | 14 | public static int getWidth(Object[][] m) { |
@@ -14,13 +16,37 @@ public final class Matrix { | |||
14 | } | 16 | } |
15 | 17 | ||
16 | /** | 18 | /** |
17 | * @param m the matrix | 19 | * @param m the matrix (2D array) |
18 | * @return the height of the matrix (0 if null) | 20 | * @return the height of the matrix (0 if null) |
19 | */ | 21 | */ |
20 | public static int getHeight(Object[][] m) { | 22 | public static int getHeight(Object[][] m) { |
21 | return m != null ? m.length : 0; | 23 | return m != null ? m.length : 0; |
22 | } | 24 | } |
23 | 25 | ||
26 | /** | ||
27 | * @param m the matrix (2D List) | ||
28 | * @return the width of the matrix (0 if null) | ||
29 | */ | ||
30 | public static int getWidth(List<? extends List<?>> m) { | ||
31 | return m != null && m.size() > 0 ? m.get(0).size() : 0; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * @param m the matrix (2D List) | ||
36 | * @return the height of the matrix (0 if null) | ||
37 | */ | ||
38 | public static int getHeight(List<? extends List<?>> m) { | ||
39 | return m != null ? m.size() : 0; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * @param l the 2D list to check | ||
44 | * @return T(l is a valid matrix, i.e. all sub - arrays are of the same size and l is not null) | ||
45 | */ | ||
46 | public static boolean isShapeValid(List<? extends List<?>> l) { | ||
47 | return l != null && l.stream().mapToInt(List::size).allMatch(s -> s == l.get(0).size()); | ||
48 | } | ||
49 | |||
24 | private Matrix() { | 50 | private Matrix() { |
25 | // static class | 51 | // static class |
26 | } | 52 | } |
diff --git a/src/test/java/fr/umlv/java/wallj/board/BoardParserTest.java b/src/test/java/fr/umlv/java/wallj/board/BoardParserTest.java new file mode 100644 index 0000000..fdeebbe --- /dev/null +++ b/src/test/java/fr/umlv/java/wallj/board/BoardParserTest.java | |||
@@ -0,0 +1,41 @@ | |||
1 | package fr.umlv.java.wallj.board; | ||
2 | |||
3 | import fr.umlv.java.wallj.model.BlockType; | ||
4 | import org.junit.jupiter.api.Assertions; | ||
5 | import org.junit.jupiter.api.Test; | ||
6 | |||
7 | import java.io.IOException; | ||
8 | import java.net.URISyntaxException; | ||
9 | import java.nio.file.Path; | ||
10 | import java.nio.file.Paths; | ||
11 | |||
12 | final class BoardParserTest { | ||
13 | |||
14 | private Path getResourcePath(String str) throws URISyntaxException { | ||
15 | return Paths.get(getClass().getResource(str).toURI()); | ||
16 | } | ||
17 | |||
18 | @Test | ||
19 | void testFailParseBadShape() { | ||
20 | Assertions.assertThrows(IllegalArgumentException.class, () -> { | ||
21 | BoardParser.parse(getResourcePath("/maps/nonRectangular.txt")); | ||
22 | }); | ||
23 | } | ||
24 | |||
25 | @Test | ||
26 | void testParse() throws IOException, URISyntaxException { | ||
27 | BlockType W = BlockType.wALL, F = BlockType.FREE, G = BlockType.GARBAGE, T = BlockType.TRASH; | ||
28 | BlockType[][] blocks = new BlockType[][]{ | ||
29 | {W, W, W, W, W, W}, | ||
30 | {W, F, G, F, T, W}, | ||
31 | {W, W, W, W, W, W}}; | ||
32 | |||
33 | Board board = BoardParser.parse(getResourcePath("/maps/smallValid.txt")); | ||
34 | |||
35 | Assertions.assertEquals(board.getDim(), TileVec2.of(blocks.length, blocks[0].length)); | ||
36 | for (int row = 0; row < blocks.length; ++row) | ||
37 | for (int col = 0; col < blocks[0].length; ++col) | ||
38 | Assertions.assertEquals(board.getBlockTypeAt(TileVec2.of(row, col)), blocks[row][col]); | ||
39 | } | ||
40 | |||
41 | } | ||
diff --git a/src/test/resources/maps/nonRectangular.txt b/src/test/resources/maps/nonRectangular.txt new file mode 100644 index 0000000..8d834db --- /dev/null +++ b/src/test/resources/maps/nonRectangular.txt | |||
@@ -0,0 +1,3 @@ | |||
1 | WWWWWWWWWW | ||
2 | W W | ||
3 | WWWWWWWWWW | ||
diff --git a/src/test/resources/maps/smallValid.txt b/src/test/resources/maps/smallValid.txt new file mode 100644 index 0000000..fa3a3a9 --- /dev/null +++ b/src/test/resources/maps/smallValid.txt | |||
@@ -0,0 +1,3 @@ | |||
1 | WWWWWW | ||
2 | W G TW | ||
3 | WWWWWW \ No newline at end of file | ||