-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path扫雷.py
290 lines (231 loc) · 8.84 KB
/
扫雷.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
import queue
from random import randint
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.QtCore import QObject, pyqtSignal
# rqwrwq fwq
dx = [1, -1, 0, 0, -1, 1, -1, 1] # 坐标
dy = [0, 0, 1, -1, -1, 1, 1, -1]
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(583, 476)
self.gridLayoutWidget = QtWidgets.QWidget(Form)
self.gridLayoutWidget.setGeometry(QtCore.QRect(150, 130, 251, 171))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.la_r = QtWidgets.QLabel(self.gridLayoutWidget)
self.la_r.setStyleSheet("font: 14pt \"Arial\";")
self.la_r.setObjectName("la_r")
self.gridLayout.addWidget(self.la_r, 0, 0, 1, 1)
self.la_c = QtWidgets.QLabel(self.gridLayoutWidget)
self.la_c.setStyleSheet("font: 14pt \"Arial\";")
self.la_c.setObjectName("la_c")
self.gridLayout.addWidget(self.la_c, 1, 0, 1, 1)
self.line_c = QtWidgets.QLineEdit(self.gridLayoutWidget)
self.line_c.setObjectName("line_c")
self.gridLayout.addWidget(self.line_c, 0, 1, 1, 1)
self.line_r = QtWidgets.QLineEdit(self.gridLayoutWidget)
self.line_r.setObjectName("line_r")
self.gridLayout.addWidget(self.line_r, 1, 1, 1, 1)
self.linker = QtWidgets.QCommandLinkButton(Form)
self.linker.setGeometry(QtCore.QRect(440, 407, 101, 51))
self.linker.setObjectName("linker")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.la_r.setText(_translate("Form", "请输入列数"))
self.la_c.setText(_translate("Form", "请输入列数"))
self.linker.setText(_translate("Form", "继续"))
class Mybutton(QtWidgets.QPushButton):
def set(self, x, y):
self.x = x
self.y = y
def send(self):
# print(self.x, self.y)
# self.hide()
deal(int(self.x), int(self.y))
pass
class game(object):
# def __init__(self, Form,r,c):
# Form.setObjectName("Form")
# Form.resize(r*50, c*50)
def setupUi(self, Form):
Form.setObjectName("Form")
# self.hide()
Form.resize(583, 476)
self.r = -1
self.c = -1
self.buttons = []
self.la_c = QtWidgets.QLabel(self)
self.la_c.setStyleSheet("font: 14pt \"Arial\";")
self.la_c.setObjectName("la_c")
def set(self, r, c, Form):
print(r, c)
Form.resize(int(r) * 50, int(c) * 50)
self.r = r
self.c = c
self.buttons = []
for i in range(int(r)):
# print(i)
tmp = []
for j in range(int(c)):
# print(i, " ", j)
tmp.append(Mybutton(self))
# tmp[j].move(i*20+50,j*20+50)
tmp[j].set(i, j)
tmp[j].clicked.connect(tmp[j].send)
# tmp[j].send.connect(self.deal)
tmp[j].setGeometry(i * 20 + 50, j * 20 + 50, 20, 20)
self.buttons.append(tmp)
mine = 9999
no_mine = 0
n_mine = 10
width = 10
height = 10
def set_init(self, width=10, height=10, nMines=10, vis={}): # 初始化
self.map = []
for _ in range(height):
t_line = []
for _ in range(width):
t_line.append(self.no_mine)
self.map.append(t_line)
self.vis = vis
self.width = width
self.height = height
self.n_mine = nMines
self.remix()
self.restart()
def reset(self): # 重新设置,但是高度和炸弹的数目是不变的
self.map.clear()
self.vis.clear()
for _ in range(self.height):
t_line = []
for _ in range(self.width):
t_line.append(self.no_mine)
self.map.append(t_line)
self.remix()
self.restart() # 初始化标记
# 打乱布局重新随机编排
def remix(self): # 设置炸弹的位置,以及各种数字的序号
for y in range(self.height):
for x in range(self.width):
self.map[y][x] = self.no_mine
def add_mark(x, y):
# 如果不是雷的标记就+1
if self.map[y][x] + 1 < self.mine:
self.map[y][x] += 1
mine_count = 0
while mine_count < self.n_mine:
x = randint(0, self.width - 1)
y = randint(0, self.height - 1)
if self.map[y][x] != self.mine:
self.map[y][x] = self.mine
mine_count += 1
# 雷所在的位置的8个方位的数值+1
## 上下左右
if y - 1 >= 0: add_mark(x, y - 1)
if y + 1 < self.height: add_mark(x, y + 1)
if x - 1 >= 0: add_mark(x - 1, y)
if x + 1 < self.width: add_mark(x + 1, y)
## 四个角: 左上角、左下角、右上角、右下角
if x - 1 >= 0 and y - 1 >= 0: add_mark(x - 1, y - 1)
if x - 1 >= 0 and y + 1 < self.height: add_mark(x - 1, y + 1)
if x + 1 < self.width and y - 1 >= 0: add_mark(x + 1, y - 1)
if x + 1 < self.width and y + 1 < self.height: add_mark(x + 1, y + 1)
def restart(self):
for y in range(self.height):
for x in range(self.width):
print(self.map[y][x], end=" ")
print()
for i in range(self.width): # 初始化标记
tmp = {}
for j in range(self.height):
tmp[j] = 0
self.vis[i] = tmp
def Search(self, x, y): # 搜索
q = queue.Queue()
if self.map[y][x] == self.mine:
print("炸弹")
elif self.map[y][x] != 0:
self.vis[y][x] = 1
# self.buttons[x][y].setText(str(self.map[y][x]))
else:
q.put((x, y))
self.vis[y][x] = 1
while not q.empty():
top = q.get()
for i in range(8):
xx = top[0] + dx[i]
yy = top[1] + dy[i]
if 0 <= xx < self.width and 0 <= yy < self.height and self.vis[yy][xx] == 0:
if self.map[yy][xx] != 0:
self.vis[yy][xx] = 1
self.buttons[xx][yy].setText(str(self.map[yy][xx]))
else:
self.vis[yy][xx] = 1
q.put((xx, yy))
self.buttons[xx][yy].hide()
def show(self):
for i in range(self.width):
for j in range(self.height):
print(self.vis[i][j], end=" ")
print()
def __getitem__(self, key):
return self.map[key]
def __str__(self):
format_str = ""
for y in range(self.height):
format_str += str(self[y]) + "\n"
return format_str
__repr__ = __str__
def deal(x, y):
print(myWin.tmp.buttons[x][y].x, myWin.tmp.buttons[x][y].y)
if (myWin.tmp.map[y][x] == 0):
myWin.tmp.buttons[x][y].hide()
myWin.tmp.Search(x, y)
elif myWin.tmp.map[y][x] == 9999:
myWin.tmp.buttons[x][y].setText("*")
else:
myWin.tmp.buttons[x][y].setText(str(myWin.tmp.map[y][x]))
class MynextForm(QMainWindow, game):
def __init__(self, parent=None):
super(MynextForm, self).__init__(parent)
self.setupUi(self)
# self.pushButton.clicked.connect(self.showMsg)
self.r = 0
self.c = 0
class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
# self.pushButton.clicked.connect(self.showMsg)
self.linker.clicked.connect(self.read)
self.tmp = MynextForm()
self.r = 0
self.c = 0
def showMsg(self):
QMessageBox.information(self, "信息提示框", "OK,内置信号与自定义槽函数!")
def p(self):
print(123)
def read(self):
print(self.line_c.text())
print(self.line_r.text())
self.hide()
self.tmp.set(self.line_c.text(), self.line_r.text(), self.tmp)
self.tmp.show()
self.tmp.set_init(int(self.line_c.text()), int(self.line_r.text()), 100)
# res.Search(0, 0)
# self.game = game(self)
# self.game = game(self,)
# self.game.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())