-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.rb
73 lines (59 loc) · 1.14 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
class Tile
MOVES = [
[ 1, 1],
[-1,-1],
[ 1,-1],
[-1, 1],
[ 0, 1],
[ 0,-1],
[ 1, 0],
[-1, 0]
]
attr_accessor :bombed, :flagged, :revealed, :bombed
attr_reader :board
def initialize(board)
@revealed = false
@bombed = false
@flagged = false
@board = board
end
def to_s
if self.flagged?
"F"
elsif !self.revealed?
"*"
elsif self.bombed?
"O"
elsif self.neighbor_bomb_count != 0
"#{neighbor_bomb_count}"
else
"_"
end
end
def revealed?
self.revealed
end
def flagged?
self.flagged
end
def bombed?
self.bombed
end
def set_bomb
self.bombed = true
end
def neighbors
new_coord = []
pos = self.board.find_position(self)
MOVES.each do |diff|
new_coord << [pos[0] + diff[0], pos[1] + diff[1] ]
end
new_coord.select! {|x| x[0].between?(0, self.board.grid.length - 1) && x[1].between?(0, self.board.grid.length - 1) }
neighbors = []
new_coord.each { |pos| neighbors << self.board[pos] }
neighbors
end
def neighbor_bomb_count
neighbors.count{|x| x.bombed == true}
end
end