-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
99 lines (73 loc) · 2.29 KB
/
Board.java
File metadata and controls
99 lines (73 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Board {
private final Space[][] spaces;
private static final int[][] NEIGH_LOC = {{1,0},{-1,0},{0,1},{0,-1}};
private final int SIZE;
public Board(int size) {
this.SIZE = size;
spaces = new Space[this.SIZE][this.SIZE];
for (int i = 0; i < this.SIZE; i++) {
for (int j = 0; j < this.SIZE; j++) {
this.spaces[i][j] = new Space(SpaceType.INVALID, i, j);
}
}
}
private boolean isSafe(int val){
return (val >= 0 && val < SIZE);
}
private boolean isSafe(int val1, int val2){
return (isSafe(val1) && isSafe(val2));
}
public void generateBoard(float randomness){
for (int i = 0; i < this.SIZE; i++){
for (int j = 0; j < this.SIZE; j++){
if (Math.random() > randomness){
this.spaces[i][j].setType(SpaceType.EMPTY);
} else {
this.spaces[i][j].setType(SpaceType.BLOCK);
}
}
}
}
public Space getSpace(int x, int y){
return isSafe(x,y) ? this.spaces[x][y] : null;
}
public Space getSpace(Point point){
return getSpace(point.x, point.y);
}
public void swapSpace(int x, int y) {
if (isSafe(x, y)) {
this.getSpace(x, y).swapType();
}
}
public Space[] getNeighbors(Space space){
ArrayList<Space> neighbors = new ArrayList<Space>();
for (int[] i : NEIGH_LOC){
int x = space.getX() + i[0];
int y = space.getY() + i[1];
if (isSafe(x,y)) {
Space s = this.getSpace(x, y);
if (s.getType() == SpaceType.EMPTY)
neighbors.add(s);
}
}
return neighbors.toArray(new Space[neighbors.size()]);
}
public void resetState(){
for (Space spc[] : spaces){
for (Space space: spc){
space.reset();
}
}
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for (Space[] s: this.spaces){
out.append(Arrays.toString(s)).append('\n');
}
return out.toString();
}
}