-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
63 lines (51 loc) · 1.11 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
require_relative 'tile'
require 'yaml'
class Board
def initialize
@grid = Array.new(9) {Array.new(9)}
end
def [](pos)
grid[pos[0]][pos[1]]
end
def []=(pos, mark)
grid[pos[0]][pos[1]] = mark
end
def populate_grid
grid.each_with_index do |row, row_idx|
row.each_with_index do |cell, col_idx|
self[[row_idx, col_idx]] = Tile.new(self, [row_idx, col_idx])
end
end
add_bombs
end
def lost?
grid.flatten.any? {|tile| tile.bombed && tile.revealed }
end
def won?
return false if lost?
grid.flatten.all? { |tile| tile.bombed || tile.revealed }
end
def render
puts "Welcome to Minesweeper. Prepare to do a good job."
puts " #{(0..8).to_a.join(' ')}"
grid.each_with_index do |row, row_idx|
print "#{row_idx} "
row.each do |cell|
print "#{cell.display} "
end
print "\n"
end
end
private
attr_reader :grid
def add_bombs
9.times do
position = grid.sample.sample
until !position.bombed
position = grid.sample.sample
end
position.bombed = true
end
nil
end
end