-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.rb
266 lines (215 loc) · 5.42 KB
/
minesweeper.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require 'set'
require 'debugger'
require 'yaml'
class Minesweeper
attr_accessor :board
def initialize(board)
@board = board
end
def play
until self.board.over?
self.board.draw_board
puts "What do you want to do? Enter 'r' for reveal or 'f' for flag."
puts "Or enter 's' if you want to save the game state."
action = gets.chomp
if action == 's'
puts "Enter in a name for your save file: "
file_name = gets.chomp
file_name.gsub!(" ", "_")
self.save(file_name)
next
end
puts "Enter the row of the tile you want to act on: "
row = gets.chomp.to_i
puts "Enter the column of the tile you want to act on: "
column = gets.chomp.to_i
pos = [row, column]
selected_tile = self.board[pos]
if action != "r" && action != "f"
puts "Invalid action. Please try again"
elsif selected_tile.revealed?
puts "This tile was already revealed or flagged. Please try again."
else
if action == "r"
selected_tile.reveal
elsif action == "f"
selected_tile.place_flag
end
end
end
self.print_result
end
def print_result
if self.board.win?
puts "You win!"
else
puts "You lose!"
end
self.board.draw_board
end
def save(file_name)
content = self.board.to_yaml
File.open("#{file_name}.yml", "w") do |f|
f.puts content
end
end
end
class Board
attr_accessor :grid
def initialize(row_size, column_size, num_bombs)
@grid = self.create_with_random(row_size, column_size, num_bombs)
end
def [](pos)
row, column = pos[0], pos[1]
return self.grid[row][column]
end
#factory method to randomly place bombs on a grid
def create_with_random(row_size, column_size, num_bombs)
overall_board = Array.new(row_size) { Array.new(column_size)}
i = 0
bomb_places = Set.new
while i < num_bombs
random_row = (0...row_size).to_a.sample
random_column = (0...column_size).to_a.sample
unless bomb_places.include?([random_row, random_column])
bomb_places.add( [random_row, random_column] )
i += 1
end
end
(0...row_size).each do |row|
(0...column_size).each do |column|
pos = [row, column]
new_tile = Tile.new(self, row, column, bomb_places.include?(pos))
overall_board[row][column] = new_tile
end
end
overall_board
end
def draw_board
self.grid.each do |row|
row.each do |column|
print "#{column.value} "
end
puts
end
nil
end
def reveal_all
self.grid.each do |row|
row.each do |tile|
unless tile.revealed?
tile.revealed = true
if tile.bombed?
tile.value = "b"
end
end
end
end
end
def over?
self.grid.each do |row|
return true if row.any? {|tile| tile.value == "B" || tile.value == "b"}
return false if row.any? {|tile| !tile.revealed? }
end
end
def win?
if self.over?
self.grid.each do |row|
return false if row.any? {|tile| tile.value == "B" || tile.value == "b"}
end
end
return true
end
end
class Tile
attr_accessor :board, :row, :column, :bombed, :flagged, :revealed, :value
def initialize(my_board, row, column, bombed_status = false)
@board = my_board
@row = row
@column = column
@bombed = bombed_status
@flagged = false
@revealed = false
@value = "*"
end
def bombed?
self.bombed
end
def flagged?
self.flagged
end
def revealed?
self.revealed
end
def reveal
unless self.revealed?
if self.bombed?
self.revealed = true
self.value = "B"
self.board.reveal_all
else
self.revealed = true
surrounding_bombs = self.neighbor_bomb_count
if surrounding_bombs > 0
self.value = surrounding_bombs.to_s
else
self.value = "_"
self.neighbors.each do |tile|
tile.reveal
end
end
end
end
end
def place_flag
unless self.revealed?
self.revealed = true
self.value = "F"
end
end
def neighbors
neighbor_array = []
deltas = [
[1,1], [-1, 1], [1, -1], [-1, -1], [1,0], [-1,0], [0, -1], [0,1]
]
cur_row = self.row
cur_column = self.column
deltas.each do |x|
new_row = cur_row + x[0]
new_column = cur_column + x[1]
if (0...self.board.grid.length).to_a.include?(new_row)
if (0...self.board.grid[0].length).to_a.include?(new_column)
neighbor_array << self.board.grid[cur_row + x[0]][cur_column + x[1]]
end
end
end
neighbor_array
end
def neighbor_bomb_count
bomb_count = 0
self.neighbors.each do |x|
if x.bombed?
bomb_count += 1
end
end
bomb_count
end
end
if __FILE__ == $PROGRAM_NAME
if ARGV.empty?
puts "How many rows do you want your game to have?"
game_rows = gets.chomp.to_i
puts "How many columns do you want your game to have?"
game_columns = gets.chomp.to_i
puts "How many bombs do you want your game to have?"
game_bombs = gets.chomp.to_i
game_board = Board.new(game_rows, game_columns, game_bombs)
game = Minesweeper.new(game_board)
game.play
else
content = File.read(ARGV.shift)
game_board = YAML.load(content)
game = Minesweeper.new(game_board)
game.play
end
end