diff options
Diffstat (limited to 'src/main/java/fr/umlv')
-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 |
3 files changed, 87 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 | } |