-
Notifications
You must be signed in to change notification settings - Fork 0
/
mineboard.rb
111 lines (93 loc) · 2.2 KB
/
mineboard.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'io/console'
class Mineboard
attr_accessor :grid, :num_bombs, :pointer_pos
def initialize(rows = 9, cols = 9, num_bombs = 9)
@grid = Array.new(rows) { Array.new(cols) }
@num_bombs = num_bombs
@total_tiles = rows * cols
@pointer_pos = [0, 0]
generate_tiles
end
def [](pos)
grid[pos[0]][pos[1]]
end
def generate_tiles
bomb_pos = generate_bomb_pos
@grid.each_with_index do |row, idx_row|
row.count.times do |idx_col|
bomb = bomb_pos.include?([idx_row, idx_col]) ? true : false
tile = Tile.new([idx_row, idx_col], self, bomb)
@grid[idx_row][idx_col] = tile
end
end
end
def revealed_count
@grid.flatten.count &:revealed?
end
def flag_count
@grid.flatten.count &:flagged?
end
def generate_bomb_pos
bomb_pos = []
until bomb_pos.count == num_bombs
random_pos = [rand(9), rand(9)]
bomb_pos << random_pos unless bomb_pos.include?(random_pos)
end
bomb_pos
end
def reveal(position)
self[position].reveal
end
def flag(position)
self[position].toggle_flag
end
def reveal_all
grid.count.times do |row|
grid[0].count.times do |col|
position = [row, col]
self[position].reveal
end
end
end
def flag_message
puts "You have flagged #{flag_count} tiles."
puts "You have #{num_bombs - flag_count} tiles left to flag."
end
def move_cursor
input = STDIN.getch
case input
when "a"
@pointer_pos[1] -= 1
when "d"
@pointer_pos[1] += 1
when "s"
@pointer_pos[0] += 1
when "w"
@pointer_pos[0] -= 1
when "f"
flag(pointer_pos)
when "r"
reveal(pointer_pos)
when "e"
exit
end
end
def display
system("clear")
puts " #{(0..8).to_a.join(" ")}"
@grid.each_with_index do |row, index|
row_array = ["#{index} "]
row.each do |tile|
row_array << tile.display(@pointer_pos)
end
puts row_array.join('')
end
end
def won?
revealed_count == (@total_tiles - num_bombs) &&
num_bombs == flag_count
end
def lost?
@grid.flatten.any? { |tile| tile.revealed? && tile.bomb? }
end
end