Beispiel #1
0
class DoublePlayer(BasePlayer):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.is_black=True
        self.chessboard=chessboard=[[None for i in range(0,19)] for j in range(0,19)]
        self.is_over=True
        self.restart_btn.clicked.connect(self.restart)
        self.win_label=None
        self.huiqi_btn.clicked.connect(self.huiqi)
        self.lose_btn.clicked.connect(self.lose)
        self.history=[]
    def restart(self):
        self.is_over=False
        if self.win_label is not None:
            self.win_label.close()
        for i in range(19):
            for j in range(19):
                if self.chessboard[j][i] is not None:
                    self.chessboard[j][i].close()
                    self.chessboard[j][i]=None
    def huiqi(self):

        if self.is_over:
            return
        a = self.history.pop()
        xx = a[0]
        yy = a[1]
        if self.chessboard[xx][yy] is not None:
            self.chessboard[xx][yy].close()
            self.chessboard[xx][yy] = None

            self.is_black = not self.is_black

    def lose(self):
        if  self.is_over:
            return
        xx = self.history[::-1][0][0]
        yy = self.history[::-1][0][1]
        self.win_label = QLabel(self)
        if self.chessboard[xx][yy].color == 'b':
            pic = QPixmap("photos/黑棋胜利.png")
        else:
            pic = QPixmap("photos/白棋胜利.png")
        self.win_label.setPixmap(pic)
        self.win_label.move(100, 100)
        self.win_label.show()
        self.is_over = True

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        if self.is_over:
            return
        if a0.x() <40 or a0.x()>600:
            return
        if a0.y()<40 or a0.y()>600:
            return
        if self.is_black:
            self.chess=Chess(color='b',parent=self)
        else:
            self.chess = Chess(color='w', parent=self)
        #self.is_black=not self.is_black
        if (a0.x()-50)%30<=15:
            x=(a0.x()-50)//30*30+50
        else:
            x=((a0.x()-50)//30+1)*30+50
        if (a0.y() - 50) % 30 <= 15:
            y = (a0.y()-50) // 30 * 30 + 50
        else:
            y = ((a0.y() - 50) // 30 + 1) * 30 + 50
        xx =(x-50)//30
        yy =(y-50)//30


        if self.chessboard[xx][yy] is not None:
            return
        self.chessboard[xx][yy]=self.chess
        self.history.append([xx, yy])

        x=x-self.chess.width()/2
        y=y-self.chess.height()/2
        self.chess.move(x,y)
        self.chess.show()
        self.is_black = not self.is_black
        color = is_win(self.chessboard)

        if color  is False:
            return
        else:
            #QMessageBox.information(self,"消息","{}棋胜利".format(color))
            self.win_label =QLabel(self)
            if color=='b':
                pic =QPixmap("photos/黑棋胜利.png")
            else:
                pic = QPixmap("photos/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100,100)
            self.win_label.show()
            self.is_over=True
Beispiel #2
0
class NetworkPlayer(BasePlayer):
    dataSignal = pyqtSignal(dict)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        self.tcp_socket = None
        self.chessboard = [[None for i in range(0, 19)] for j in range(0, 19)]
        self.history = []
        self.dataSignal.connect(self.deal_data)
        self.restart_btn.clicked.connect(self.restart)
        self.lose_btn.clicked.connect(self.lose)
        self.huiqi_btn.clicked.connect(self.huiqi)
        self.is_over = True
        self.is_connect = True
        self.my_trun = True
        self.win_label = None

    def setup_ui(self):
        super().setup_ui()
        self.state_lable = QLabel("游戏状态", self)
        self.state_lable.move(660, 170)
        self.setWindowTitle('等待链接')
        self.cuicu_btn = TDPushButton("photos/催促按钮_normal.png",
                                      "photos/催促按钮_hover.png",
                                      "photos/催促按钮_press.png", self)
        self.cuicu_btn.show()
        self.cuicu_btn.move(640, 460)
        self.cuicu_btn.clicked.connect(self.sound)

    def sound(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "sound"}) + "END").encode())
        self.sound1()

    def sound1(self):
        sound = pygame.mixer.Sound("photos/cuicu.wav")
        sound.set_volume(1)
        sound.play()

    def deal_data(self, data):
        print(data)
        if data["msg"] == "name":
            name = data['data']
            title = '与' + name + '联机对战中'

            self.setWindowTitle(title)
            self.state_lable = QLabel("连接成功", self)
            self.state_lable.show()
            self.state_lable.move(660, 200)

        if data["msg"] == "pos":  #收到落子位置的代码
            pos = data['data']
            xx = pos[0]
            yy = pos[1]
            colors = pos[2]
            self.chess = Chess(color=colors, parent=self)
            self.is_black = not self.is_black
            self.my_trun = not self.my_trun

            if self.chessboard[xx][yy] is not None:
                return
            self.chessboard[xx][yy] = self.chess

            self.history.append((xx, yy))
            x = xx * 30 + 50 - 15
            y = yy * 30 + 50 - 15
            self.chess.move(x, y)
            self.state_text.setText("己方下棋")
            self.chess.show()
            sound = pygame.mixer.Sound("photos/luozisheng.wav")
            sound.set_volume(1)
            sound.play()
            color = is_win(self.chessboard)
            if color is False:
                return
            else:
                self.win_label = QLabel(self)
                if color == 'b':
                    pic = QPixmap("photos/黑棋胜利.png")
                else:
                    pic = QPixmap("photos/白棋胜利.png")
                self.win_label.setPixmap(pic)
                self.win_label.move(100, 100)
                self.win_label.show()
                self.is_over = True

        if data['msg'] == "error":
            QMessageBox.information(self, "消息", "对方已退出游戏,联机异常,即将返回")
            self.backSignal.emit()
            self.close()
        if data['msg'] == "restart":
            question = QMessageBox.question(self, "消息", "对方请求重新开始,是否同意",
                                            QMessageBox.Yes | QMessageBox.No)
            if question == QMessageBox.Yes:
                self.restartyes()
                self.restart1()
                self.state_text.setText("己方下棋")
            if question == QMessageBox.No:
                self.restartno()
        if data['msg'] == "restartyes":
            QMessageBox.information(self, "消息", "对方已同意")
            self.restart1()
            self.state_text.setText("对方下棋")
        if data['msg'] == "restartno":
            QMessageBox.information(self, "消息", "对方拒绝重新开始")
            #self.restartno()
        if data["msg"] == "lose":
            QMessageBox.information(self, "消息", "对方认输")
            self.lose1()
        if data['msg'] == "huiqi":
            hhuiqi = QMessageBox.question(self, "消息", "请求悔棋",
                                          QMessageBox.Yes | QMessageBox.No)
            if hhuiqi == QMessageBox.Yes:
                self.huiqiyes()
                self.huiqi1()
            if hhuiqi == QMessageBox.No:
                self.huiqino()
        if data["msg"] == "huiqiyes":
            QMessageBox.information(self, "消息", "OK")
            self.huiqi1()
        if data["msg"] == "huiqino":
            QMessageBox.information(self, "消息", "对方不同意")
        if data["msg"] == "sound":
            self.sound1()

    def recv_data(self, sock):
        #收到数据
        print("recv_data")
        while self.is_connect:
            try:
                r_data = recv_sockdata(sock)
            except ConnectionResetError as e:
                print(e)
                data = {"msg": "error", "data": 'disconnect'}
                self.dataSignal.emit(data)
                break
            except ConnectionAbortedError:
                pass
            data = json.loads(r_data)
            self.dataSignal.emit(data)

    def restart(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "restart"}) + "END").encode())

    def restart1(self):
        self.is_over = False
        if self.win_label is not None:
            self.win_label.close()
        for i in range(19):
            for j in range(19):
                if self.chessboard[j][i] is not None:
                    self.chessboard[j][i].close()
                    self.chessboard[j][i] = None

    def restartyes(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "restartyes"}) + "END").encode())

    def restartno(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "restartno"}) + "END").encode())

    def lose(self):
        if self.is_over:
            return

        self.tcp_socket.sendall((json.dumps({"msg": "lose"}) + "END").encode())
        self.lose1()

    def lose1(self):
        if self.is_over:
            return
        xx = self.history[::-1][0][0]
        yy = self.history[::-1][0][1]
        self.win_label = QLabel(self)
        if self.chessboard[xx][yy].color == 'b':
            pic = QPixmap("photos/黑棋胜利.png")
        else:
            pic = QPixmap("photos/白棋胜利.png")
        self.win_label.setPixmap(pic)
        self.win_label.move(100, 100)
        self.win_label.show()
        self.is_over = True

    def huiqi(self):
        if self.is_over:
            return
        self.tcp_socket.sendall(
            (json.dumps({"msg": "huiqi"}) + "END").encode())

    def huiqi1(self):
        if self.is_over:
            return
        a = self.history.pop()
        xx = a[0]
        yy = a[1]
        if self.chessboard[xx][yy] is not None:
            self.chessboard[xx][yy].close()
            self.chessboard[xx][yy] = None
            self.is_black = not self.is_black
            self.my_trun = not self.my_trun

    def huiqiyes(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "huiqiyes"}) + "END").encode())

    def huiqino(self):
        self.tcp_socket.sendall(
            (json.dumps({"msg": "huiqino"}) + "END").encode())

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        if self.is_over:
            return
        if not self.my_trun:
            return

        if a0.x() < 40 or a0.x() > 600:
            return
        if a0.y() < 40 or a0.y() > 600:
            return
            # 通过标识,决定棋子的颜色

        if self.is_black:
            self.chess = Chess(color='b', parent=self)
        else:
            self.chess = Chess('w', self)
            # 将棋子定位到准确的坐标点
        if (a0.x() - 50) % 30 <= 15:
            x = (a0.x() - 50) // 30 * 30 + 50
        else:
            x = ((a0.x() - 50) // 30 + 1) * 30 + 50

        if (a0.y() - 50) % 30 <= 15:
            y = (a0.y() - 50) // 30 * 30 + 50
        else:
            y = ((a0.y() - 50) // 30 + 1) * 30 + 50
            # 在棋盘数组中,保存棋子对象
        xx = (x - 50) // 30
        yy = (y - 50) // 30
        # 如果此处已经有棋子,点击失效
        if self.chessboard[xx][yy] is not None:
            return

        self.chessboard[xx][yy] = self.chess
        self.history.append([xx, yy])

        x = x - self.chess.width() / 2
        y = y - self.chess.height() / 2
        self.chess.move(x, y)
        self.state_text.setText("对方下棋")
        self.chess.show()
        sound = pygame.mixer.Sound("photos/luozisheng.wav")
        sound.set_volume(1)
        sound.play()

        #落子后发送棋子的位置
        if self.is_black:
            pos_data = {"msg": "pos", "data": (xx, yy, "b")}
            self.tcp_socket.sendall((json.dumps(pos_data) + "END").encode())
        else:
            pos_data = {"msg": "pos", "data": (xx, yy, "w")}
            self.tcp_socket.sendall((json.dumps(pos_data) + "END").encode())
        self.is_black = not self.is_black
        self.my_trun = not self.my_trun
        color = is_win(self.chessboard)
        if color is False:
            return
        else:
            self.win_label = QLabel(self)
            if color == 'b':
                pic = QPixmap("photos/黑棋胜利.png")
            else:
                pic = QPixmap("photos/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True

    def closeEvent(self, a0: QtGui.QCloseEvent):
        self.is_connect = False
        self.tcp_socket.close()
        super().closeEvent(a0)
Beispiel #3
0
class SingPlayer(BasePlayer):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.is_black = True
        self.chessboard = chessboard = [[None for i in range(0, 19)] for j in range(0, 19)]
        self.is_over = True
        self.restart_btn.clicked.connect(self.restart)
        self.win_label = None
        self.huiqi_btn.clicked.connect(self.huiqi)
        self.lose_btn.clicked.connect(self.lose)
        self.num = []

    def restart(self):
        self.is_over = False
        if self.win_label is not None:
            self.win_label.close()
        for i in range(19):
            for j in range(19):
                if self.chessboard[j][i] is not None:
                    self.chessboard[j][i].close()
                    self.chessboard[j][i] = None

    def huiqi(self):
        if  self.is_over:
            return
        a = self.num.pop()
        xx = a[0]
        yy = a[1]
        if self.chessboard[xx][yy] is not None:
            self.chessboard[xx][yy].close()
            self.chessboard[xx][yy] = None
        a = self.num.pop()
        xx = a[0]
        yy = a[1]
        if self.chessboard[xx][yy] is not None:
            self.chessboard[xx][yy].close()
            self.chessboard[xx][yy] = None

    def lose(self):
        if  self.is_over:
            return
        xx = self.num[::-1][0][0]
        yy = self.num[::-1][0][1]
        self.win_label = QLabel(self)
        if self.chessboard[xx][yy].color == 'b':
            pic = QPixmap("photos/黑棋胜利.png")
        else:
            pic = QPixmap("photos/白棋胜利.png")
        self.win_label.setPixmap(pic)
        self.win_label.move(100, 100)
        self.win_label.show()
        self.is_over = True

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        if self.is_over:
            return
        if a0.x() < 40 or a0.x() > 600:
            return
        if a0.y() < 40 or a0.y() > 600:
            return
        if self.is_black:
            self.chess = Chess(color='b', parent=self)
        else:
            self.chess = Chess(color='w', parent=self)
        self.is_black = not self.is_black
        if (a0.x() - 50) % 30 <= 15:
            x = (a0.x() - 50) // 30 * 30 + 50
        else:
            x = ((a0.x() - 50) // 30 + 1) * 30 + 50
        if (a0.y() - 50) % 30 <= 15:
            y = (a0.y() - 50) // 30 * 30 + 50
        else:
            y = ((a0.y() - 50) // 30 + 1) * 30 + 50
        xx = (x - 50) // 30
        yy = (y - 50) // 30

        #self.num.append([xx, yy])
        if self.chessboard[xx][yy] is not None:
            return
        self.chessboard[xx][yy] = self.chess
        self.num.append([xx,yy])

        x = x - self.chess.width() / 2
        y = y - self.chess.height() / 2
        self.chess.move(x, y)
        self.chess.show()
        color = is_win(self.chessboard)

        if color is False:
           pass
        else:
            # QMessageBox.information(self,"消息","{}棋胜利".format(color))
            self.win_label = QLabel(self)
            if color == 'b':
                pic = QPixmap("photos/黑棋胜利.png")
            else:
                pic = QPixmap("photos/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True
        self.auto_run()

#判断完胜负,计算机落子
    def auto_run(self):
    #分别保存计算机白子和黑子的分数
        score_c = [[0 for i in range(0,19)]for j in range(0,19)]
        score_p = [[0 for i in range(0,19)] for j in range(0,19)]
        for j in range(0,19):
            for i in range(0,19):
                if self.chessboard[i][j] is not None:
                    continue #如果有棋子,继续下一个点
            #假设下黑棋的分数
                self.chessboard[i][j]=Chess('b',self)
                score_c[i][j]+=self.score(i,j,'b')
            #假设下白棋的分数
                self.chessboard[i][j] = Chess('w', self)
                score_p[i][j] += self.score(i, j, 'w')
            #恢复棋盘为空
                self.chessboard[i][j]=None
        r_score_c=[]
        r_score_p =[]
        for item in score_c:
            r_score_c+=item
        for item in score_p:
            r_score_p+=item
            #最终分数,去两者中最大的,然后将取值合并成为一个数组
        result=[max(a,b) for a,b in zip(r_score_c,r_score_p)]
        #取出最大值点的下标
        chess_index=result.index(max(result))
        #通过下标计算出落子的位置,
        xx= chess_index//19
        yy= chess_index%19


    #计算机执行落子函数
        if self.is_black:
            self.chess =Chess('b',self)
        else:
            self.chess =Chess('w',self)

        x =xx *30 +50-15
        y =yy*30+50-15
        self.chess.move(x,y)
        self.chess.show()
        self.chessboard[xx][yy] =self.chess
        self.num.append((xx,yy))
        self.is_black =not self.is_black
        color = is_win(self.chessboard)
        if color is False:
            pass
        else:

            self.win_label = QLabel(self)
            if color == 'b':
                pic = QPixmap("photos/黑棋胜利.png")
            else:
                pic = QPixmap("photos/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True

    def score( self,x,y,color):
        #计算如果在x,y这个点下color颜色的棋子,会的多少分
        blank_score=[0,0,0,0]
        chess_score=[0,0,0,0]
        #右方向

        for i in range(x,x+5):
            if i >=19:
                break
            if self.chessboard[i][y] is not None:
                if self.chessboard[i][y].color==color:
                #如果是相同点,同色点分数加1
                    chess_score[0] +=1
                #朝同一方向进行,美遇到相同的颜色,加1
                else:
                    break
            else:
                blank_score[0]+= 1
                break


        for i in range(x-1, x -5,-1):
            if i <= 0:
                break
            if self.chessboard[i][y] is not None:
                if self.chessboard[i][y].color == color:
                    # 如果是相同点,同色点分数加1
                    chess_score[0] += 1
        # 朝同一方向进行,美遇到相同的颜色,加1
                else:
                    break
            else:
                blank_score[0] += 1
                break
            #下方向
        for j in range(y,y+5):
            if j >=19:
                break
            if self.chessboard[x][j] is not None:
                if self.chessboard[x][j].color==color:
                    chess_score[1]+=1
                else:
                    break
            else:
                blank_score[1]+=1
                break
            #上方向
        for j in range(y-1,y-5,-1):
            if j <= 0:
                break
            if self.chessboard[x][j] is not None:
                if self.chessboard[x][j].color==color:
                    chess_score[1]+=1
                else:
                    break
            else:
                blank_score[1]+=1
                break
                #右下方向
        j = y
        for i in range(x,x+5):
            if i>=19 or j>=19:
                break
            if self.chessboard[i][j] is not None:
                if self.chessboard[i][j].color==color:
                    chess_score[2] +=1
                else:
                    break
            else:
                blank_score[2]+=1
                break
            j+=1
                #左上
        j =y-1
        for i in range(x-1,x-5,-1):
            if i<=0 or j<=0:
                break
            if self.chessboard[i][j] is not None:
                if self.chessboard[i][j].color==color:
                    chess_score[2]+=1
                else:
                    break
            else:
                blank_score[2]+=1
                break
            j -=1
        j = y
        for i in range(x , x - 5, -1):
            if i <= 0 or j >= 19:
                break
            if self.chessboard[i][j] is not None:
                if self.chessboard[i][j].color == color:
                    chess_score[3] += 1
                else:
                    break
            else:
                blank_score[3] += 1
                break
            j+=1
                    #右上
        j = y - 1
        for i in range(x + 1, x + 5):
            if i >=19 or j <= 0:
                break
            if self.chessboard[i][j] is not None:
                if self.chessboard[i][j].color == color:
                    chess_score[3] += 1
                else:
                    break
            else:
                blank_score[3] += 1
                break
            j-=1
                    #计算总分
        for score in chess_score:
            if score > 4:#如果某个方向超过4 ,则此处五子连珠
                return 100
        for i in range(0,len(blank_score)):
            if blank_score[i]==0:#说明在此方向上没有同色,也没有继续落子的地方
                blank_score[i] -= 20
                    #四个方向的分数胡,将两个列表一次相加
        result =[a+b for a,b in zip (chess_score,blank_score)]
        return max(result)#返回四个方向的最大值