-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.rb
211 lines (185 loc) · 4.74 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
require 'yaml'
#test
class Minesweeper
attr_reader :bombs, :view, :model
def initialize(board_size = 9)
@board_size = board_size - 1
@model = create_board("_")
@view = create_board("*")
@bombs = []
@bomb_count = get_bomb_count
@flags = []
@game_status = "playing"
@start_time = nil
@end_time = nil
insert_bombs
insert_numbers
end
def get_bomb_count
if @board_size == 8
bomb_count = 10
else
bomb_count = 40
end
bomb_count
end
def insert_bombs
until @bombs.count == @bomb_count
i = Random.new.rand(0..@board_size)
j = Random.new.rand(0..@board_size)
@bombs << [i,j] unless @bombs.include?([i,j])
end
@bombs.each do |bomb|
@model[bomb[0]][bomb[1]] = "B"
end
end
def insert_numbers
@model.each_with_index do |row, i|
row.each_with_index do |position, j|
next if @model[i][j] == "B"
bomb_count = count_adjacent_bombs([i,j])
@model[i][j] = bomb_count unless (bomb_count == 0)
end
end
end
def count_adjacent_bombs(curr_pos)
bomb_count = 0
trans_array = [ [0,-1], [1,-1], [1,0],
[1,1], [0,1], [-1,1],
[-1,0], [-1,-1] ]
trans_array.each do |trans|
row, pos = (curr_pos[0] + trans[0]), (curr_pos[1] + trans[1])
unless row < 0 or row > (@board_size) or pos < 0 or pos > (@board_size)
if @model[row][pos] == "B"
bomb_count += 1
end
end
end
bomb_count
end
def process_all_around_blank(starting_coord)
queue = []
queue << starting_coord
until queue.empty?
coord = queue.shift
trans_array = [ [0,-1], [1,-1], [1,0],
[1,1], [0,1], [-1,1],
[-1,0], [-1,-1] ]
trans_array.each do |trans|
row, pos = (coord[0] + trans[0]), (coord[1] + trans[1])
unless row < 0 or row > @board_size or pos < 0 or pos > @board_size
if @model[row][pos] != "_"
reveal_in_view([row, pos])
elsif @model[row][pos] == "_" && @view[row][pos] == "*" && @view[row][pos] != 'F'
queue << [row, pos]
reveal_in_view([row, pos])
end
end
end
end
end
def play
puts "Input Format: (r)eveal/(f)ormat 0-8 0-8"
puts "Example: r 1 3"
@start_time = Time.now
until @game_status == "lose" or @game_status == "win"
option, coord = collect_input
case option
when "f"
set_flag(coord)
if @flags.count == @bomb_count
if all_flags_correct?
@game_status = "win"
@end_time = Time.now
else
@game_status = "lose"
end
end
when "r"
process_selection(coord)
when "s"
save_to_file
puts "Game saved to ./saved_game.yaml"
break
end
if @game_status == "win"
total_time = (@end_time - @start_time).to_i
puts "You win! It took you #{total_time} seconds"
File.open("top_times.txt", "a") do |f|
f.puts total_time
end
top_times = []
File.foreach("top_times.txt") do |line|
top_times << line.chomp + " seconds"
end
top_times = top_times[0..10].sort
puts "The top times are #{top_times}"
end
puts "You suck!" if @game_status == "lose"
print_board(@view)
insert_blank_lines(2)
end
end
def save_to_file
File.open("saved_game.yaml", "w"){|file| YAML.dump(self,file)}
end
def insert_blank_lines(num)
num.times { puts "" }
end
def collect_input
input = gets.chomp.split(" ")
return input[0], [input[1].to_i, input[2].to_i]
end
def all_flags_correct?
if @flags.sort == @bombs.sort
true
else
false
end
end
def process_selection(users_choice)
#curr_coord = queue.shift
val = @model[users_choice[0]][users_choice[1]]
if val == 'B'
@bombs.each { |bomb| reveal_in_view(bomb) }
@game_status = "lose"
elsif val != '_'
reveal_in_view(users_choice)
else #it equaled "_"
reveal_in_view(users_choice)
process_all_around_blank(users_choice)
end
end
def reveal_in_view(coord)
@view[coord[0]][coord[1]] = @model[coord[0]][coord[1]]
end
def create_board(value)
model = []
(0..@board_size).each do |i|
model[i] = []
(0..@board_size).each do |j|
model[i][j] = value
end
end
model
end
def set_flag(coord)
@flags << coord
@view[coord[0]][coord[1]] = 'F'
end
def print_board(board)
board.each do |line|
puts line.join(" ")
end
end
end
if $PROGRAM_NAME == __FILE__
if ARGV[0]
file = ARGV.pop
a = YAML.load_file(file)
a.play
else
a = Minesweeper.new
a.play
end
end