-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.rb
85 lines (71 loc) · 1.35 KB
/
tile.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
84
85
require 'byebug'
require_relative 'board.rb'
class Tile
attr_accessor :is_bomb, :status, :neighbor_bomb_count
DIRECTIONS = [
[-1, -1],
[-1, 0],
[+1, -1],
[0, -1],
[0, +1],
[-1, +1],
[+1, 0],
[+1, +1]]
def self.neighbors_coords(pos, board)
result = []
row, col = pos
DIRECTIONS.each do |coord|
shift_row, shift_col = coord
neighbor_row, neighbor_col = row + shift_row, col + shift_col
if board.in_bounds?([neighbor_row, neighbor_col])
result << [neighbor_row, neighbor_col]
end
end
result
end
def self.neighbors(pos, board)
Tile.neighbors_coords(pos, board).map { |coord| board[*coord] }
end
def initialize(is_bomb)
@is_bomb = is_bomb
@status = :hidden
end
def reveal
self.status = :revealed
end
def flag
self.status = :flagged
end
def unflag
self.status = :hidden
end
def flagged?
status == :flagged
end
def toggle_flag
flagged? ? unflag : flag
end
def revealed?
status == :revealed
end
def neighbor_bomb_count
@neighbor_bomb_count
end
def is_bomb?
is_bomb
end
def to_s
case status
when :hidden
"*"
when :revealed
if is_bomb?
"B"
else
neighbor_bomb_count == 0 ? "_" : neighbor_bomb_count.to_s
end
when :flagged
"F"
end
end
end