-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.py
85 lines (81 loc) · 2.06 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
import random
def minesweeper():
rows, cols = (8, 8)
arr = [[0 for i in range(cols)] for j in range(rows)]
xcount = 0
while xcount < 8: #fill the grid with 8 bombs
bombrow = random.randint(0, rows-1)
bombcol = random.randint(0, cols-1)
if arr[bombrow][bombcol] != 'X':
arr[bombrow][bombcol] = 'X'
#update adjacent squares to show the right number
try:
if bombrow > 0 and bombcol > 0:
arr[bombrow-1][bombcol-1] += 1
except:
pass
try:
if bombcol > 0:
arr[bombrow][bombcol-1] += 1
except:
pass
try:
if bombrow < rows-1 and bombcol > 0:
arr[bombrow+1][bombcol-1] += 1
except:
pass
try:
if bombrow > 0:
arr[bombrow-1][bombcol] += 1
except:
pass
try:
if bombrow < rows-1:
arr[bombrow+1][bombcol] += 1
except:
pass
try:
if bombrow > 0 and bombcol < cols-1:
arr[bombrow-1][bombcol+1] += 1
except:
pass
try:
if bombcol < cols-1:
arr[bombrow][bombcol+1] += 1
except:
pass
try:
if bombrow < rows-1 and bombcol < cols-1:
arr[bombrow+1][bombcol+1] += 1
except:
print("Exception!")
pass
xcount += 1
#convert grid to string
msg = ""
for row in range(rows):
for col in range(cols):
if arr[row][col] == 0:
msg += "||⬜|| "
elif arr[row][col] == 'X':
msg += "||💣|| "
elif arr[row][col] == 1:
msg += "||1️⃣|| "
elif arr[row][col] == 2:
msg += "||2️⃣|| "
elif arr[row][col] == 3:
msg += "||3️⃣|| "
elif arr[row][col] == 4:
msg += "||4️⃣|| "
elif arr[row][col] == 5:
msg += "||5️⃣|| "
elif arr[row][col] == 6:
msg += "||6️⃣|| "
elif arr[row][col] == 7:
msg += "||7️⃣|| "
elif arr[row][col] == 8:
msg += "||8️⃣|| "
else:
msg += str(arr[row][col])
msg += "\n"
return msg