Beispiel #1
0
    def mouseReleaseEvent(self, a0: QMouseEvent):
        if self.is_over:  # 如果游戏已经结束,点击失效
            return
        # 如果点击在棋盘区域
        if a0.x() >= 50 and a0.x() <= 50 + 30 * 18 + 14 and a0.y(
        ) >= 50 and a0.y() <= 50 + 30 * 18 + 14:
            # 讲像素坐标转化成棋盘坐标,判断棋盘此位置是否为空
            pos = trans_pos(a0)
            if chessboard[pos[1]][pos[0]] is not None:
                return  # 如果对应位置不为空,说明有棋子,则直接返回

            # 不为空,则生成棋子并显示
            self.chess = Chessman(self.color, self)
            self.chess.move(a0.pos())
            self.logo_move()
            self.chess.show()
            self.change_color()
            pygame.mixer.music.play()

            # 在棋盘的对应位置放上棋子
            chessboard[pos[1]][pos[0]] = self.chess
            # 并且在列表中记录坐标
            history.append((pos[1], pos[0], self.chess.color))

            # 每次落子后,都判断一下胜负
            res = is_win(chessboard)
            if res:
                self.win(res)  # 通过颜色,显示胜利的图片
    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        # 如果游戏已经结束,点击失效
        if self.is_over == True:
            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)
        # 翻转棋子颜色
        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()

        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("source/黑棋胜利.png")
            else:
                pic = QPixmap("source/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True

        # 判断完胜负,计算机落子
        self.auto_run()
Beispiel #3
0
    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent):
        print("asd")
        # 如果游戏已经结束,点击失效
        if a0.x() < 40 or a0.x() > 600:
            return
        if a0.y() < 40 or a0.y() > 600:
            return
        # 通过标识,决定棋子的颜色
        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.chess.show()

        # 落子后, 发送棋子位置
        pos_data = {"msg": "pos", "data": (xx, yy)}
        self.tcp_socket.sendall((json.dumps(pos_data) + " END").encode())

        # 翻转棋子颜色
        # 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("source/黑棋胜利.png")
            else:
                pic = QPixmap("source/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True
Beispiel #4
0
    def auto_run(self):
        '''
        电脑自动执行落子操作
        :return: 自动落子
        '''
        # 找到能下棋的空位置中,假设电脑和人下在此处,得到分数中最大值

        score_c = [[0 for i in range(0, 19)] for i in range(0, 19)]
        score_p = [[0 for i in range(0, 19)] for i in range(0, 19)]

        # 计算所有的分数
        for j in range(0, 19):
            for i in range(0, 19):
                if chessboard[i][j] is None:
                    # 如果此处为空 , 计算此处分数,分别记下落黑子和落白子不同的分数
                    chessboard[i][j] = Chessman('b', parent=self)
                    score_c[i][j] += self.score(i, j, 'b')
                    chessboard[i][j] = Chessman('w', parent=self)
                    score_p[i][j] += self.score(i, j, 'w')
                    chessboard[i][j].close()
                    chessboard[i][j] = None

        # 为便于计算,将两个二维数组,转换成两个一位数组
        r_score_c = []
        for item in score_c:
            r_score_c.extend(item)

        r_score_p = []
        for item in score_p:
            r_score_p.extend(item)

        # 最终分数,取两个数组中的最大值合并成一个数组
        result = [max(a, b) for a, b in zip(r_score_c, r_score_p)]

        # 取最大值点的下标
        chess_index = result.index(max(result))
        # 通过下标计算出位置并落子
        x = chess_index // 19
        y = chess_index % 19

        self.chess = Chessman(self.color, self)
        self.chess.move(QPoint(y * 30 + 50, x * 30 + 50))
        self.chess.show()
        self.logo_move()
        self.change_color()
        pygame.mixer.music.play()
        chessboard[x][y] = self.chess
        history.append((x, y, self.chess.color))

        # 每次落子后,都判断一下胜负
        res = is_win(chessboard)
        if res:
            self.win(res)  # 通过颜色,显示胜利的图片
            return None
Beispiel #5
0
    def mouseReleaseEvent(self, a0: QMouseEvent):
        if self.is_over:  # 如果游戏已经结束,点击失效
            return
        if not self.my_turn:
            print("not my turn")
            return
        # 如果点击在棋盘区域
        if a0.x() >= 50 and a0.x() <= 50 + 30 * 19 and a0.y() >= 50 and a0.y(
        ) <= 50 + 30 * 19:

            # 讲像素坐标转化成棋盘坐标,判断棋盘此位置是否为空
            pos = trans_pos(a0)
            if chessboard[pos[1]][pos[0]] is not None:
                return  # 如果对应位置不为空,说明有棋子,则直接返回

            # 不为空,则生成棋子并显示
            self.chess = Chessman(self.color, self)
            self.chess.move(a0.pos())
            self.chess.show()
            pygame.mixer.music.play()  # 播放声音
            self.logo_move()  # 移动小标
            self.change_color()

            # 在棋盘的对应位置放上棋子
            chessboard[pos[1]][pos[0]] = self.chess
            # 并且在列表中记录坐标
            history.append((pos[1], pos[0], self.chess.color))
            # 将坐标发送给另一方
            if self.tcp_socket is not None:
                data = {"msg": "position", "data": pos}
                self.tcp_socket.sendall((json.dumps(data) + " END").encode())

            # 每次落子后,都判断一下胜负
            res = is_win(chessboard)
            if res:
                self.win(res)  # 通过颜色,显示胜利的图片
                return
            self.my_turn = False
            self.label_statuvalue.setText("对方回合")
Beispiel #6
0
    def deal_data(self, data):
        '''
        对收到的数据进行处理
        '''
        print(data)
        if data['msg'] == 'action':
            if data['data'] == 'restart':
                result = QMessageBox.information(
                    self, "消息", "对方请求(重新)开始游戏,是否同意?",
                    QMessageBox.Yes | QMessageBox.No)
                if result == QMessageBox.Yes:
                    data = {"msg": "replay", "data": True, "type": "restart"}
                    self.tcp_socket.sendall(
                        (json.dumps(data) + " END").encode())
                    self.restart_func()
                    self.is_over = False
                    if self.my_turn:
                        self.label_statuvalue.setText("己方回合")
                    else:
                        self.label_statuvalue.setText("对方回合")
                else:
                    data = {"msg": "replay", "data": False, "type": "restart"}
                    self.tcp_socket.sendall(
                        (json.dumps(data) + " END").encode())
                    self.label_statuvalue.setText("点击开始")

            if data['data'] == 'lose':
                QMessageBox.information(self, "消息", "对方认输")
                if self.my_turn:

                    self.win(color=self.color)
                    # self.change_color()
                else:
                    self.change_color()
                    self.win(color=self.color)
            if data['data'] == 'goback':
                result = QMessageBox.information(
                    self, "消息", "对方请求悔棋,是否同意?",
                    QMessageBox.Yes | QMessageBox.No)
                if result == QMessageBox.Yes:
                    data = {"msg": "replay", "data": True, "type": "goback"}
                    self.tcp_socket.sendall(
                        (json.dumps(data) + " END").encode())
                    self.goback_func()
                    # self.is_over = False
                    if self.my_turn:
                        self.label_statuvalue.setText("己方回合")
                    else:
                        self.label_statuvalue.setText("对方回合")
                else:
                    data = {"msg": "replay", "data": False, "type": "goback"}
                    self.tcp_socket.sendall(
                        (json.dumps(data) + " END").encode())
                    # self.label_statuvalue.setText("等待开始")

            if data['data'] == 'cuicu':
                print(self.is_connected)
                if not self.is_connected:
                    return
                if self.is_over:
                    return
                print("cuicu")
                sound.play()
                # pygame.mixer.music.load("source/luozisheng.wav")

            if data['data'] == 'ready':
                pass
            if data['data'] == 'exit':
                # 对方退出游戏
                self.is_connected = False
                self.is_listening = False
                self.tcp_socket.close()
                self.tcp_socket = None
            print(data)

        elif data['msg'] == 'position':
            print(data['data'])
            # 在对应位置落子
            pos = data['data']
            if chessboard[pos[1]][pos[0]] is not None:
                return  # 如果对应位置不为空,说明有棋子,则直接返回

            self.chess = Chessman(self.color, self)
            self.chess.move(QPoint(pos[0] * 30 + 50, pos[1] * 30 + 50))
            self.chess.show()
            pygame.mixer.music.play()  # 播放声音
            self.logo_move()  # 移动小标
            self.change_color()

            # 在棋盘的对应位置放上棋子
            chessboard[pos[1]][pos[0]] = self.chess
            # 并且在列表中记录坐标
            history.append((pos[1], pos[0], self.chess.color))
            # 每次落子后,都判断一下胜负
            res = is_win(chessboard)
            if res:
                self.win(res)  # 通过颜色,显示胜利的图片
                return
            self.my_turn = True
            self.label_statuvalue.setText("己方回合")
        elif data['msg'] == 'replay':
            if data['type'] == 'restart':  # 重开回执
                if data['data'] == True:
                    self.restart_func()
                else:
                    QMessageBox.information(self, "消息", "对方拒绝了你的请求")
                    self.label_statuvalue.setText("点击开始")
                    return
                if self.my_turn:
                    self.label_statuvalue.setText("己方回合")
                else:
                    self.label_statuvalue.setText("对方回合")
            if data['type'] == 'goback':  # 悔棋回执

                if data['data'] == True:
                    self.is_over = False
                    self.goback_func()
                else:
                    QMessageBox.information(self, '消息', '对方拒绝了你的请求')
                if self.my_turn:
                    self.label_statuvalue.setText("己方回合")
                else:
                    self.label_statuvalue.setText("对方回合")
                self.is_over = False

        elif data['msg'] == 'name':
            self.setWindowTitle('与 {} 对战中'.format(data['data']))
    def auto_run(self):
        '''
        计算机执行落子函数
        '''
        # 分别保存黑子,白子分数的数组
        scores_c = [[0 for i in range(0, 19)] for j in range(0, 19)]
        scores_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)
                scores_c[i][j] += self.score(i, j, 'b')
                # 假设下白棋的分数
                self.chessboard[i][j] = Chess('w', self)
                scores_p[i][j] += self.score(i, j, 'w')
                # 恢复棋盘为空
                self.chessboard[i][j] = None

        # 先将两个二维数组,转成一位数组便于运算
        r_scores_c = []
        r_scores_p = []
        for item in scores_c:
            r_scores_c += item
        for item in scores_p:
            r_scores_p += item

        # 最终分数,取两者中更大的一个,然后将取值合并成为一个数组
        result = [max(a, b) for a, b in zip(r_scores_c, r_scores_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.history.append((x, y))
        self.is_black = not self.is_black
        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("source/黑棋胜利.png")
            else:
                pic = QPixmap("source/白棋胜利.png")
            self.win_label.setPixmap(pic)
            self.win_label.move(100, 100)
            self.win_label.show()
            self.is_over = True
Beispiel #8
0
    def deal_data(self, data):
        print(data)
        if data["msg"] == "name":
            name = data["data"]
            title = "与" + name + "对战中"
            self.is_connect = True
            self.setWindowTitle("已连接")
        if data["msg"] == "pos":
            pos = data['data']
            xx = pos[0]
            yy = pos[1]
            # 通过标识,决定棋子的颜色
            if self.is_black:
                self.chess = Chess('b', self)
            else:
                self.chess = Chess('w', self)
            # 如果此处已经有棋子,点击失效
            if self.chessboard[xx][yy] is not None:
                return

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

            x = xx * 30 + 35
            y = yy * 30 + 35

            self.chess.move(x, y)
            self.chess.show()
            self.is_black = not self.is_black
            self.my_turn = not self.my_turn

            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("source/黑棋胜利.png")
                else:
                    pic = QPixmap("source/白棋胜利.png")
                self.win_label.setPixmap(pic)
                self.win_label.move(100, 100)
                self.win_label.show()
                self.is_over = True

        # 触发重新开始(开始)

        if data["msg"] == "action":
            if data["data"] == "restart":
                result = QMessageBox.information(
                    self, "消息", "对方请求(重新)开始游戏,是否同意?",
                    QMessageBox.Yes | QMessageBox.No)
                if result == QMessageBox.Yes:
                    # 同意的代码
                    self.tcp_socket.sendall((json.dumps({
                        "msg": "replay",
                        "type": "reatart"
                    }) + " END").encode())
                    self.restart_func()
                else:
                    # 拒绝的代码
                    self.tcp_socket.sendall((json.dumps({
                        "msg": "replay",
                        "type": "noreatart"
                    }) + " END").encode())

            elif data["data"] == "lose":
                QMessageBox.information(self, "认输", "对方认输")
                self.tcp_socket.sendall((json.dumps({
                    "msg": "replay",
                    "type": "lose"
                }) + "END").encode())
                self.lose_func()
            elif data["data"] == "huiqi":
                result = QMessageBox.information(
                    self, "消息", "对方请求悔棋,是否同意?",
                    QMessageBox.Yes | QMessageBox.No)
                if result == QMessageBox.Yes:
                    # 同意的代码
                    self.tcp_socket.sendall((json.dumps({
                        "msg": "replay",
                        "type": "huiqi"
                    }) + " END").encode())
                    self.huiqi_func()
                else:
                    # 拒绝的代码
                    self.tcp_socket.sendall((json.dumps({
                        "msg": "replay",
                        "type": "nohuiqi"
                    }) + " END").encode())
            elif data["data"] == "cuicu":
                self.sound_cuicu.play()
        if data["msg"] == "replay":
            if data["type"] == "huiqi":
                QMessageBox.information(self, "同意消息", "对方已同意!")
                self.huiqi_func()

            elif data["type"] == "lose":
                self.lose_func()
            elif data["type"] == "nohuiqi":
                QMessageBox.information(self, "不同意消息", "对方不同意!")
            elif data["type"] == "reatart":
                QMessageBox.information(self, "同意消息", "对方已同意!")
                self.restart_func()
            elif data["type"] == "noreatart":
                QMessageBox.information(self, "不同意消息", "对方不同意!")

        if data['msg'] == "error":  # 连接断开的情况
            if data["data"] == "disconnect":
                QMessageBox.information(self, "消息", "对方已退出游戏,连接断开,即将返回主界面")
                self.backSignal.emit()
                self.close()
            elif data["data"] == "recycle":
                print("?????")
                self.backSignal.emit()
                print(self)
                self.close()