hanze/game-client

src/main/java/nl/isygameclient/models/board/Board.java in modern-design
Repositories | Summary | Log | Files

Board.java (1420B) download


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