pyqt5实现井字棋的示例代码

本文介绍的是利用pyqt5实现井字棋,下面话不多说了,来一起看看详细的介绍

作者:袁少泽

代码如下(示例):

import random
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
start=time.time()
class Ui_Form(object):
 def setupUi(self, Form):
 Form.setObjectName("Form")
 Form.resize(415, 346)
 self.label_3 = QtWidgets.QLabel(Form)
 self.label_3.setGeometry(QtCore.QRect(30, 290, 241, 20))
 self.label_3.setObjectName("label_3")
 self.pushButton_10 = QtWidgets.QPushButton(Form)
 self.pushButton_10.setGeometry(QtCore.QRect(210, 310, 75, 23))
 self.pushButton_10.setCheckable(False)
 self.pushButton_10.setDefault(False)
 self.pushButton_10.setObjectName("pushButton_10")
 self.label_2 = QtWidgets.QLabel(Form)
 self.label_2.setGeometry(QtCore.QRect(20, 260, 241, 20))
 self.label_2.setObjectName("label_2")
 self.label = QtWidgets.QLabel(Form)
 self.label.setGeometry(QtCore.QRect(290, 10, 111, 31))
 self.label.setObjectName("label")
 self.layoutWidget = QtWidgets.QWidget(Form)
 self.layoutWidget.setGeometry(QtCore.QRect(70, 40, 191, 191))
 self.layoutWidget.setObjectName("layoutWidget")
 self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
 self.gridLayout.setContentsMargins(0, 0, 0, 0)
 self.gridLayout.setObjectName("gridLayout")
 self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton.setEnabled(True)
 self.pushButton.setToolTip("")
 self.pushButton.setText("")
 self.pushButton.setObjectName("pushButton")
 self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
 self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_4.setText("")
 self.pushButton_4.setObjectName("pushButton_4")
 self.gridLayout.addWidget(self.pushButton_4, 0, 1, 1, 1)
 self.pushButton_7 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_7.setEnabled(True)
 self.pushButton_7.setText("")
 self.pushButton_7.setObjectName("pushButton_7")
 self.gridLayout.addWidget(self.pushButton_7, 0, 2, 1, 1)
 self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_2.setText("")
 self.pushButton_2.setObjectName("pushButton_2")
 self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
 self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_5.setText("")
 self.pushButton_5.setObjectName("pushButton_5")
 self.gridLayout.addWidget(self.pushButton_5, 1, 1, 1, 1)
 self.pushButton_8 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_8.setText("")
 self.pushButton_8.setObjectName("pushButton_8")
 self.gridLayout.addWidget(self.pushButton_8, 1, 2, 1, 1)
 self.pushButton_3 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_3.setText("")
 self.pushButton_3.setObjectName("pushButton_3")
 self.gridLayout.addWidget(self.pushButton_3, 2, 0, 1, 1)
 self.pushButton_6 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_6.setText("")
 self.pushButton_6.setObjectName("pushButton_6")
 self.gridLayout.addWidget(self.pushButton_6, 2, 1, 1, 1)
 self.pushButton_9 = QtWidgets.QPushButton(self.layoutWidget)
 self.pushButton_9.setText("")
 self.pushButton_9.setObjectName("pushButton_9")
 self.gridLayout.addWidget(self.pushButton_9, 2, 2, 1, 1)
 self.label_4 = QtWidgets.QLabel(Form)
 self.label_4.setGeometry(QtCore.QRect(70, 10, 131, 21))
 self.label_4.setObjectName("label_4")
 self.pushButton_11 = QtWidgets.QPushButton(Form)
 self.pushButton_11.setGeometry(QtCore.QRect(300, 310, 75, 23))
 self.pushButton_11.setObjectName("pushButton_11")
 self.retranslateUi(Form)
 self.pushButton_11.clicked.connect(Form.close)
 QtCore.QMetaObject.connectSlotsByName(Form)
 def retranslateUi(self, Form):
 _translate = QtCore.QCoreApplication.translate
 Form.setWindowTitle(_translate("Form", "Form"))
 self.label_3.setText(_translate("Form", "TextLabel"))
 self.pushButton_10.setText(_translate("Form", "重新开始"))
 self.label_2.setText(_translate("Form", "TextLabel"))
 self.label.setText(_translate("Form", "TextLabel"))
 self.label_4.setText(_translate("Form", "TextLabel"))
 self.pushButton_11.setText(_translate("Form", "退出"))
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
 MainWindow.setObjectName("MainWindow")
 MainWindow.resize(294, 167)
 self.centralwidget = QtWidgets.QWidget(MainWindow)
 self.centralwidget.setObjectName("centralwidget")
 self.label = QtWidgets.QLabel(self.centralwidget)
 self.label.setGeometry(QtCore.QRect(110, 20, 161, 31))
 self.label.setObjectName("label")
 self.pushButton = QtWidgets.QPushButton(self.centralwidget)
 self.pushButton.setGeometry(QtCore.QRect(50, 80, 75, 23))
 self.pushButton.setObjectName("pushButton")
 self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
 self.pushButton_2.setGeometry(QtCore.QRect(180, 80, 75, 23))
 self.pushButton_2.setObjectName("pushButton_2")
 self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
 self.pushButton_3.setGeometry(QtCore.QRect(270, 10, 21, 23))
 self.pushButton_3.setObjectName("pushButton_3")
 MainWindow.setCentralWidget(self.centralwidget)
 self.statusbar = QtWidgets.QStatusBar(MainWindow)
 self.statusbar.setObjectName("statusbar")
 MainWindow.setStatusBar(self.statusbar)

 self.retranslateUi(MainWindow)
 self.pushButton_3.clicked.connect(MainWindow.close)
 QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
 _translate = QtCore.QCoreApplication.translate
 MainWindow.setWindowTitle(_translate("MainWindow", "井字棋-模式选择"))
 self.label.setText(_translate("MainWindow", "模式选择"))
 self.pushButton.setText(_translate("MainWindow", "双人"))
 self.pushButton_2.setText(_translate("MainWindow", "单人"))
 self.pushButton_3.setText(_translate("MainWindow", "X"))
class QIPAN():
 def __init__(self):
 self.qi11 = ' '
 self.qi12 = ' '
 self.qi13 = ' '
 self.qi21 = ' '
 self.qi22 = ' '
 self.qi23 = ' '
 self.qi31 = ' '
 self.qi32 = ' '
 self.qi33 = ' '
qipan = QIPAN()
class danren(QWidget):
 def __init__(self):
 super(danren, self).__init__()
 self.ui = Ui_Form()
 self.ui.setupUi(self)
 self.setWindowTitle('单人模式')
 self.initUI()
 self.ui.label_4.setText("单人模式")
 self.time = 0
 self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)###

 def initUI(self):
 self.ui.pushButton.setText(qipan.qi11)
 self.ui.pushButton_2.setText(qipan.qi12)
 self.ui.pushButton_3.setText(qipan.qi13)
 self.ui.pushButton_4.setText(qipan.qi21)
 self.ui.pushButton_5.setText(qipan.qi22)
 self.ui.pushButton_6.setText(qipan.qi23)
 self.ui.pushButton_7.setText(qipan.qi31)
 self.ui.pushButton_8.setText(qipan.qi32)
 self.ui.pushButton_9.setText(qipan.qi33)
 self.ui.pushButton.setToolTip('pushButton/qi11')
 self.ui.pushButton_2.setToolTip('pushButton_2/qi12')
 self.ui.pushButton_3.setToolTip('pushButton_3/qi13')
 self.ui.pushButton_4.setToolTip('pushButton_4/qi21')
 self.ui.pushButton_5.setToolTip('pushButton_5/qi22')
 self.ui.pushButton_6.setToolTip('pushButton_6/qi23')
 self.ui.pushButton_7.setToolTip('pushButton_7/qi31')
 self.ui.pushButton_8.setToolTip('pushButton_8/qi32')
 self.ui.pushButton_9.setToolTip('pushButton_9/qi33')
 self.ui.label.setText('')
 self.ui.label_2.setText('当前为O玩家VS电脑')
 self.ui.label_3.setText('')
 self.timer = QTimer()
 self.timer.timeout.connect(self.showtime)
 self.startTimer()
 self.CONNECT()
 def CONNECT(self):
 self.ui.pushButton.clicked.connect(self.change)
 self.ui.pushButton_2.clicked.connect(self.change2)
 self.ui.pushButton_3.clicked.connect(self.change3)
 self.ui.pushButton_4.clicked.connect(self.change4)
 self.ui.pushButton_5.clicked.connect(self.change5)
 self.ui.pushButton_6.clicked.connect(self.change6)
 self.ui.pushButton_7.clicked.connect(self.change7)
 self.ui.pushButton_8.clicked.connect(self.change8)
 self.ui.pushButton_9.clicked.connect(self.change9)
 self.ui.pushButton.clicked.connect(self.PANDUAN)
 self.ui.pushButton_2.clicked.connect(self.PANDUAN)
 self.ui.pushButton_3.clicked.connect(self.PANDUAN)
 self.ui.pushButton_4.clicked.connect(self.PANDUAN)
 self.ui.pushButton_5.clicked.connect(self.PANDUAN)
 self.ui.pushButton_6.clicked.connect(self.PANDUAN)
 self.ui.pushButton_7.clicked.connect(self.PANDUAN)
 self.ui.pushButton_8.clicked.connect(self.PANDUAN)
 self.ui.pushButton_9.clicked.connect(self.PANDUAN)
 self.ui.pushButton_10.clicked.connect(self.HUANYUAN)
 self.ui.pushButton_11.clicked.connect(self.HUANYUAN)
 def change(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton.setText(a)
 else:
  a = "O"
  self.ui.pushButton.setText(a)
 self.ui.pushButton.setEnabled(False)
 if self.time < 9:
  self.suiji()
 def change2(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_2.setText(a)
 else:
  a = "O"
  self.ui.pushButton_2.setText(a)
 self.ui.pushButton_2.setEnabled(False)
 if self.time < 9:
  self.suiji()
 def change3(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_3.setText(a)
 else:
  a = "O"
  self.ui.pushButton_3.setText(a)
 self.ui.pushButton_3.setEnabled(False)
 if self.time<9:
  self.suiji()
 def change4(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_4.setText(a)
 else:
  a = "O"
  self.ui.pushButton_4.setText(a)
 self.ui.pushButton_4.setEnabled(False)
 if self.time < 9:
  self.suiji()
 def change5(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_5.setText(a)
 else:
  a = "O"
  self.ui.pushButton_5.setText(a)
 self.ui.pushButton_5.setEnabled(False)
 if self.time < 9:
  self.suiji()
 def change6(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_6.setText(a)
 else:
  a = "O"
  self.ui.pushButton_6.setText(a)
 self.ui.pushButton_6.setEnabled(False)
 if self.time < 9:
  self.suiji()
 def change7(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_7.setText(a)
 else:
  a = "O"
  self.ui.pushButton_7.setText(a)
 self.ui.pushButton_7.setEnabled(False)
 if self.time<9:
  self.suiji()
 def change8(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_8.setText(a)
 else:
  a = "O"
  self.ui.pushButton_8.setText(a)
 self.ui.pushButton_8.setEnabled(False)
 if self.time<9:
  self.suiji()
 def change9(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton_9.setText(a)
 else:
  a = "O"
  self.ui.pushButton_9.setText(a)
 self.ui.pushButton_9.setEnabled(False)
 if self.time<9:
  self.suiji()
 def showtime(self):
 end = time.time()
 self.ui.label.setText(f'游戏开始{int(end - start)}秒')
 def PANDUAN(self):
 if self.time<9:
  if (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_7.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_6.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'X' and self.ui.pushButton_6.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X'):
  self.ui.label_3.setText("电脑获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time=0
  elif (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_2.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_7.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_9.text() == 'O') \
   or (
   self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_6.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'O' and self.ui.pushButton_6.text() == 'O' and self.ui.pushButton_9.text() == 'O'):
  self.ui.label_3.setText("O玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time=0
  else:
  self.ui.label_3.setText("暂未分出胜负")
 elif self.time==9:
  if (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_7.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_6.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'X' and self.ui.pushButton_6.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X'):
  self.ui.label_3.setText("X玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time=0
  elif (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_2.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_7.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_9.text() == 'O') \
   or (
   self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_6.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'O' and self.ui.pushButton_6.text() == 'O' and self.ui.pushButton_9.text() == 'O'):
  self.ui.label_3.setText("O玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time=0
  else:
  self.ui.label_3.setText("平局")
  self.time=0
 def HUANYUAN(self):
 self.ui.label_2.setText('当前为O玩家回合')
 self.ui.pushButton.setText(qipan.qi11)
 self.ui.pushButton_2.setText(qipan.qi12)
 self.ui.pushButton_3.setText(qipan.qi13)
 self.ui.pushButton_4.setText(qipan.qi21)
 self.ui.pushButton_5.setText(qipan.qi22)
 self.ui.pushButton_6.setText(qipan.qi23)
 self.ui.pushButton_7.setText(qipan.qi31)
 self.ui.pushButton_8.setText(qipan.qi32)
 self.ui.pushButton_9.setText(qipan.qi33)
 self.ui.pushButton.setEnabled(True)
 self.ui.pushButton_2.setEnabled(True)
 self.ui.pushButton_3.setEnabled(True)
 self.ui.pushButton_4.setEnabled(True)
 self.ui.pushButton_5.setEnabled(True)
 self.ui.pushButton_6.setEnabled(True)
 self.ui.pushButton_7.setEnabled(True)
 self.ui.pushButton_8.setEnabled(True)
 self.ui.pushButton_9.setEnabled(True)
 self.time=0
 def suiji(self):
 Truelabel=[]
 labels=[self.ui.pushButton,self.ui.pushButton_2,self.ui.pushButton_3,
  self.ui.pushButton_4,self.ui.pushButton_5,self.ui.pushButton_6,
  self.ui.pushButton_7,self.ui.pushButton_8,self.ui.pushButton_9]
 for label in labels:
  if label.isEnabled()==True:
  Truelabel.append(label)
 luckylabel=random.choice(Truelabel)
 if luckylabel==self.ui.pushButton:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  self.ui.pushButton.setText(a)
  else:
  a = "O"
  self.ui.pushButton.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton.setEnabled(False)
  self.PANDUAN()
 elif luckylabel==self.ui.pushButton_2:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_2.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_2.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_2.setEnabled(False)
  self.PANDUAN()
 elif luckylabel == self.ui.pushButton_3:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_3.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_3.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_3.setEnabled(False)
  self.PANDUAN()
 elif luckylabel==self.ui.pushButton_4:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_4.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_4.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_4.setEnabled(False)
  self.PANDUAN()
 elif luckylabel==self.ui.pushButton_5:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = "O"
  self.ui.pushButton_5.setText(a)
  else:
  a = "O"
  b = "X"
  self.ui.pushButton_5.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_5.setEnabled(False)
  self.PANDUAN()
 elif luckylabel==self.ui.pushButton_6:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_6.setText(a)
  else:
  a = "O"
  b = "X"
  self.ui.pushButton_6.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_6.setEnabled(False)
  self.PANDUAN()
 elif luckylabel==self.ui.pushButton_7:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_7.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_7.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_7.setEnabled(False)
  self.PANDUAN()
 elif luckylabel == self.ui.pushButton_8:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_8.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_8.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_8.setEnabled(False)
  self.PANDUAN()
 elif luckylabel == self.ui.pushButton_9:
  self.time = self.time + 1
  if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_9.setText(a)
  else:
  a = "O"
  b = 'X'
  self.ui.pushButton_9.setText(a)
  self.ui.label_2.setText('当前为o玩家VS电脑')
  self.ui.pushButton_9.setEnabled(False)
  self.PANDUAN()
 def startTimer(self):
 self.timer.start(1000)
class shuangren(QWidget):
 def __init__(self):
 super(shuangren, self).__init__()
 self.ui = Ui_Form()
 self.ui.setupUi(self)
 self.setWindowTitle('双人模式')
 self.ui.label_4.setText("双人模式")
 self.ui.pushButton_11.clicked.connect(self.HUANYUAN)
 self.initUI()
 self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
 def initUI(self):
 self.ui.pushButton.setText(qipan.qi11)
 self.ui.pushButton_2.setText(qipan.qi12)
 self.ui.pushButton_3.setText(qipan.qi13)
 self.ui.pushButton_4.setText(qipan.qi21)
 self.ui.pushButton_5.setText(qipan.qi22)
 self.ui.pushButton_6.setText(qipan.qi23)
 self.ui.pushButton_7.setText(qipan.qi31)
 self.ui.pushButton_8.setText(qipan.qi32)
 self.ui.pushButton_9.setText(qipan.qi33)
 self.ui.pushButton.setToolTip('pushButton/qi11')
 self.ui.pushButton_2.setToolTip('pushButton_2/qi12')
 self.ui.pushButton_3.setToolTip('pushButton_3/qi13')
 self.ui.pushButton_4.setToolTip('pushButton_4/qi21')
 self.ui.pushButton_5.setToolTip('pushButton_5/qi22')
 self.ui.pushButton_6.setToolTip('pushButton_6/qi23')
 self.ui.pushButton_7.setToolTip('pushButton_7/qi31')
 self.ui.pushButton_8.setToolTip('pushButton_8/qi32')
 self.ui.pushButton_9.setToolTip('pushButton_9/qi33')
 self.ui.label.setText('')
 self.ui.label_2.setText('当前为O玩家回合')
 self.ui.label_3.setText('')
 self.timer = QTimer()
 self.timer.timeout.connect(self.showtime)
 self.startTimer()
 self.bangding()
 def bangding(self):
 self.ui.pushButton.clicked.connect(self.change)
 self.ui.pushButton_2.clicked.connect(self.change2)
 self.ui.pushButton_3.clicked.connect(self.change3)
 self.ui.pushButton_4.clicked.connect(self.change4)
 self.ui.pushButton_5.clicked.connect(self.change5)
 self.ui.pushButton_6.clicked.connect(self.change6)
 self.ui.pushButton_7.clicked.connect(self.change7)
 self.ui.pushButton_8.clicked.connect(self.change8)
 self.ui.pushButton_9.clicked.connect(self.change9)
 self.ui.pushButton.clicked.connect(self.PANDUAN)
 self.ui.pushButton_2.clicked.connect(self.PANDUAN)
 self.ui.pushButton_3.clicked.connect(self.PANDUAN)
 self.ui.pushButton_4.clicked.connect(self.PANDUAN)
 self.ui.pushButton_5.clicked.connect(self.PANDUAN)
 self.ui.pushButton_6.clicked.connect(self.PANDUAN)
 self.ui.pushButton_7.clicked.connect(self.PANDUAN)
 self.ui.pushButton_8.clicked.connect(self.PANDUAN)
 self.ui.pushButton_9.clicked.connect(self.PANDUAN)
 self.ui.pushButton_10.clicked.connect(self.HUANYUAN)
 self.time = 0
 def change(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton.setEnabled(False)
 def change2(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_2.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_2.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_2.setEnabled(False)
 def change3(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_3.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_3.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_3.setEnabled(False)
 def change4(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_4.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_4.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_4.setEnabled(False)
 def change5(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = "O"
  self.ui.pushButton_5.setText(a)
 else:
  a = "O"
  b = "X"
  self.ui.pushButton_5.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_5.setEnabled(False)
 def change6(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_6.setText(a)
 else:
  a = "O"
  b = "X"
  self.ui.pushButton_6.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_6.setEnabled(False)
 def change7(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_7.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_7.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_7.setEnabled(False)
 def change8(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_8.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_8.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_8.setEnabled(False)
 def change9(self):
 self.time = self.time + 1
 if (self.time + 2) % 2 == 0:
  a = "X"
  b = 'O'
  self.ui.pushButton_9.setText(a)
 else:
  a = "O"
  b = 'X'
  self.ui.pushButton_9.setText(a)
 self.ui.label_2.setText(f'当前为{b}玩家回合')
 self.ui.pushButton_9.setEnabled(False)
 def showtime(self):
 end = time.time()
 self.ui.label.setText(f'游戏开始{int(end - start)}秒')

 def PANDUAN(self):
 if self.time < 9:
  if (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_7.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_6.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'X' and self.ui.pushButton_6.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X'):
  self.ui.label_3.setText("X玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time = 0
  elif (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_2.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_7.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_9.text() == 'O') \
   or (
   self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_6.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'O' and self.ui.pushButton_6.text() == 'O' and self.ui.pushButton_9.text() == 'O'):
  self.ui.label_3.setText("O玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time = 0
  else:
  self.ui.label_3.setText("暂未分出胜负")
 elif self.time == 9:
  if (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_7.text() == 'X') \
   or (
   self.ui.pushButton.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_4.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_6.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_3.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'X' and self.ui.pushButton_6.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X'):
  self.ui.label_3.setText("X玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time = 0
  elif (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_2.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_7.text() == 'O') \
   or (
   self.ui.pushButton.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_9.text() == 'O') \
   or (
   self.ui.pushButton_4.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_6.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'O' and self.ui.pushButton_5.text() == 'O' and self.ui.pushButton_3.text() == 'O') \
   or (
   self.ui.pushButton_7.text() == 'X' and self.ui.pushButton_8.text() == 'X' and self.ui.pushButton_9.text() == 'X') \
   or (
   self.ui.pushButton_2.text() == 'X' and self.ui.pushButton_5.text() == 'X' and self.ui.pushButton_8.text() == 'X') \
   or (
   self.ui.pushButton_3.text() == 'O' and self.ui.pushButton_6.text() == 'O' and self.ui.pushButton_9.text() == 'O'):
  self.ui.label_3.setText("O玩家获胜")
  self.ui.label_2.setText("结束")
  self.ui.pushButton.setEnabled(False)
  self.ui.pushButton_2.setEnabled(False)
  self.ui.pushButton_3.setEnabled(False)
  self.ui.pushButton_4.setEnabled(False)
  self.ui.pushButton_5.setEnabled(False)
  self.ui.pushButton_6.setEnabled(False)
  self.ui.pushButton_7.setEnabled(False)
  self.ui.pushButton_8.setEnabled(False)
  self.ui.pushButton_9.setEnabled(False)
  self.time = 0
  else:
  self.ui.label_3.setText("平局")
  self.time = 0
 def HUANYUAN(self):
 self.ui.pushButton.setText(qipan.qi11)
 self.ui.pushButton_2.setText(qipan.qi12)
 self.ui.pushButton_3.setText(qipan.qi13)
 self.ui.pushButton_4.setText(qipan.qi21)
 self.ui.pushButton_5.setText(qipan.qi22)
 self.ui.pushButton_6.setText(qipan.qi23)
 self.ui.pushButton_7.setText(qipan.qi31)
 self.ui.pushButton_8.setText(qipan.qi32)
 self.ui.pushButton_9.setText(qipan.qi33)
 self.ui.pushButton.setEnabled(True)
 self.ui.pushButton_2.setEnabled(True)
 self.ui.pushButton_3.setEnabled(True)
 self.ui.pushButton_4.setEnabled(True)
 self.ui.pushButton_5.setEnabled(True)
 self.ui.pushButton_6.setEnabled(True)
 self.ui.pushButton_7.setEnabled(True)
 self.ui.pushButton_8.setEnabled(True)
 self.ui.pushButton_9.setEnabled(True)
 self.ui.label_3.setText("")
 def startTimer(self):
 self.timer.start(1000)
class MODLECHOSE(QMainWindow):
 def __init__(self):
 super(MODLECHOSE,self).__init__()
 self.ui=Ui_MainWindow()
 self.ui.setupUi(self)
 self.ui.pushButton.clicked.connect(self.caoshuang)
 self.ui.pushButton_2.clicked.connect(self.caodan)
 self.danwin = danren()
 self.shuangwin = shuangren()
 self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)#######

 def caodan(self):
  self.danwin.show()
 def caoshuang(self):
  self.shuangwin.show()
if __name__ == '__main__':
 app = QApplication(sys.argv)
 main = MODLECHOSE()
 main.show()
 sys.exit(app.exec_())

到此这篇关于pyqt5实现井字棋的文章就介绍到这了,更多相关pyqt5井字棋内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • PyQt5实现五子棋游戏(人机对弈)

    这篇博客主要是为了学习Python和PyQt,因为对棋类游戏比较热衷,所以从规则较简单的五子棋入手,利用PyQt5实现图形界面,做一个可以进行人机对弈的脚本,最后打包成应用程序.AI的算法打算用神经网络来完成,正在苦学TensorFlow中. 本来我以为五子棋规则很简单,不就像小学时候玩的那样,五个棋子连在一起就赢了嘛,但是后来发现事情并没有那么简单,现在的五子棋有禁手这个规则 ,"三三禁手" ."四四禁手"."长连禁手"等等,都是为了限制现行一

  • pyqt5实现井字棋的示例代码

    本文介绍的是利用pyqt5实现井字棋,下面话不多说了,来一起看看详细的介绍 作者:袁少泽 代码如下(示例): import random import sys import time from PyQt5.QtCore import * from PyQt5.QtWidgets import * start=time.time() class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") For

  • Java实现简单井字棋小游戏代码实例

    Java第一次实验,老师让做一个井字棋,电脑随机下棋. 然后就想能不能聪明一点,可以判断出走哪一步棋:然后只能做到不会输,还是不够聪明,只能呆板地堵住用户,smartRobot的第三个判断逻辑找不到最佳位置,赢得概率比较小:而且我没事干时,想玩玩这个小游戏找找成就感,但每次都会赢了机器人,所以删删改改了四五次,最后才成. 可以选择谁先开始,但startGame里的代码更加冗余了.看着就很乱,但没想到好的办法. smartRobot里的代码全部重写了,比原来更聪明一点了:下在四个角的位置时,能优先

  • JavaScript 井字棋人工智能实现代码

    /* Code Written by Cory Fogliani (Email: cory@ijustdontcare.com) Testers: Cory Fogliani, Chris Gordon Featured on JavaScript Kit (http://javascriptkit.com) For this and over 400+ free scripts, visit http://javascriptkit.com */ //if IE4/NS6, apply sty

  • python入门游戏之井字棋实例代码

    目录 井字棋简介 实例代码 总结 井字棋简介 井字棋又称三子棋,英文名为Tic Tac Toe.具体玩法为在一个3x3的棋盘上,一个玩家用X做棋子,另一个玩家用O做棋子,谁先在棋盘上的一行.一列或对角线上画满三个棋子,即可获胜,如果棋盘下满无人胜出,即为平局. 实例代码 #要用的函数,可以在主程序用到时再看 def panduan1(a,b):#判断是否获胜,返回1则证明此颜色的棋获胜 x=0 if x==0: for i in range(3):#检索二维数组的第i行 q=0;p=0 for

  • C语言实现井字棋(三子棋)

    本文实例为大家分享了C语言实现井字棋的具体代码,供大家参考,具体内容如下 一.实现思路 1.初始化数组 三子棋是九宫格的格式,所以用二维数组接收数据.用'O'代表电脑下的子,'X'代表玩家下的子.未下子的时候初始化 ' '(space).则二维数组为"char"类型,大小为char board[3][3]. 2.打印棋盘 打印出井字的棋盘,同时为了将数据显示在每格的中间,用空格隔开(" %c |")的格式设置棋盘"|"用来形成竖,接着打印&quo

  • python实现简单的井字棋

    本文实例为大家分享了python实现简单的井字棋的具体代码,供大家参考,具体内容如下 使用python实现井字棋游戏,没有具体算法,只是用随机下棋简单实现: import random board = [['+','+','+'],['+','+','+'],['+','+','+']] def ma(board): if isempty(board): a = random.randint(0, 2) b = random.randint(0, 2) if board[a][b] != 'X'

  • C语言实现井字棋(三子棋)

    本文实例为大家分享了C语言实现井字棋的具体代码,供大家参考,具体内容如下 一.实现思路 1.初始化数组 三子棋是九宫格的格式,所以用二维数组接收数据.用'O'代表电脑下的子,'X'代表玩家下的子.未下子的时候初始化 ' '(space).则二维数组为"char"类型,大小为char board[3][3]. 2.打印棋盘 打印出井字的棋盘,同时为了将数据显示在每格的中间,用空格隔开(" %c |")的格式设置棋盘"|"用来形成竖,接着打印&quo

  • python实现人机对战的井字棋游戏

    本文实例为大家分享了python实现人机对战井字棋的具体代码,供大家参考,具体内容如下 游戏简介:在九宫格内进行,如果一方抢先于另一方向(横.竖.斜)连成3子,则获得胜利.游戏中输入方格位置代号的形式如下: 设计前的思路: 游戏中,board棋盘存储玩家.计算机的落子信息,未落子处未EMPTY.由于人机对战,需要实现计算机智能性,下面是为这个计算机机器人设计的简单策略:如果有一步棋可以让计算机机器人在本轮获胜,那就选那一步走.否则,如果有一步棋可以让玩家在本轮获胜,那就选那一步走.否则,计算机机

  • C++控制台实现简单人机对弈井字棋

    本文实例为大家分享了C++实现简单人机对弈井字棋的具体代码,供大家参考,具体内容如下 main.cpp #include"TicTacToe.h" int main()  {     Game game;     game.getWinner();     return 0;      } TicTacToe.h #pragma once #include<iostream> using namespace std; #include<array> #inclu

  • React中井字棋游戏的实现示例

    目录 需求分析 实现分析 涉及的组件 涉及的状态 编码实现 项目初始化 定义各个组件的props/state Square组件props Board组件props Game组件state 各组件代码 Square Board Game 最近开始接触React,我认为读官方文档是最快上手一门技术的途径了,恰好React的官方文档中有这样一个井字棋游戏的demo,学习完后能够快速上手React,这是我学习该demo的总结 需求分析 首先看看这个游戏都有哪些需求吧 游戏玩家:X和O,每次落棋后需要切换

随机推荐