张拽拽

导航

12/13

  1. 实现发牌器(排中不包含大小鬼),共4个玩家,每人13张牌
    from random import shuffle
    
    class Card(object):
        colors = ['黑桃','方片','红心','梅花']
        numbers = ['A']+ [str(i) for i in range(2,11)]+ ['J']+ ['Q']+ ['K']
        def __init__(self,color,number,face= True):
            self._color = color
            self._number = number
            self._face = face
    
        def show(self):
            if self._face:
                return self._color + self._number
            else:
                return 'x'
    
        def flip(self):
            self._face = not self._face
    
    
    class Hand(object):
        def __init__(self,name = '神秘玩家'):
            self._name = name
            self._cards = []
    
        def addcard(self,card):
            self._cards.append(card)
    
        def show(self):
            end = []
            for c in self._cards:
                end.append(c.show())
            return ','.join(end)
    
    class Poke(Hand):
        def allcard(self):
            for c in Card.colors:
                for n in Card.numbers:
                    self._cards.append(Card(c,n))
    
        def randpoke(self):
            shuffle(self._cards)
    
        def dealpoke(self,hands,count = 13):
            for c in range(count):
                for h in hands:
                    poppoke = self._cards.pop()
                    h.addcard(poppoke)
    
    
    if __name__ =='__main__':
        poke = Poke()
        poke.allcard()
        poke.randpoke()
        hands = [Hand('小猪'),Hand('小狗'),Hand('蜈蚣'),Hand('老虎')]
        poke.dealpoke(hands)
    
        for h in hands:
            print(h._name,h.show())

posted on 2018-12-14 15:21  张拽拽  阅读(156)  评论(0编辑  收藏  举报