实现发牌器(排中不包含大小鬼),共4个玩家,每个人13张牌
from random import shuffleclass 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_.faceclass Hand(object): def __init__(self,name='神秘玩家'): self._name=name self._cards=[] #一手牌 def add(self,card): '''增加一张牌''' self._cards.append(card) def show(self): res = [] for c in self._cards: res.append(c.show()) return ','.join(res)class Poke(Hand): def getall(self): '''生成由52张牌组成的一手牌''' for c in Card.colors: for n in Card.numbers: self._cards.append(Card(c,n)) def randomPuke(self): '''洗牌''' shuffle(self._cards) def dealPuke(self,hands,count = 13): '''发牌''' for c in range(count): for h in hands: poppuke = self._cards.pop() h.add(poppuke)if __name__ == '__main__': poke = Poke() #生成一副扑克 poke.getall() poke.randomPuke() hands = [Hand('王志勇'),Hand('陈云亮'),Hand('杨智超'),Hand('吴童')] poke.dealPuke(hands) for h in hands: print(h._name,h.show())