-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminesweeper.py
178 lines (155 loc) · 5.44 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
import random
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
# declare a two-dimension array (9 x 9) with each element value as 0
# -:Un-swapped; m:mine; 2:found-mine; 3:failed mine; 4; empty
rows, cols = (8, 8)
fieldArray = [['-' for i in range(cols)] for j in range(rows)]
def laying_mine(count):
rate = rows * cols / count
mined = 0
while mined < count:
for r in range(rows):
for c in range(cols):
if mined < count:
if fieldArray[r][c] != 'm':
if 1 == random.randint(0, rate.__round__()):
fieldArray[r][c] = 'm'
mined = mined + 1
def print_field(display_mine):
head = []
row_number = 0
for i in range(cols):
head.append(i)
print(' ', end='')
print(head)
for line in fieldArray:
print(row_number, end=': ')
for e in line:
if display_mine is False:
if e == '-':
print('-'+', ',end='')
if e == 'm':
print('-'+', ',end='')
if e == 'r' or e == 'f':
print('P'+', ',end='')
if e == 'u':
print('u' + ', ', end='')
if e == 'b':
print('m' + ', ', end='')
if str(e).isdigit() is True:
print(str(e) + ', ', end='')
if display_mine is True:
print(str(e) + ', ', end='')
print('\n')
row_number += 1
def is_num(value):
try:
value+1
except TypeError:
return False
else:
return True
def insert_m(x,y):
if fieldArray[y][x] == '-':
fieldArray[y][x] = 'f' # false mine
elif fieldArray[y][x] == 'f':
fieldArray[y][x] = '-'
if fieldArray[y][x] == 'm':
fieldArray[y][x] = 'r' # real mine
elif fieldArray[y][x] == 'r':
fieldArray[y][x] = 'm'
def count_mine_in_game():
findmine = 0
for i in range(rows):
for j in range(cols):
if 'm' == fieldArray[i][j]:
findmine += 1
if 'f' == fieldArray[i][j]:
findmine += 1
return findmine
def around_mine_num(x, y):
minenum = 0
if y >= 1 and x >= 1: # top left
if fieldArray[y - 1][x - 1] == 'm' or fieldArray[y - 1][x - 1] == 'r':
minenum += 1
if y >= 1: # top
if fieldArray[y - 1][x] == 'm' or fieldArray[y - 1][x] == 'r':
minenum += 1
if y >= 1 and x <= cols - 2: # top right
if fieldArray[y - 1][x + 1] == 'm' or fieldArray[y - 1][x + 1] == 'r':
minenum += 1
if x >= 1: # left
if fieldArray[y][x - 1] == 'm' or fieldArray[y][x - 1] == 'r':
minenum += 1
if x <= cols - 2: # right
if fieldArray[y][x + 1] == 'm' or fieldArray[y][x + 1] == 'r':
minenum += 1
if y <= rows - 2: # down
if fieldArray[y + 1][x] == 'm' or fieldArray[y + 1][x] == 'r':
minenum += 1
if x >= 1 and y <= rows - 2: # down left
if fieldArray[y + 1][x - 1] == 'm' or fieldArray[y + 1][x - 1] == 'r':
minenum += 1
if x <= cols - 2 and y <= rows - 2: # down right
if fieldArray[y + 1][x + 1] == 'm' or fieldArray[y + 1][x + 1] == 'r':
minenum += 1
return minenum
def insert_wrong_or_right(insertlist, rows, cols):
wrong_input = 0
if wrong_input == 0:
if len(insertlist) != 3:
print('wrong format!')
wrong_input = 1
if wrong_input == 0:
if not insertlist[0].isdigit() or not insertlist[1].isdigit():
print('not digit!')
wrong_input = 1
insertlist[0] = int(insertlist[0])
insertlist[1] = int(insertlist[1])
print(insertlist)
if wrong_input == 0:
if insertlist[0] > rows - 1 or insertlist[1] > cols - 1:
print('out of limit!')
wrong_input = 1
if wrong_input == 0:
if insertlist[2] != 'm' and insertlist[2] != 'n':
print('not m or n!')
wrong_input = 1
if wrong_input == 0:
return 1
def main_loop():
win = 0
while win == 0:
insert_right = 0
print_field(True) # real value in list
print_field(False) # display value
insert = input("Insert in num(y),num(x),n/m: ")
insertlist = insert.split(',')
insert_right = insert_wrong_or_right(insertlist,rows,cols)
if insert_right == 1:
y = int(insertlist[0])
x = int(insertlist[1])
mn = insertlist[2]
print('correct input!')
print(y, x, mn)
if mn == 'n':
if fieldArray[y][x] == 'm':
fieldArray[y][x] = 'b' # boom
win = -1
print('game over')
print_field(True)
if fieldArray[y][x] == '-':
fieldArray[y][x] = 'u' # discovered
fieldArray[y][x] = around_mine_num(x,y)
if mn == 'm':
insert_m(x, y)
if count_mine_in_game() == 0:
print('Win !')
win = 1
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
win = 0
laying_mine(10)
main_loop()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/