-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.groovy
executable file
·48 lines (39 loc) · 1.08 KB
/
Grid.groovy
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
@groovy.lang.Grab(group = 'com.google.guava', module = 'guava', version = '10.0.1')
import com.google.common.collect.Table
import com.google.common.collect.ArrayTable
class Grid {
private Table table
static Grid create(Integer maxX, Integer maxY, Boolean defaultValue) {
IntRange rows = 1..maxX
IntRange columns = 1..maxY
Table table = ArrayTable.create(rows, columns)
rows.each {x ->
columns.each {y ->
table.put(x, y, defaultValue)
}
}
return new Grid(table)
}
private Grid(Table t) {
table = t
}
Integer getColumnCount() {
table.columnKeyList().size()
}
Integer getRowCount() {
table.rowKeyList().size()
}
Object each(Closure c) {
table.columnKeyList().each {y ->
table.rowKeyList().each {x ->
c(x, y, table.get(x, y))
}
}
}
Boolean put(x, y, v) {
table.put(x, y, v)
}
Boolean get(x, y) {
table.get(x, y)
}
}