编程语言
首页 > 编程语言> > Python斗地主扑克牌游戏

Python斗地主扑克牌游戏

作者:互联网

斗地主扑克牌游戏

import random


class PokerGame:
    def __init__(self):
        self.poker = ['♣2', '♥2', '♦2', '♠2',
                      '♣3', '♥3', '♦3', '♠3',
                      '♣4', '♥4', '♦4', '♠4',
                      '♣5', '♥5', '♦5', '♠5',
                      '♣6', '♥6', '♦6', '♠6',
                      '♣7', '♥7', '♦7', '♠7',
                      '♣8', '♥8', '♦8', '♠8',
                      '♣9', '♥9', '♦9', '♠9',
                      '♣10', '♥10', '♦10', '♠10',
                      '♣J', '♥J', '♦J', '♠J',
                      '♣Q', '♥Q', '♦Q', '♠Q',
                      '♣K', '♥K', '♦K', '♠K',
                      '♣A', '♥A', '♦A', '♠A',
                      'JOKER', 'joker'] 


    def __repr__(self):
        return f'{str(self.__dict__)[10:-1]}'

    @staticmethod
    def give_card(poker):  # 发牌
        player1 = []
        player2 = []
        player3 = []
        num = 0
        index = 0
        # 给三个人发牌
        while num != 17:
            player1.append(poker[index])
            player2.append(poker[index+1])
            player3.append(poker[index+2])
            num += 1
            index += 3
        # 定义牌大小规则
        rule = {'♣3': 0, '♥3': 0, '♦3': 0, '♠3': 0,
                '♣4': 1, '♥4': 1, '♦4': 1, '♠4': 1,
                '♣5': 2, '♥5': 2, '♦5': 2, '♠5': 2,
                '♣6': 3, '♥6': 3, '♦6': 3, '♠6': 3,
                '♣7': 4, '♥7': 4, '♦7': 4, '♠7': 4,
                '♣8': 5, '♥8': 5, '♦8': 5, '♠8': 5,
                '♣9': 6, '♥9': 6, '♦9': 6, '♠9': 6,
                '♣10': 7, '♥10': 7, '♦10': 7, '♠10': 7,
                '♣J': 8, '♥J': 8, '♦J': 8, '♠J': 8,
                '♣Q': 9, '♥Q': 9, '♦Q': 9, '♠Q': 9,
                '♣K': 10, '♥K': 10, '♦K': 10, '♠K': 10,
                '♣A': 11, '♥A': 11, '♦A': 11, '♠A': 11,
                '♣2': 12, '♥2': 12, '♦2': 12, '♠2': 12,
                'joker': 13, 'JOKER': 14}
        new_player1 = sorted(player1, key=lambda x: rule[x])
        new_player2 = sorted(player2, key=lambda x: rule[x])
        new_player3 = sorted(player3, key=lambda x: rule[x])
        new_undercard = sorted(poker[-3:], key=lambda x: rule[x])    # 底牌
        print(f'Player1:{new_player1 }')
        print(f'Player2:{new_player2}')
        print(f'Player3:{new_player3}')
        print(f'底牌:{new_undercard}')



    def shuffle_the_deck(self):  # 洗牌
        self.poker = str(self.__dict__)[10:-1]
        self.poker = eval(self.poker)
        random.shuffle(self.poker)
        return self.poker


poker1 = PokerGame()
poker2 = poker1.shuffle_the_deck()      # 洗牌
poker1.give_card(poker2)                # 发牌

标签:__,10,poker,扑克牌,Python,index,self,斗地主,new
来源: https://blog.csdn.net/Maple12138/article/details/114036308