-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.rb
97 lines (83 loc) · 2.16 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
86
87
88
89
90
91
92
93
94
95
96
97
require 'colorize'
class Tile
attr_accessor :position
attr_reader :board
SURROUNDINGS = [[-1, -1], [-1, 0], [-1, 1], [0, -1],
[0, 1], [1, -1], [1, 0], [1, 1]]
def initialize(position, board, bomb = false)
@position = position
@bomb = bomb
@flagged = false
@revealed = false
@board = board
end
def flagged?
@flagged
end
def bomb?
@bomb
end
def revealed?
@revealed
end
def toggle_flag
@flagged = !@flagged
end
def count_bombs
neighbors.count{ |neighbor| neighbor.bomb? }
end
def neighbors
rows = board.grid[0].count
cols = board.grid.count
neighbor_tiles = []
SURROUNDINGS.each do |neighbor|
new_position = [(neighbor[0] + position[0]), (neighbor[1] + position[1])]
next unless (0...rows).include? new_position[0]
next unless (0...cols).include? new_position[1]
neighbor_tiles << board[new_position]
end
neighbor_tiles
end
def reveal
return true if flagged?
@revealed = true
return false if bomb?
if count_bombs == 0
neighbors.each do |neighbor|
neighbor.reveal unless neighbor.revealed?
end
end
true
end
def display_tile(str, pointer)
background = pointer ? :white : nil
case str
when 'F'
background ||= :blue
' F '.colorize(:color => :red, :background => background)
when '*'
background ||= position.inject(&:+).even? ? :light_black : :black
' '.colorize( :background => background)
when 'B'
background ||= :red
' * '.colorize( :background => background)
when '_'
background ||= :light_white
' '.colorize( :background => background)
else
background ||= :light_white
" #{str} ".colorize( :background => background)
end
end
def display(pointer_pos)
cursor = (pointer_pos == position)
return display_tile('F', cursor) if flagged?
return display_tile('*', cursor) unless revealed?
if bomb?
display_tile('B', cursor)
else
bombs = count_bombs
bombs == 0 ? display_tile('_', cursor) : display_tile(bombs.to_s, cursor)
end
end
end