-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.rb
50 lines (37 loc) · 917 Bytes
/
board.rb
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
class Board
BOARD_SIZE = 9
NUM_BOMBS = 10
attr_reader :grid
def initialize
@grid = Array.new(BOARD_SIZE) { Array.new(BOARD_SIZE) {Tile.new(self)} }
until tiles.count { |x| x.bombed? } == NUM_BOMBS
x,y = rand(self.grid.length), rand(self.grid.length)
pos = [x,y]
self[pos].set_bomb
end
end
def [](pos)
row, col = pos[0], pos[1]
self.grid[row][col]
end
def find_position(tile)
x,y = nil,nil
self.grid.each {|row| x= self.grid.index(row) if row.include?(tile)}
y = self.grid[x].index(tile)
[x,y]
end
def tiles
self.grid.flatten
end
# def []=(pos, value)
# row, col = pos[0], pos[1]
# self.grid[row][col] = value
# end
def render
system("clear")
puts " #{(0...self.grid.length).to_a.join(" ")}"
self.grid.each_with_index do |row, i|
p "#{i} #{row.each(&:inspect).join(" ")}"
end
end
end