最近看到一个小游戏“数字华容道”。
闲来没事就自己实现了一个(存在无解的情况)
实现的代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'num.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5.QtWidgetsimport *
from PyQt5import QtCore, QtGui, QtWidgets
import sys
import random
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.MainWindow = MainWindow
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(310, 100, 131, 121))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.pushButton_4 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 1, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.pushButton_1 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_1.setObjectName("pushButton_1")
self.gridLayout.addWidget(self.pushButton_1, 0, 0, 1, 1)
self.pushButton_7 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_7.setObjectName("pushButton_7")
self.gridLayout.addWidget(self.pushButton_7, 2, 0, 1, 1)
self.pushButton_8 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_8.setObjectName("pushButton_8")
self.gridLayout.addWidget(self.pushButton_8, 2, 1, 1, 1)
self.pushButton_9 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_9.setObjectName("pushButton_9")
self.gridLayout.addWidget(self.pushButton_9, 2, 2, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "4"))
self.pushButton_3.setText(_translate("MainWindow", "3"))
self.pushButton_2.setText(_translate("MainWindow", "2"))
self.pushButton_5.setText(_translate("MainWindow", "5"))
self.pushButton_6.setText(_translate("MainWindow", "6"))
self.pushButton_1.setText(_translate("MainWindow", "1"))
self.pushButton_7.setText(_translate("MainWindow", "7"))
self.pushButton_8.setText(_translate("MainWindow", "8"))
self.pushButton_1.clicked.connect(self.button_click)
self.pushButton_2.clicked.connect(self.button_click)
self.pushButton_3.clicked.connect(self.button_click)
self.pushButton_4.clicked.connect(self.button_click)
self.pushButton_5.clicked.connect(self.button_click)
self.pushButton_6.clicked.connect(self.button_click)
self.pushButton_7.clicked.connect(self.button_click)
self.pushButton_8.clicked.connect(self.button_click)
self.pushButton_9.clicked.connect(self.button_click)
self.pushButtons = [self.pushButton_1,self.pushButton_2,self.pushButton_3,self.pushButton_4,self.pushButton_5,self.pushButton_6,self.pushButton_7,self.pushButton_8,self.pushButton_9]
## 先创个 list
num_list = ['', '1', '2', '3','4','5','6','7','8']
num_list1 = (random.sample(num_list, 9))
for index,pushButtonin enumerate(self.pushButtons):
pushButton.setText(_translate("MainWindow",num_list1[index]))
def button_click(self):
sender =self.gridLayoutWidget.sender()
position = sender.objectName()
position_num =int(position.split('_')[1])
try:
if self.pushButtons[position_num +1 -1].text() =='':
self.pushButtons[position_num +1 -1].setText(sender.text())
sender.setText('')
self.check()
return
except:
pass
try:
if self.pushButtons[position_num -3 -1].text() =='':
if position_num -3 -1 <0:
return
self.pushButtons[position_num -3 -1].setText(sender.text())
sender.setText('')
self.check()
return
except:
pass
try:
if self.pushButtons[position_num -1 -1].text() =='':
if position_num -1 -1 <0:
return
self.pushButtons[position_num -1 -1].setText(sender.text())
sender.setText('')
self.check()
return
except:
pass
try:
if self.pushButtons[position_num +3 -1].text() =='':
self.pushButtons[position_num +3 -1].setText(sender.text())
sender.setText('')
self.check()
return
except:
pass
def check(self):
for index,pushButtonin enumerate(self.pushButtons):
if index ==8:
QMessageBox.about(self.MainWindow, "提示", "恭喜成功!")
if pushButton.text() !=str(index+1):
return
if __name__ =='__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
一个小时随便写的,存在bug。
欢迎交流。