示例#1
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
示例#2
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)  # 通过颜色,显示胜利的图片
示例#3
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("对方回合")
示例#4
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']))