マージャンカードのトランプ操作を実現

14957 ワード

import random


class Card:
    #   5   ,             
    cards = []
    player1 = []
    player2 = []
    player3 = []
    player4 = []

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    #             (     )
    @classmethod
    def init_cards(cls):
        #        
        wan = ("  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ")
        #        
        tong = ("  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
               "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ")
        #        
        tiao = ("  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
            "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ")
        #       
        wind = ("  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ")
        word = ("  ", "  ", "  ", "  ", "  ", "  ",
             "  ", "  ", "  ", "  ", "  ", "  ",
             "  ", "  ", "  ", "  ", "  ", "  ",
             "  ", "  ", "  ", "  ", "  ", "  ")

        #     5    ,   136         ,          
        for wa in wan:
            a = Card(wa)
            cls.cards.append(a)
        for to in tong:
            b = Card(to)
            cls.cards.append(b)
        for ti in tiao:
            c = Card(ti)
            cls.cards.append(c)
        for wi in wind:
            d = Card(wi)
            cls.cards.append(d)
        for wo in word:
            e = Card(wo)
            cls.cards.append(e)

    #      
    @classmethod
    def show_cards(cls):
        for card in cls.cards:
            print(card, end=" ")
        print()

    #   
    @classmethod
    def wash_cards(cls):
        idxx = random.randint(1, 136)
        for idx in range(136):

            cls.cards[idx], cls.cards[idxx] = cls.cards[idxx], cls.cards[idx]

    @classmethod
    def send_cards(cls):
        #              4  ,  3 ,       1  ,    13  
        x = random.randint(2, 12)   #      ,        ,        
        for _ in range(3):
            #         4  
            for _ in range(4):
                cls.player1.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player2.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player3.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player4.append(cls.cards.pop(2 * x))
        #       1  
        cls.player1.append(cls.cards.pop(2 * x))
        cls.player2.append(cls.cards.pop(2 * x))
        cls.player3.append(cls.cards.pop(2 * x))
        cls.player4.append(cls.cards.pop(2 * x))

    @classmethod
    def show_player(cls):
        print("   :", end="")
        for card in cls.player1:
            print(card, end=" ")
        print()
        print("   :", end="")
        for card in cls.player2:
            print(card, end=" ")
        print()
        print("   :", end="")
        for card in cls.player3:
            print(card, end=" ")
        print()
        print("   :", end="")
        for card in cls.player4:
            print(card, end=" ")
        print()
Card.init_cards()
Card.show_cards()
Card.wash_cards()
Card.show_cards()
Card.send_cards()
Card.show_player()