看做是有52张牌的牌手,所以继承Hand类。由于其中cards列表变量要存储52张牌
而且要发牌,洗牌,所以增加方法如下方法:"""
def populate(self): # 生成一副牌
for suit in Card.SUITS:
for rank in Card.RANKS:
self.add(Card(rank, suit))
def shuffle(self): # 洗牌
import random
random.shuffle(self.cards) # 打乱牌的顺序
def deal(self, hands, per_hand=13): # 将牌发给玩家,每人默认13张牌
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[0]
self.cards.remove(top_card)
hand.add(top_card)
# self.give(top_card,hand) #上两句可以用此句替换
else:
print('不能继续发牌了,牌已经发完了!')
if __name__ == "__main__":
print('This is a module with classes for playing cards.')
players = [Hand(), Hand(), Hand(), Hand()]
poke1 = Poke()
poke1.populate() # 生成一副牌
poke1.shuffle() # 洗牌
poke1.deal(players, 13) # 发给每人13张牌
n = 1
for hand in players:
print('牌手', n, end=':')
print(hand)
n = n + 1
到此这篇关于Python用类实现扑克牌发牌的示例代码的文章就介绍到这了,更多相关Python 扑克牌发牌内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
专门建立的Python学习扣扣圈,从零基础开始到Python各领域的项目实战教程、开发工具与电子书籍。与你分享企业当下对于python人才需求及学好python的高效技巧,不停更新最新教程!点击加入我们的python学习圈