-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.rb
84 lines (67 loc) · 1.58 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
require './tile'
require './mineboard'
require 'yaml'
class Minesweeper
attr_accessor :board, :total_tiles, :num_bombs
def initialize(rows = 9, cols = 9, num_bombs = 9)
@rows = rows
@cols = cols
@total_tiles = rows * cols
@num_bombs = num_bombs
@board = Mineboard.new(@rows, @cols, @num_bombs)
end
def self.load(filename = "Minesweeper")
# yaml_string = File.read(filename)
YAML::load_file(filename)
end
def run
until @board.won? || @board.lost?
board.display
board.move_cursor
end
display_end
end
def save(filename)
File.open(filename, "w") do |f|
f.puts self.to_yaml
end
end
def save_request
print "Do you want to save the game (y/n)? "
if gets.chomp.downcase == "y"
print "Enter the filename: "
save(gets.chomp)
end
end
def display_end
won_text = 'Congratulations!! You won!!'
lost_text = 'You stepped on a bomb!! Game over.'
text = @board.won? ? won_text : lost_text
board.reveal_all
board.display
puts text
end
def ask_flag
print "1 for flag, 2 for reveal? "
gets.chomp.to_i
end
def ask_position
print "What row? "
row = gets.chomp.to_i
print "What column? "
col = gets.chomp.to_i
[row, col]
end
end
if __FILE__ == $PROGRAM_NAME
print "Would you like to load a game (y/n) "
if gets.chomp.downcase == 'y'
print "Enter filename: "
filename = gets.chomp
old_game = Minesweeper.load(filename)
old_game.run
else
game = Minesweeper.new(9, 9, 5)
game.run
end
end