aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv')
-rw-r--r--src/main/java/fr/umlv/java/wallj/board/Board.java54
-rw-r--r--src/main/java/fr/umlv/java/wallj/utils/Matrix.java28
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 @@
1package fr.umlv.java.wallj.board; 1package fr.umlv.java.wallj.board;
2 2
3import fr.umlv.java.wallj.model.BlockType; 3import fr.umlv.java.wallj.model.BlockType;
4import fr.umlv.java.wallj.utils.Matrix;
4 5
5/** 6/**
6 * A mutable BlockType matrix. 7 * An immutable BlockType matrix.
7 */ 8 */
8public final class Board { 9public 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 @@
1package fr.umlv.java.wallj.utils;
2
3/**
4 * Utility functions for two dimension arrays.
5 */
6public 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}