-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
83 lines (69 loc) · 1.63 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require '/Users/appacademy/Desktop/w2d1/minesweeper/tile.rb'
class Board
attr_accessor :board, :size, :bombs
def initialize(size, bombs)
@board = Array.new(size) { Array.new(size) { Tile.new } }
@size = size
possible_pos = []
size.times do |i|
size.times do |j|
possible_pos << [i, j]
end
end
possible_pos.shuffle!
bombs.times do
loc = possible_pos.pop
@board[loc[0]][loc[1]].bomb = true
end
neighboring_bomb_count(@board)
end
NEIGHBORS = [
[-1, 1],
[-1, 0],
[-1, -1],
[0, 1],
[0, -1],
[1, 1],
[1, 0],
[1, -1]
]
def valid_pos?(pos)
pos >= 0 && pos <= (@size - 1)
end
def neighboring_bomb_count(board)
board.each_with_index do |row, i|
row.each_with_index do |tile, j|
NEIGHBORS.each do |neighbor|
x, y = (i + neighbor[0]), (j + neighbor[1])
if valid_pos?(x) && valid_pos?(y)
tile.num_adj_bombs += 1 if @board[x][y].bomb
end
end
end
end
end
def reveal(coord)
@board[coord[0]][coord[1]].reveal = true
sorted = false
until sorted
sorted = true
NEIGHBORS.each do |neighbor|
x, y = (coord[0] + neighbor[0]), (coord[1] + neighbor[1])
if valid_pos?(x) && valid_pos?(y)
@board[x][y].reveal = true
sorted = false
end
# reveal([x, y]) if @board[x][y].num_adj_bombs == 0
# return
end
end
end
def print_board
@board.each do |row|
p row.map {|x| [x.num_adj_bombs, "bomb #{x.bomb}", x.reveal] }
end
end
end
board = Board.new(5, 3)
board.reveal([0,0])
board.print_board