-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard.rb
More file actions
40 lines (32 loc) · 688 Bytes
/
board.rb
File metadata and controls
40 lines (32 loc) · 688 Bytes
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
class Board
def initialize(state)
@xmax = state.size
@ymax = state[0].size
state.each do |row|
raise "Board rows are of varying lengths" if row.size != @ymax
end
@state = state
end
def [](a, b)
@state[a][b]
end
def each()
for x in 0...@xmax
for y in 0...@ymax
yield x, y
end
end
end
def neighbor_iter(x, y)
temp = @state[x][y]
@state[x][y] = nil
xrange = ([x-1, 0].max)..([x+1, @xmax-1].min)
yrange = ([y-1, 0].max)..([y+1, @ymax-1].min)
for i in xrange
for j in yrange
yield i, j if @state[i][j]
end
end
@state[x][y] = temp
end
end