-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.rb
352 lines (306 loc) · 7.63 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require 'time'
require 'yaml'
class Minesweeper
def initialize
@solution_board = []
@player_board = []
@mine_coordinates = []
@neighbors_key = [
[-1,-1],
[-1,0],
[-1,1],
[0,-1],
[0,1],
[1,-1],
[1,0],
[1,1]
]
end
def start_game
puts "Welcome to Minesweeper!"
puts "new game, load game, or show scoreboard? (n: new, l: load, s: scoreboard)"
input = gets.chomp
case input
when "n"
print "Pick board size (1: 9x9 or 2: 16x16): "
board_size = gets.chomp
set_boards(board_size)
game
when "l"
load
game
when"s"
puts "9: 9x9 or 16: 16x16"
number = gets.chomp.to_i
display_scoreboard(number)
start_game
when "reset"
print "Password: "
password = gets.chomp
if password == "andyisfreakingawesome"
reset_scoreboard
else
puts "invalid password!"
start_game
end
else
puts "invalid input!"
puts
start_game
end
end
def game
@start_time = Time.now
until game_over?
display_board
player_turn
end
display_scoreboard
end
def player_turn
puts "choose an action (s: save game, f: flag or r: reveal):"
action = gets.chomp
case action
when "f"
x,y = get_user_xy
flag(x,y)
when "r"
x,y = get_user_xy
reveal(x,y)
if @solution_board[x][y] == "_"
check_neighbors(x,y)
end
when "s"
save
when "cheat"
print "Password: "
password = gets.chomp
answer if password == "andyisfreakingawesome"
else
puts "invalid input!"
player_turn
end
end
def game_over?
unexplored_count = 0
@player_board.each_index do |row|
@player_board.each_index do |col|
if @player_board[row][col] == "B"
@solution_board[row][col] = "X"
display_board(true)
puts "You lose! GAME OVER!"
return true
elsif @player_board[row][col] == "*"
unexplored_count += 1
end
end
end
if flagged_mines? || unexplored_count == 0
puts "You Win!"
@time_taken = Time.now - @start_time
save_score
return true
end
false
end
def get_user_xy
puts "please enter the coordinates (e.g.: 2,3)"
input = gets.chomp.split(",").map(&:to_i)
end
def display_scoreboard(number = @player_board.length)
puts "High Scores for #{number}x#{number}!"
load_scores(number)[0..9].each_with_index do |score,index|
name, time = score
time = time.to_s
puts "#{index + 1}. #{name} #{time.rjust(20)} seconds"
end
end
def reset_scoreboard
File.open("high_scores_9", 'w') do |file|
file.puts [].to_yaml
end
File.open("high_scores_16", 'w') do |file|
file.puts [].to_yaml
end
end
def load_scores(number = @player_board.length)
scoreboard = YAML::load(File.read("high_scores_#{number}"))
end
def save_score
print "Enter your name: "
name = gets.chomp
entry = [name, @time_taken]
number = @player_board.length
old_scoreboard = load_scores(number)
new_scoreboard = old_scoreboard << entry
new_scoreboard = new_scoreboard.sort_by {|name,time| time}
File.open("high_scores_#{number}", "w") do |file|
file.puts new_scoreboard.to_yaml
end
puts "Scoreboard Updated!"
end
def save
print "file name: "
file_name = gets.chomp
temp = [@player_board, @solution_board]
File.open(file_name, 'w') do |file|
file.puts temp.to_yaml
end
puts "game saved!"
start_game
end
def load
print "enter file name or quit to go back to menu: "
file_name = gets.chomp
if File.exists?(file_name)
load_file = YAML::load(File.read(file_name))
elsif file_name == "quit"
start_game
else
puts "invalid file name!"
load
end
@player_board, @solution_board = load_file
end
def reveal(x,y)
@player_board[x][y] = @solution_board[x][y]
end
def flag(x,y)
@player_board[x][y] = 'F'
end
def check_neighbors(x1,y1)
adjacent_squares = []
@neighbors_key.each do |diff|
x, y = x1 + diff[0], y1 + diff[1]
location = [x,y]
if x.between?(0,8) && y.between?(0,8) && !@mine_coordinates.include?([x,y])
adjacent_squares << location
end
end
adjacent_squares.each do |coord|
next if @player_board[coord[0]][coord[1]] != "*"
next if @solution_board[coord[0]][coord[1]] == "B"
if @solution_board[coord[0]][coord[1]] == "_"
reveal(coord[0],coord[1])
check_neighbors(coord[0],coord[1])
else
reveal(coord[0],coord[1])
end
end
end
def flagged_mines?
count = 0
@player_board.each_index do |row|
@player_board.each_index do |col|
if @player_board[row][col] == "F" && @solution_board[row][col] = "B"
count += 1
end
end
end
if count == 10 && @player_board.length == 9
true
elsif count == 40
true
else
false
end
end
def display_board(solution_board = false)
print " "
@solution_board.each_index {|index| print "#{index}".center(5)}
puts
if solution_board == true
@solution_board.each_with_index do |line, i|
print "#{i}".center(3)
print line
puts
end
end
@player_board.each_with_index do |line, i|
print "#{i}".center(3)
print line
puts
end
end
def set_boards(board_size)
if board_size == "1"
size = 9
num_of_mines = 10
else
size = 16
num_of_mines = 40
end
set_solution_board(num_of_mines, size)
set_player_board(size)
end
def set_solution_board(num_of_mines, size)
@solution_board += create_default_board(size,true)
mine_location(num_of_mines,size) # adds mines to the board
number_generator # adds numbers to the board
end
def set_player_board(size)
@player_board += create_default_board(size)
end
def create_default_board(size,solution_board = false)
board = []
size.times do |row|
board << []
size.times do |col|
board[row][col] = '_' if solution_board == true
board[row][col] = '*' if solution_board == false
end
end
board
end
def mine_location(num_of_mines,size)
until @mine_coordinates.length == num_of_mines
coordinate = [rand(size),rand(size)]
@mine_coordinates << coordinate if !@mine_coordinates.include?(coordinate)
end
@mine_coordinates.each do |coord|
x,y = coord
@solution_board[x][y] = 'B'
end
end
def number_generator
adjacent_hash = get_adjacent_hash
adjacent_hash.each do |coord, count|
x,y = coord
@solution_board[x][y] = "#{count}"
end
end
def get_adjacent_squares
adjacent_squares = []
size = @solution_board.length - 1
@mine_coordinates.each do |coord|
@neighbors_key.each do |diff|
x, y = coord[0] + diff[0], coord[1] + diff[1]
location = [x,y]
if x.between?(0,size) && y.between?(0,size) && !@mine_coordinates.include?([x,y])
adjacent_squares << location
end
end
end
adjacent_squares
end
def get_adjacent_hash
adjacent_squares = get_adjacent_squares
adjacent_hash = Hash.new(0)
adjacent_squares.each do |coord|
adjacent_hash[coord] += 1
end
adjacent_hash
end
def answer
print " "
@solution_board.each_index {|index| print "#{index}".center(5)}
puts
@solution_board.each_with_index do |line, i|
print "#{i}".center(3)
print line
puts
end
end
end
mines = Minesweeper.new
mines.start_game