-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grid.pde
90 lines (76 loc) · 1.9 KB
/
Grid.pde
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
class Grid implements Iterable<Tile>{
Tile[][] grid;
int size;
Grid(int size) {
this.size = size;
grid = new Tile[size][size];
}
Tile get(int i, int j) {
return grid[i][j];
}
void set(int i, int j, Tile tile) {
grid[i][j] = tile;
if (tile!=null)
tile.setNewPosition(120*(i+1),120*(j+1));
}
public boolean containsTile(Tile t) {
for(Tile[] tiles: grid) {
for(Tile tile: tiles) {
if(tile==t) return true;
}
}
return false;
}
public List<Tile> getAtPosition(int i, int j) {
List<Tile> ts = new ArrayList<Tile>();
for(Tile[] tiles: grid) {
for(Tile tile: tiles) {
if(tile!=null && tile.getNewPositionX()==120*(i+1) && tile.getNewPositionY()==120*(j+1))
ts.add(tile);
}
}
return ts;
}
public int hashCode() {
int i = 0;
int hashCode = 0;
for(Tile[] tiles: grid) {
for(Tile tile: tiles) {
hashCode += ++i*tile.hashCode();
}
}
return hashCode;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Grid other = (Grid) obj;
for(int i=0; i<size; i++) {
for(int j=0; j<size; j++) {
if(get(i,j)==null && other.get(i,j)!=null) return false;
else if(get(i,j)==null && other.get(i,j)==null) continue;
else if(!get(i,j).equals(other.get(i,j))) return false;
}
}
return true;
}
public Iterator<Tile> iterator() {
return new TileIterator();
}
class TileIterator implements Iterator<Tile> {
int x=0;
boolean hasNext() {
return x<size*size;
}
Tile next() {
int i = x%size;
int j = x/size;
x++;
return grid[i][j];
}
}
}