Board.java (1186B) download
1package nl.isygameclient.models.board;
2
3import java.util.Arrays;
4import nl.isygameclient.util.Vector2D;
5
6public class Board<T> {
7 protected final int width;
8 protected final int height;
9
10 protected final T[][] field;
11
12 @SuppressWarnings("unchecked")
13 public Board(int width, int height) {
14 this.width = width;
15 this.height = height;
16 this.field = (T[][]) new Object[height][width];
17 }
18
19 public void set(T value, Vector2D<Integer, Integer> pos) {
20 field[pos.getY()][pos.getX()] = value;
21 }
22
23 public T get(Vector2D<Integer, Integer> pos) {
24 return field[pos.getY()][pos.getX()];
25 }
26
27 public int size() {
28 return width * height;
29 }
30
31 public void clear() {
32 for (int y = 0; y < height; y++)
33 for (int x = 0; x < width; x++)
34 field[y][x] = null;
35 }
36
37 public int getWidth() {
38 return width;
39 }
40
41 public int getHeight() {
42 return height;
43 }
44
45 public T[][] getField() {
46 return field;
47 }
48
49 @Override
50 public String toString() {
51 StringBuilder builder = new StringBuilder();
52 for (T[] row : field) {
53 builder.append(Arrays.toString(row))
54 .append(", \n");
55 }
56
57 return "Board{"
58 +
59 "width=" + width +
60 ", height=" + height +
61 ", field=\n" + builder +
62 '}';
63 }
64}