-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.rb
More file actions
244 lines (211 loc) · 5.83 KB
/
minesweeper.rb
File metadata and controls
244 lines (211 loc) · 5.83 KB
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
require 'debugger'
require 'yaml'
require 'colorize'
class Minesweeper
attr_accessor :gameboard, :name, :size, :mines
def initialize
user_settings
end
def user_settings
puts "What's your name?"
@name = gets.chomp
puts "Do you want to load a saved game? Yes/No"
@saved = gets.chomp.downcase
if @saved != "yes"
puts "Board size?"
@size = gets.chomp.to_i
puts "How many mines?"
@mines = gets.chomp.to_i
end
end
def run
if @saved == "yes"
@gameboard = YAML::load(File.read("#{@name}-minesweeper.txt"))
else
@gameboard = Board.new(@size, @mines)
end
@gameboard.print_high_scores
start_time = Time.now
until @gameboard.over
@gameboard.display
play_turn
@gameboard.check_win(@name, Time.now - start_time)
end
@gameboard.display
@gameboard.print_high_scores
end
def play_turn
puts "Input position to check (x,y)? ex. 1,2 or press 'f' for flag or 's' to save"
input = gets.chomp
until Regexp.new(/^\d,\d$/).match(input) || Regexp.new(/^[fs]$/).match(input)
puts "Invalid input, try again:"
input = gets.chomp
end
if input == 'f'
flag_input
elsif input == 's'
save_game
else
input = input.split(',')
input_array = input.map { |x| x.to_i}.reverse
@gameboard.update(input_array)
end
end
def flag_input
puts "Which position do you want to flag? Or to un-flag, re-enter a flagged position"
flag = gets.chomp
until Regexp.new(/^\d,\d$/).match(flag)
puts "Invalid input, try again:"
flag = gets.chomp
end
flag = flag.split(',')
flag_array = flag.map { |x| x.to_i}.reverse
@gameboard.flag_position(flag_array)
puts "Position Flagged."
end
def save_game
saved_board = @gameboard.to_yaml
File.open("#{@name}-minesweeper.txt", 'w') do |line|
line.puts saved_board
end
@gameboard.over = true
puts "Game has been saved in #{@name}-minesweeper.txt"
end
end
class Board
attr_accessor :mine_pos, :over, :check_pos, :flagged_pos
def initialize(size = 9, mines = 20)
@size = size
@mines = mines
@mine_pos = []
@flagged_pos = []
@over = false
@high_scores = []
@board = Array.new(@size) { ['*'] * @size }
set_mines(@size, mines)
end
def set_mines(size, mines)
counter = 0
while counter < mines
pos = [rand(size), rand(size)]
unless mine_pos.include?(pos)
@mine_pos << pos
counter += 1
end
end
end
def display
print " "
(0..@size-1).each { |x| print x.to_s + " "}
puts
print " #{'_ '*@size}"
puts
@board.each_with_index do |row, index|
puts "#{index} |" + row.join(" ")
#print "\n"
end
end
def check_win(name, duration)
stars = @board.inject(0) { |total, row| total + row.count("*") }
if @flagged_pos.length + stars == @mines
@over = true
puts "Congratulations, you won!!"
high_scores(name, duration)
end
end
def update(pos)
if @mine_pos.include?(pos)
gameover(pos)
else
search_surrounding_spots(pos)
end
end
def search_surrounding_spots(pos)
queue = [pos]
checked = []
until queue.empty?
this_pos = queue.shift
checked << this_pos
num_bombs = check_bomb(this_pos)
if num_bombs == 0
reveal_blanks(this_pos, checked, queue)
else
reveal_bomb_count(this_pos, num_bombs)
end
end
end
def reveal_blanks(this_pos, checked, queue)
@board[this_pos[0]][this_pos[1]] = '_'.colorize( :white )
@check_pos.each do |position|
unless checked.include?(position) || queue.include?(position) || @flagged_pos.include?(position)
queue << position
@board[position[0]][position[1]] = '_'.colorize( :white)
end
end
end
def reveal_bomb_count(this_pos, num_bombs)
unless @flagged_pos.include?(this_pos)
case num_bombs
when 1
@board[this_pos[0]][this_pos[1]] = num_bombs.to_s.colorize( :blue )
when 2
@board[this_pos[0]][this_pos[1]] = num_bombs.to_s.colorize( :green )
when 3
@board[this_pos[0]][this_pos[1]] = num_bombs.to_s.colorize( :red )
else
@board[this_pos[0]][this_pos[1]] = num_bombs.to_s.colorize( :magenta )
end
end
end
def gameover(pos)
@over = true
puts "You hit a bomb! Gameover"
@board[pos[0]][pos[1]] = 'B'.colorize( :color => :red, :background => :blue)
end
def flag_position(pos)
if @board[pos[0]][pos[1]] == '*'
@board[pos[0]][pos[1]] = 'F'
@flagged_pos << pos
elsif @board[pos[0]][pos[1]] == 'F'
@board[pos[0]][pos[1]] = '*'
@flagged_pos.slice!(@flagged_pos.index(pos))
else
puts "Try again"
end
end
def check_bomb(pos)
counter = 0
@check_pos = [
[pos[0],pos[1]+1], [pos[0]+1,pos[1]],
[pos[0]+1,pos[1]+1], [pos[0],pos[1]-1],
[pos[0]-1,pos[1]], [pos[0]-1,pos[1]-1],
[pos[0]-1,pos[1]+1], [pos[0]+1,pos[1]-1]
]
@check_pos.select! do |position|
position[0] < @size && position[1] < @size && position[0] >= 0 && position[1] >= 0
end
@check_pos.each do |coord|
if @mine_pos.include?(coord)
counter += 1
end
end
counter
end
def high_scores(name, duration)
@high_scores = YAML::load(File.read("high-scores-minesweeper.txt"))
@high_scores << [name, @size, @mines, duration]
File.open("High-scores-minesweeper.txt", 'w') do |line|
line.puts @high_scores.to_yaml
end
end
def print_high_scores
high_scores = YAML::load(File.read("high-scores-minesweeper.txt"))
puts "**HIGH SCORES**"
high_scores.sort!{ |num1, num2| num1[3] <=> num2[3]}
high_scores.each do |entry|
print "Name: #{entry[0]} Board size: #{entry[1]} Mines: #{entry[2]} Time: #{entry[3]} \n"
end
end
end
y = Minesweeper.new
y.run