實現紙牌游戲的隨機抽牌洗牌過程(item系列幾個內置方法的實例) 1、namedtuple:命名元組,可以創建一個沒有方法只有屬性的類 from collections import namedtuple card = namedtuple('card',['rank','suit']) # ran ...
實現紙牌游戲的隨機抽牌洗牌過程(item系列幾個內置方法的實例)
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='方板')]