-
Notifications
You must be signed in to change notification settings - Fork 0
/
minefield.rb
205 lines (170 loc) · 5.53 KB
/
minefield.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
#---------------------IMPLEMENTATION OF MINEFIELD CLASS-------------------------#
#Public variables made available to minesweeper.rb (main)
class Minefield
MINE = "x"
SURROUNDING_POSITIONS = lambda { |row, col| [[row+1,col-1],[row+1, col],[row+1,col+1],
[row, col-1],[row,col+1],[row-1,col-1],[row-1,col],[row-1, col+1]] }
attr_reader :row_count, :column_count, :field, :mine_count
#---------------------CONSTRUCTOR-------------------#
def initialize(row_count, column_count, mine_count)
@column_count = column_count
@row_count = row_count
@mine_count = mine_count
@mine_found = false
@field = Array.new(row_count) {Array.new(column_count)}
generate_mines
generate_numbers
end
#------------GENERATE FIRST BOARD WITH MINES--------------#
def generate_mines
mine_count.times do
x = rand(column_count-1)
y = rand(row_count-1)
unless contains_mine?(x,y)
x = rand(column_count-1)
y = rand(row_count-1)
end
field[x][y] = MINE
end
end
#---------------GENERATE NUMBERS------------#
def generate_numbers
field.each_with_index do |row,r_index|
row.each_with_index do |col,c_index|
if field[r_index][c_index] != MINE
field[r_index][c_index] = count_adjacent_mines(r_index, c_index)
end
end
end
end
#----------------CHECK IF CELL CLEARED----------#
# Return true if the cell been uncovered, false otherwise.
def cell_cleared?(row, col)
if field[row][col] != MINE
field[row][col] < 0 ? true : false
end
end
# def clear_mines
# field.each_with_index do |row,r_i|
# row.each_with_index do |col,c_i|
# if contians_mine?(r_i,c_i)
# x = r_i
# y = c_i
# end
# end
# end
# x,y
# end
#------------------CLEAR CELL----------------#
# Uncover the given cell. If there are no adjacent mines to this cell
# it should also clear any adjacent cells as well. This is the action
# when the player clicks on the cell.
def clear(row, col)
if field[row][col] != MINE
field[row][col] = field[row][col] * -1
find_and_unveil(row,col)
else
@mine_found = true
end
end
#-----------------------FIND AND UNVEIL----------------#
def find_and_unveil(row,col)
if field[row][col] == -9
neighbors_to_rerun_on = []
SURROUNDING_POSITIONS.call(row, col).each do |rowcol|
unveil_neighbors(rowcol[0],rowcol[1],neighbors_to_rerun_on)
end
# unveil_neighbors(row+1, col-1,neighbors_to_rerun_on)
# unveil_neighbors(row+1, col,neighbors_to_rerun_on)
# unveil_neighbors(row+1, col+1,neighbors_to_rerun_on)
# unveil_neighbors(row, col-1,neighbors_to_rerun_on)
# unveil_neighbors(row, col+1,neighbors_to_rerun_on)
# unveil_neighbors(row-1, col-1,neighbors_to_rerun_on)
# unveil_neighbors(row-1, col,neighbors_to_rerun_on)
# unveil_neighbors(row-1, col+1,neighbors_to_rerun_on)
neighbors_to_rerun_on.each do |cell_pair|
find_and_unveil(cell_pair[0],cell_pair[1])
end
end
end
#---------------------UNVEIL NEIGHBORS---------------#
def unveil_neighbors(row,col,neighbors_to_rerun_on)
if in_board?(row,col)
if field[row][col] != MINE
if field[row][col] > 0
if field[row][col] == 9
neighbors_to_rerun_on << [row,col]
end
field[row][col] = field[row][col] * -1
end
end
end
end
#----------------ANY_MINES_DETONATED--------------#
# Check if any cells have been uncovered that also contained a mine. This is
# the condition used to see if the player has lost the game.
def any_mines_detonated?
@mine_found
end
#-----------------CHECK IF ALL_CELLS_CLEARED-------------#
# Check if all cells that don't have mines have been uncovered. This is the
# condition used to see if the player has won the game.
def all_cells_cleared?
total_cells = row_count * column_count
cells_cleared = 0
field.each_with_index do |row,r_i|
row.each_with_index do |col,c_i|
if field[r_i][c_i] != MINE
if (field[r_i][c_i] < 0)
cells_cleared += 1
end
end
end
end
cells_cleared + 38 == total_cells ? (return true) : (return false)
end
#-------------------CHECK IF CONTAINS MINE------------------#
def contains_mine?(row, col)
if field[row][col] == MINE
return true
end
end
#-------------------COUNT_ADJACENT MINES---------------------#
def count_adjacent_mines(row, col)
num = 0
SURROUNDING_POSITIONS.call(row, col).each do |rowcol|
num += check_tile(rowcol[0],rowcol[1])
end
# num += check_tile(row-1, col-1)
# num += check_tile(row-1, col)
# num += check_tile(row-1, col+1)
# num += check_tile(row, col-1)
# num += check_tile(row, col+1)
# num += check_tile(row+1, col-1)
# num += check_tile(row+1, col)
# num += check_tile(row+1, col+1)
num == 0 ? (num = 9) : num
num
end
#--------------------ADJACENT MINES------------------#
def adjacent_mines(row, col)
value_to_display = (field[row][col]).abs
if value_to_display == 9
value_to_display = 0
end
value_to_display
end
#---------------CHECK TILE---------------#
def check_tile(row,col)
if in_board?(row,col)
if @field[row][col] == MINE
return 1
end
end
return 0
end
#---------------CHECK IF IN BOARD--------------#
def in_board?(row, col)
(row >= 0 && col >= 0) && (row <= row_count-1 && col <= column_count-1)
end
end