原文连接:https://blog.csdn.net/sinat_38682860/article/details/105686284?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1
1、namedtuple:命名元组,可以创建一个没有方法只有属性的类
from collections import namedtuple
card = namedtuple('card',['rank','suit']) # rank 牌面大小,suit牌面的花色
# card其实就是namedtuple创建的一个类,其属性就是rank和suit
c1 = card(2,'红心 ')
# c1是实例化的对象
print(c1)
print(c1.suit)
运行结果:
card(rank=2, suit='红心 ')
红心2
2、按照循环顺序,通过对象[索引]任意取牌
from collections import namedtuple
Card = namedtuple('Card',['rank','suit']) # rank 牌面大小,suit牌面的花色
class FranchDeck:
ranks = [str(n) for n in range(2,11)]+list('JQKA')
suits = ['红心','方板','梅花','黑桃']
def __init__(self):
self._cards = [Card(rank,suit) for rank in FranchDeck.ranks for suit in FranchDeck.suits]
# 嵌套循环,每循环一次都拿到一张牌,并且都会存到一个列表中,这个里面的牌是按照循环的顺序的
def __getitem__(self, item):
return self._cards[item]
deck = FranchDeck()
print(deck[10]) # 出现对象加索引的的取法就是触发__getitem__方法
运行结果:
Card(rank='4', suit='梅花')
Process finished with exit code 0
3、choice实现随机抽取一张纸牌的过程
from collections import namedtuple
Card = namedtuple('Card',['rank','suit']) # rank 牌面大小,suit牌面的花色
class FranchDeck:
ranks = [str(n) for n in range(2,11)]+list('JQKA')
suits = ['红心','方板','梅花','黑桃']
def __init__(self):
self._cards = [Card(rank,suit) for rank in FranchDeck.ranks for suit in FranchDeck.suits]
# 嵌套循环,每循环一次都拿到一张牌,并且都会存到一个列表中,这个里面的牌是按照循环的顺序的
def __getitem__(self, item):
return self._cards[item]
def __len__(self):
return len(self._cards)
# 实现按照循环循序抽取对应的纸牌
deck = FranchDeck()
print(deck[10]) # 出现对象加索引的的取法就是触发__getitem__方法
# 实现随机抽取一张牌的过程
from random import choice
print(choice(deck))
# 注意:# choice能够实现随机抽取纸牌,必须得依赖__len__方法,如果前面没有定义__len__方法就会报错,
# 即只要用到choice就会触发__len__方法
运行结果:
Card(rank='A', suit='梅花')
Process finished with exit code 0
4、shuffle实现随机洗牌的过程
from collections import namedtuple
Card = namedtuple('Card',['rank','suit']) # rank 牌面大小,suit牌面的花色
class FranchDeck:
ranks = [str(n) for n in range(2,11)]+list('JQKA')
suits = ['红心','方板','梅花','黑桃']
def __init__(self):
self._cards = [Card(rank,suit) for rank in FranchDeck.ranks for suit in FranchDeck.suits]
# 嵌套循环,每循环一次都拿到一张牌,并且都会存到一个列表中,这个里面的牌是按照循环的顺序的
def __getitem__(self, item):
return self._cards[item]
def __len__(self):
return len(self._cards) # 传纸牌的长度,是从这么多张纸牌里面抽取
def __setitem__(self, key, value):
self._cards[key] = value
deck = FranchDeck()
print(deck[10])
# 通过shuffle和__setitem__实现随机洗牌的过程,如果前面没有定义__setitem__方法同样会报错
# 因为随机洗牌的过程索引对应的牌就会发生改变,所以只能通过__setitem__去改变
from random import shuffle
shuffle(deck)
print(deck[10])
运行结果:
Card(rank='4', suit='梅花')
Card(rank='K', suit='红心')
总结:
以上纸牌游戏实现随机抽牌,洗牌的过程,仅仅只是用了内置的方法,并没有用我们自己定义的方法就实现了
,需要注意的是: __getitem__与对象[索引],__len__与choice,__setitem__与shuffle必须同时结合使用
内置函数,内置模块,内置的基础类型<---->类的内置方法,比如:== <--->__eq__,len()<--->__len__
5、总代码----纸牌游戏随机取牌以及洗牌的过程
# 按照循环顺序,通过对象[索引]任意取牌
# choice实现随机抽取一张纸牌的过程
# shuffle实现随机洗牌的过程
import json
from collections import namedtuple
Card = namedtuple('Card',['rank','suit']) # rank 牌面大小,suit牌面的花色
class FranchDeck:
ranks = [str(n) for n in range(2,11)]+list('JQKA')
suits = ['红心','方板','梅花','黑桃']
def __init__(self):
self._cards = [Card(rank,suit) for rank in FranchDeck.ranks for suit in FranchDeck.suits]
# 嵌套循环,每循环一次都拿到一张牌,并且都会存到一个列表中,这个里面的牌是按照循环的顺序的
def __getitem__(self, item): # 通过对象[索引]进行触发
return self._cards[item]
def __len__(self): # 通过choice进行触发
return len(self._cards) # 传纸牌的长度,是从这么多张纸牌里面抽取
def __setitem__(self, key, value): # 通过shuffle进行触发
self._cards[key] = value
def __str__(self):
return json.dumps(self._cards,ensure_ascii=False) # 序列化
deck = FranchDeck()
# 通过对象[索引]和__getitem__实现按照循环循序抽取对应的纸牌
print(deck[10]) # 出现对象加索引的的取法就是触发__getitem__方法
# 通过choice和__len__实现随机抽取一张牌的过程
from random import choice
print(choice(deck))
# choice能够实现随机抽取纸牌,必须得依赖内置的__len__方法,如果前面没有定义__len__方法就会报错
print(deck[10])
# 通过shuffle和__setitem__实现随机洗牌的过程
from random import shuffle
shuffle(deck)
print(deck[10])
print(deck) # 需要进行序列化才可以
print(deck[:5]) # 直接利用对象进行切片
运行结果:
Card(rank='4', suit='梅花')
Card(rank='A', suit='红心')
Card(rank='4', suit='梅花')
Card(rank='A', suit='梅花')
[["6", "梅花"], ["9", "梅花"], ["3", "梅花"], ["9", "方板"], ["4", "方板"], ["Q", "红心"], ["7", "红心"], ["J", "黑桃"], ["8", "梅花"], ["J", "红心"], ["A", "梅花"], ["6", "黑桃"], ["Q", "方板"], ["4", "梅花"], ["10", "红心"], ["A", "红心"], ["4", "红心"], ["7", "方板"], ["5", "黑桃"], ["K", "黑桃"], ["2", "方板"], ["Q", "梅花"], ["2", "黑桃"], ["8", "黑桃"], ["J", "梅花"], ["A", "黑桃"], ["2", "红心"], ["6", "红心"], ["5", "红心"], ["J", "方板"], ["10", "方板"], ["K", "方板"], ["K", "红心"], ["3", "红心"], ["8", "红心"], ["8", "方板"], ["5", "方板"], ["K", "梅花"], ["9", "红心"], ["5", "梅花"], ["7", "黑桃"], ["3", "方板"], ["Q", "黑桃"], ["2", "梅花"], ["7", "梅花"], ["4", "黑桃"], ["9", "黑桃"], ["10", "梅花"], ["6", "方板"], ["3", "黑桃"], ["10", "黑桃"], ["A", "方板"]]
[Card(rank='6', suit='梅花'), Card(rank='9', suit='梅花'), Card(rank='3', suit='梅花'), Card(rank='9', suit='方板'), Card(rank='4', suit='方板')]