用Python的魔法方法实现扑克牌
"""
@author:amgoole
@date: 2021/6/26
@email: xwen.xi@icloud.com
"""
import collections
# 实现一个简单卡片类
Card = collections.namedtuple("Card", ['rank', 'suit'])
# 实现一副扑克类
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list("JQKA")
suits = "spades diamonds clubs hearts".split()
# 生成52张3扑克牌
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, item):
return self._cards[item]
# 简易发牌
def pop(self, cad=None):
if not self._cards:
print("牌已发完")
return
elif isinstance(cad, Card):
self._cards.pop(self._cards.index(cad))
else:
self._cards.pop()
# 实例化单张牌
beer_card = Card('7', 'diamonds')
print(beer_card)
deck = FrenchDeck()
# 找出前三张牌
print(deck[:3])
# 找出所有的A
print(deck[12::13])
# 设置花型权重
weight = dict(spades=3, hearts=2, diamonds=1, clubs=0)
# 计算单张牌的排位顺序
def spades_high(cad: Card):
rank = FrenchDeck.ranks.index(cad.rank)
return rank * len(FrenchDeck.suits) + weight[cad.suit]
print(spades_high(Card('3', 'clubs')))
# 按照排位顺序输出
for card in sorted(deck, key=spades_high):
print(card)
FranchDeck
实例可以被索引,切片的原因是该类实现了__getitem__
方法,当使用[]
索引或切片时,会将操作[]
传递给_cards
代理实现,因而FranchDeck
能够像列表一样被操作。