-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.rb
63 lines (53 loc) · 1.12 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
require_relative 'board'
require 'yaml'
class Minesweeper
def initialize
@board = Board.new
end
def play
until game_over?
system "clear"
@board.render
puts "Enter a position [row, col]"
position = gets.chomp.split(", ").map(&:to_i)
puts "Reveal, flag, unflag, or save? [r/f/u/s]"
option = gets.chomp
if option == "r"
won = @board.reveal(position)
break unless won
elsif option == "f"
@board[position].flag
elsif option == "u"
@board[position].unflag
else
save
won = "saved"
break
end
# break unless won
end
if won == "saved"
puts "Game saved!"
elsif won
puts "You win!"
else
puts "You lose!"
end
end
def save
puts "Enter filename to save at:"
filename = gets.chomp
File.write(filename, self.to_yaml)
end
def game_over?
@board.mine_locations.all? { |location| @board[location].flagged? }
end
end
if $PROGRAM_NAME == __FILE__
case ARGV.count
when 0
Minesweeper.new.play
when 1
YAML.load_file(ARGV.shift).play
end
end