Example #1
0
    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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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()