-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminesweeper.py
139 lines (121 loc) · 3.47 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
import random
import re
def ratio(n1, n2, n3):
return int((n2*n3)/n1)
def print_map(fl): # ┏ ┑┖ ┚─
head = " "
for x in range(1, len(fl[0]) + 1):
head += str(x) + " "
print(head)
head = " " + "_ "*(len(fl[0]))
print(head)
for x in range(1, len(fl) + 1):
print(str(x) + "|" + " ".join(fl[x-1]) + "|")
def num_set(x, y, fl):
bombs = 0
for a in range(-1, 2):
for b in range(-1, 2):
if a == 0 and b == 0:
continue
if x+a >= 0 and y+b >= 0:
try:
if fl[x+a][y+b] == "X":
bombs += 1
except IndexError:
pass
return str(bombs)
def destroy(x, y, fl, curr):
curr[x][y] = "0"
done = True
for a in range(-1, 2):
for b in range(-1, 2):
if a == 0 and b == 0:
continue
if x + a >= 0 and y + b >= 0:
try:
if curr[x+a][y+b] != fl[x+a][y+b]:
curr[x+a][y+b] = fl[x+a][y+b]
done = False
except IndexError:
pass
if done:
return
for a in range(-1, 2):
for b in range(-1, 2):
if x + a >= 0 and y + b >= 0:
try:
if curr[x + a][y + b] == "0":
destroy(x + a, y + b, fl, curr)
except IndexError:
pass
def won(curr, fl):
st1 = True
st2 = True
for x in range(len(fl)):
for y in range(len(fl[x])):
if fl[x][y] == "X" and curr[x][y] != "?":
st1 = False
break
if not st1:
break
for x in range(len(fl)):
for y in range(len(fl[x])):
if fl[x][y] != "X" and curr[x][y] != fl[x][y]:
st2 = False
break
if not st2:
break
return st1 or st2
rows = int(input("Enter rows: "))
cols = int(input("Enter columns: "))
fl = [["O" for x in range(cols)] for x in range(rows)]
curr = [["P" for x in range(cols)] for x in range(rows)]
S = rows*cols
bombs = ratio(9, 2, S)
for z in range(bombs):
x = random.randint(0, rows-1)
y = random.randint(0, cols-1)
while fl[x][y] == "X":
x = random.randint(0, rows-1)
y = random.randint(0, cols-1)
fl[x][y] = "X"
for x in range(len(fl)):
for y in range(len(fl[x])):
if fl[x][y] == "O":
fl[x][y] = num_set(x, y, fl)
dn = False
for x in range(len(fl)):
for y in range(len(fl[x])):
if fl[x][y] == "0":
destroy(x, y, fl, curr)
dn = True
break
if dn:
break
while True:
print_map(curr)
cords = input("Enter Row,Column(M:Row,Column - to mark/unmark a bomb): ")
cords = re.search(r"(?P<mark>M:)?(?P<x>\d+),(?P<y>\d+)", cords)
x = int(cords.group("x")) - 1
y = int(cords.group("y")) - 1
mark = cords.group("mark") == "M:"
square = fl[x][y]
curr_sq = curr[x][y]
if not mark:
if square == "X":
print_map(fl)
print("Game Over.")
exit()
elif square != "X" and square != "0":
curr[x][y] = square
elif square == "0":
destroy(x, y, fl, curr)
else:
if curr_sq == "?":
curr[x][y] = "P"
else:
curr[x][y] = "?"
if won(curr, fl):
print_map(fl)
print("YOU WON!!")
break