forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minesweeper.py
340 lines (273 loc) · 6.97 KB
/
Minesweeper.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Importing packages
import random
import os
# Printing the Minesweeper Layout
def print_mines_layout():
global mine_values
global n
print()
print("\t\t\tMINESWEEPER\n")
st = " "
for i in range(n):
st = st + " " + str(i + 1)
print(st)
for r in range(n):
st = " "
if r == 0:
for col in range(n):
st = st + "______"
print(st)
st = " "
for col in range(n):
st = st + "| "
print(st + "|")
st = " " + str(r + 1) + " "
for col in range(n):
st = st + "| " + str(mine_values[r][col]) + " "
print(st + "|")
st = " "
for col in range(n):
st = st + "|_____"
print(st + '|')
print()
# Function for setting up Mines
def set_mines():
global numbers
global mines_no
global n
# Track of number of mines already set up
count = 0
while count < mines_no:
# Random number from all possible grid positions
val = random.randint(0, n*n-1)
# Generating row and column from the number
r = val // n
col = val % n
# Place the mine, if it doesn't already have one
if numbers[r][col] != -1:
count = count + 1
numbers[r][col] = -1
# Function for setting up the other grid values
def set_values():
global numbers
global n
# Loop for counting each cell value
for r in range(n):
for col in range(n):
# Skip, if it contains a mine
if numbers[r][col] == -1:
continue
# Check up
if r > 0 and numbers[r-1][col] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check down
if r < n-1 and numbers[r+1][col] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check left
if col > 0 and numbers[r][col-1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check right
if col < n-1 and numbers[r][col+1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check top-left
if r > 0 and col > 0 and numbers[r-1][col-1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check top-right
if r > 0 and col < n-1 and numbers[r-1][col+1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check below-left
if r < n-1 and col > 0 and numbers[r+1][col-1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Check below-right
if r < n-1 and col < n-1 and numbers[r+1][col+1] == -1:
numbers[r][col] = numbers[r][col] + 1
# Recursive function to display all zero-valued neighbours
def neighbours(r, col):
global mine_values
global numbers
global vis
# If the cell already not visited
if [r,col] not in vis:
# Mark the cell visited
vis.append([r,col])
# If the cell is zero-valued
if numbers[r][col] == 0:
# Display it to the user
mine_values[r][col] = numbers[r][col]
# Recursive calls for the neighbouring cells
if r > 0:
neighbours(r-1, col)
if r < n-1:
neighbours(r+1, col)
if col > 0:
neighbours(r, col-1)
if col < n-1:
neighbours(r, col+1)
if r > 0 and col > 0:
neighbours(r-1, col-1)
if r > 0 and col < n-1:
neighbours(r-1, col+1)
if r < n-1 and col > 0:
neighbours(r+1, col-1)
if r < n-1 and col < n-1:
neighbours(r+1, col+1)
# If the cell is not zero-valued
if numbers[r][col] != 0:
mine_values[r][col] = numbers[r][col]
# Function for clearing the terminal
def clear():
os.system("clear")
# Function to display the instructions
def instructions():
print("Instructions:")
print("1. Enter row and column number to select a cell, Example \"2 3\"")
print("2. In order to flag a mine, enter F after row and column numbers, Example \"2 3 F\"")
# Function to check for completion of the game
def check_over():
global mine_values
global n
global mines_no
# Count of all numbered values
count = 0
# Loop for checking each cell in the grid
for r in range(n):
for col in range(n):
# If cell not empty or flagged
if mine_values[r][col] != ' ' and mine_values[r][col] != 'F':
count = count + 1
# Count comparison
if count == n * n - mines_no:
return True
else:
return False
# Display all the mine locations
def show_mines():
global mine_values
global numbers
global n
for r in range(n):
for col in range(n):
if numbers[r][col] == -1:
mine_values[r][col] = 'M'
if __name__ == "__main__":
# Size of grid
n = 8
# Number of mines
mines_no = 8
# The actual values of the grid
numbers = [[0 for y in range(n)] for x in range(n)]
# The apparent values of the grid
mine_values = [[' ' for y in range(n)] for x in range(n)]
# The positions that have been flagged
flags = []
# Set the mines
set_mines()
# Set the values
set_values()
# Display the instructions
instructions()
# Variable for maintaining Game Loop
over = False
# The GAME LOOP
while not over:
print_mines_layout()
# Input from the user
inp = input("Enter row number followed by space and column number = ").split()
# Standard input
if len(inp) == 2:
# Try block to handle errant input
try:
val = list(map(int, inp))
except ValueError:
clear()
print("Wrong input!")
instructions()
continue
# Flag input
elif len(inp) == 3:
if inp[2] != 'F' and inp[2] != 'f':
clear()
print("Wrong Input!")
instructions()
continue
# Try block to handle errant input
try:
val = list(map(int, inp[:2]))
except ValueError:
clear()
print("Wrong input!")
instructions()
continue
# Sanity checks
if val[0] > n or val[0] < 1 or val[1] > n or val[1] < 1:
clear()
print("Wrong input!")
instructions()
continue
# Get row and column numbers
r = val[0]-1
col = val[1]-1
# If cell already been flagged
if [r, col] in flags:
clear()
print("Flag already set")
continue
# If cell already been displayed
if mine_values[r][col] != ' ':
clear()
print("Value already known")
continue
# Check the number for flags
if len(flags) < mines_no:
clear()
print("Flag set")
# Adding flag to the list
flags.append([r, col])
# Set the flag for display
mine_values[r][col] = 'F'
continue
else:
clear()
print("Flags finished")
continue
else:
clear()
print("Wrong input!")
instructions()
continue
# Sanity checks
if val[0] > n or val[0] < 1 or val[1] > n or val[1] < 1:
clear()
print("Wrong Input!")
instructions()
continue
# Get row and column number
r = val[0]-1
col = val[1]-1
# Unflag the cell if already flagged
if [r, col] in flags:
flags.remove([r, col])
# If landing on a mine --- GAME OVER
if numbers[r][col] == -1:
mine_values[r][col] = 'M'
show_mines()
print_mines_layout()
print("Landed on a mine. GAME OVER!!!!!")
over = True
continue
# If landing on a cell with 0 mines in neighboring cells
elif numbers[r][col] == 0:
vis = []
mine_values[r][col] = '0'
neighbours(r, col)
# If selecting a cell with atleast 1 mine in neighboring cells
else:
mine_values[r][col] = numbers[r][col]
# Check for game completion
if(check_over()):
show_mines()
print_mines_layout()
print("Congratulations!!! YOU WIN")
over = True
continue
clear()