编程语言
首页 > 编程语言> > 一摞Python风格纸牌

一摞Python风格纸牌

作者:互联网

import collections  # 引入collections模块

Card = collections.namedtuple('Card', ['rank', 'suit'])  # 用namedtuple创造了一个简单的类Card


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')  # 使用列表操作生成字符串类型数字组成的列表,然后加上列表['J','Q','K','A']
    suits = 'spades diamonds clubs hearts'.split()  # split()函数不输入参数,默认使用空格来分割字符串,分割出黑桃、方块、梅花、红桃

    def __init__(self):  # 初始化类
        self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]  # 通过两个循环分别遍历ranks和suits来组成52张牌

    def __len__(self):  # 定义特殊方法__len__
        return len(self._cards)  # 通过len()函数来返回类属性_cards的长度

    def __getitem__(self, position):  # 定义特殊方法__getitem__
        return self._cards[position]  # 根据索引position返回self._cards中该索引位置的字符串
beer_card = Card('7', 'diamonds')  # 实例化类Card
beer_card
Card(rank='7', suit='diamonds')
deck = FrenchDeck()
len(deck)  # 调用类中__len__方法来查看一摞牌有多少张
52
deck[0]  # 有__getitem__方法来根据索引获取字符串
Card(rank='2', suit='spades')
from random import choice  # 从模块random中引入一个随机选出一个元素的函数choice

for i in range(3):
    print(choice(deck))  # 将choice函数在这一摞纸牌上实例化,来随机抽选一张纸牌
Card(rank='5', suit='diamonds')
Card(rank='8', suit='spades')
Card(rank='7', suit='spades')
deck[:3]  # 类的__getitem__方法将[]操作交给了self._cards,所以实例化的deck类自动支持切片操作
[Card(rank='2', suit='spades'),
 Card(rank='3', suit='spades'),
 Card(rank='4', suit='spades')]
for card in deck:  # 实现了__getitem__方法,这一摞牌就变成了可迭代的
    print(card)
    break
for card in reversed(deck):  # 也可以进行反向迭代
    print(card)
    break
Card(rank='2', suit='spades')
Card(rank='A', suit='hearts')

迭代通常是隐式的,如果一个集合类型没有实现__contain__方法,那么in运算符就会按顺序做一次迭代搜索
in运算符可以用在FrenchDeck类上,因为它是可迭代

Card('Q', 'hearts') in deck
True

用点数来判定扑克牌的大小。2最小,A最大;同时还要加上对花色的判定,黑桃最大、红桃次之、方块再次、梅花最小。

suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)  # 创建点数列表

def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)  # FrenDeck类的属性ranks的index方法来获取卡片的rank的索引,返回索引越大,牌也就越大
    return rank_value * len(suit_values) + suit_values[card.suit]  # 将返回的牌的索引*4(len(suit_values)=4)然后加上花色的大小就等于牌的大小


for card in sorted(deck, key=spades_high):  # 使用参数key来使用排序方法spades_high
    print(card)
Card(rank='2', suit='clubs')
Card(rank='2', suit='diamonds')
Card(rank='2', suit='hearts')
Card(rank='2', suit='spades')
Card(rank='3', suit='clubs')
Card(rank='3', suit='diamonds')
Card(rank='3', suit='hearts')
Card(rank='3', suit='spades')
Card(rank='4', suit='clubs')
Card(rank='4', suit='diamonds')
Card(rank='4', suit='hearts')
Card(rank='4', suit='spades')
Card(rank='5', suit='clubs')
Card(rank='5', suit='diamonds')
Card(rank='5', suit='hearts')
Card(rank='5', suit='spades')
Card(rank='6', suit='clubs')
Card(rank='6', suit='diamonds')
Card(rank='6', suit='hearts')
Card(rank='6', suit='spades')
Card(rank='7', suit='clubs')
Card(rank='7', suit='diamonds')
Card(rank='7', suit='hearts')
Card(rank='7', suit='spades')
Card(rank='8', suit='clubs')
Card(rank='8', suit='diamonds')
Card(rank='8', suit='hearts')
Card(rank='8', suit='spades')
Card(rank='9', suit='clubs')
Card(rank='9', suit='diamonds')
Card(rank='9', suit='hearts')
Card(rank='9', suit='spades')
Card(rank='10', suit='clubs')
Card(rank='10', suit='diamonds')
Card(rank='10', suit='hearts')
Card(rank='10', suit='spades')
Card(rank='J', suit='clubs')
Card(rank='J', suit='diamonds')
Card(rank='J', suit='hearts')
Card(rank='J', suit='spades')
Card(rank='Q', suit='clubs')
Card(rank='Q', suit='diamonds')
Card(rank='Q', suit='hearts')
Card(rank='Q', suit='spades')
Card(rank='K', suit='clubs')
Card(rank='K', suit='diamonds')
Card(rank='K', suit='hearts')
Card(rank='K', suit='spades')
Card(rank='A', suit='clubs')
Card(rank='A', suit='diamonds')
Card(rank='A', suit='hearts')
Card(rank='A', suit='spades')

标签:__,spades,纸牌,Python,diamonds,rank,suit,一摞,Card
来源: https://www.cnblogs.com/Reion/p/15230718.html