發牌器
阿新 • • 發佈:2018-12-14
實現發牌器(排中不包含大小鬼),共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 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())