-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.rb
175 lines (158 loc) · 3.85 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
require "debugger"
require "yaml"
class MineSweeper
NEIGHBORS = [
[-1, 1],
[ 0, 1],
[ 1, 1],
[ 1, 0],
[ 1,-1],
[ 0,-1],
[-1,-1],
[-1, 0]
]
def initialize
@board_arr = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]
]
@user_board = [
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*","*","*"]
]
@flag_arr = []
@user_choice = ""
## generating random bombs
10.times do
x = rand(9)
y = rand(9)
@board_arr[x][y] = "b"
NEIGHBORS.each do |space|
if in_bounds? (x+space[0]),(y+space[1])
@board_arr[x+space[0]][y+space[1]] += 1 unless @board_arr[x+space[0]][y+space[1]] == "b"
end
end
end
end
def play
@user_choice = ""
print_board("u")
until @user_choice == "q" || flag_check == true
puts "[R]eveal,[F]lag, [Q]uit, or [S]ave?"
@user_choice = gets.chomp.downcase
if @user_choice == "f" || @user_choice == "r"
puts "Enter your coordinates as (Row, Column):"
user_coor = gets.chomp.split(',')
x, y = user_coor[0].to_i, user_coor[1].to_i
if @user_choice == "f"
self.flag_set(x, y)
else @user_choice == "r"
self.reveal(x, y)
end
elsif @user_choice == "s"
save_game
elsif @user_choice == "q"
puts "Thanks for playing"
else
puts "Invalid entry"
end
end
if flag_check == true
puts "You da bomb!"
end
end
def save_game
File.open("saved_game.yaml", "w") do |f|
f.puts self.to_yaml
end
end
def in_bounds?(x,y)
return true if x <9 && x >= 0 && y <9 && y >= 0
end
def print_board(choice)
if choice == "b"
for i in (0...@board_arr.length)
p @board_arr[i]
end
else
for i in (0...@user_board.length)
p @user_board[i]
end
end
end
def reveal(x, y)
if @board_arr[x][y] == "b"
puts "GAME OVER\n\n"
print_board("b")
@user_choice = "q"
elsif @board_arr[x][y] == 0 ## check adjacent cells if they contain bomb
reveal_neighbors(x,y)
print_board("u")
else
@user_board[x][y] = @board_arr[x][y].to_s
print_board("u")
end
end
def reveal_neighbors(x, y) ## recursive until false
#debugger
if @board_arr[x][y] == 0
@user_board[x][y] = "0"
NEIGHBORS.each do |space|
new_x = x+space[0]
new_y = y+space[1]
if in_bounds?(new_x, new_y) && @board_arr[new_x][new_y] != "b"
if @user_board[new_x][new_y] == @board_arr[new_x][new_y].to_s
next
else
@user_board[new_x][new_y] = @board_arr[new_x][new_y].to_s
reveal_neighbors(new_x,new_y)
end
end
end
end
end
def flag_set(x, y)
if @user_board[x][y] != "F"
@user_board[x][y] = "F"
@flag_arr << [x, y]
else
@user_board[x][y] = "*"
@flag_arr.delete([x, y])
end
print_board("u")
end
## checks user's flags when user has set all 10 of his/her flags
def flag_check
@flag_arr.each do |flag|
x, y = flag[0], flag[1]
if @board_arr[x][y] != "b"
return false
end
end
if @flag_arr.length == 10
true
else
false
end
end
end
if ARGV.length > 0
m = YAML.load_file(ARGV.shift)
else
m = MineSweeper.new
end
m.play