diff options
author | pacien | 2018-01-08 14:11:11 +0100 |
---|---|---|
committer | pacien | 2018-01-08 14:11:11 +0100 |
commit | 6781889388164d34749fb6eb05d3b4a5ed74aa1d (patch) | |
tree | 4c2dfc749ead9464a0ae360942c21ae93a5aa3e5 /src/main/java | |
parent | 882dad3f0d8a8d16cede88cbe5be570ed19b5394 (diff) | |
download | wallj-6781889388164d34749fb6eb05d3b4a5ed74aa1d.tar.gz |
Make Board immutable
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/Board.java | 54 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/utils/Matrix.java | 28 |
2 files changed, 67 insertions, 15 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 ebe1a7e..dfcec88 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java | |||
@@ -1,16 +1,51 @@ | |||
1 | package fr.umlv.java.wallj.board; | 1 | package fr.umlv.java.wallj.board; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.model.BlockType; | 3 | import fr.umlv.java.wallj.model.BlockType; |
4 | import fr.umlv.java.wallj.utils.Matrix; | ||
4 | 5 | ||
5 | /** | 6 | /** |
6 | * A mutable BlockType matrix. | 7 | * An immutable BlockType matrix. |
7 | */ | 8 | */ |
8 | public final class Board { | 9 | public final class Board { |
9 | 10 | ||
11 | /** | ||
12 | * Board Builder | ||
13 | */ | ||
14 | public static final class Builder { | ||
15 | private final BlockType[][] map; | ||
16 | |||
17 | /** | ||
18 | * @param width width in tiles | ||
19 | * @param height height in tiles | ||
20 | */ | ||
21 | public Builder(int width, int height) { | ||
22 | this.map = new BlockType[width][height]; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * @param pos the tile position vector | ||
27 | * @param type the BlockType to set | ||
28 | * @return the Builder | ||
29 | */ | ||
30 | public Builder setBlockTypeAt(TileVec2 pos, BlockType type) { | ||
31 | map[pos.getCol()][pos.getRow()] = type; | ||
32 | return this; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * @return the immutable Board | ||
37 | */ | ||
38 | public Board build() { | ||
39 | return new Board(map); | ||
40 | } | ||
41 | } | ||
42 | |||
10 | private final BlockType[][] map; | 43 | private final BlockType[][] map; |
11 | 44 | ||
12 | public Board(int width, int height) { | 45 | private Board(BlockType[][] map) { |
13 | map = new BlockType[width][height]; | 46 | int w = Matrix.getWidth(map), h = Matrix.getHeight(map); |
47 | this.map = new BlockType[w][h]; | ||
48 | for (int row = 0; row < w; ++row) System.arraycopy(map[row], 0, this.map[row], 0, h); | ||
14 | } | 49 | } |
15 | 50 | ||
16 | /** | 51 | /** |
@@ -22,21 +57,10 @@ public final class Board { | |||
22 | } | 57 | } |
23 | 58 | ||
24 | /** | 59 | /** |
25 | * @param pos the tile position vector | ||
26 | * @param type the BlockType to set | ||
27 | * @return the overwritten element | ||
28 | */ | ||
29 | public BlockType setBlockTypeAt(TileVec2 pos, BlockType type) { | ||
30 | BlockType old = map[pos.getCol()][pos.getRow()]; | ||
31 | map[pos.getCol()][pos.getRow()] = type; | ||
32 | return old; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * @return the dimension of the Board | 60 | * @return the dimension of the Board |
37 | */ | 61 | */ |
38 | public TileVec2 getDim() { | 62 | public TileVec2 getDim() { |
39 | return TileVec2.of(map.length, map.length > 0 ? map[0].length : 0); | 63 | return TileVec2.of(Matrix.getWidth(map), Matrix.getHeight(map)); |
40 | } | 64 | } |
41 | 65 | ||
42 | } | 66 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/utils/Matrix.java b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java new file mode 100644 index 0000000..e23cb2a --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java | |||
@@ -0,0 +1,28 @@ | |||
1 | package fr.umlv.java.wallj.utils; | ||
2 | |||
3 | /** | ||
4 | * Utility functions for two dimension arrays. | ||
5 | */ | ||
6 | public final class Matrix { | ||
7 | |||
8 | /** | ||
9 | * @param m the matrix | ||
10 | * @return the width of the matrix (0 if null) | ||
11 | */ | ||
12 | public static int getWidth(Object[][] m) { | ||
13 | return m != null && m.length > 0 ? m[0].length : 0; | ||
14 | } | ||
15 | |||
16 | /** | ||
17 | * @param m the matrix | ||
18 | * @return the height of the matrix (0 if null) | ||
19 | */ | ||
20 | public static int getHeight(Object[][] m) { | ||
21 | return m != null ? m.length : 0; | ||
22 | } | ||
23 | |||
24 | private Matrix() { | ||
25 | // static class | ||
26 | } | ||
27 | |||
28 | } | ||