python3.6 Environment
ImportCollections fromRandomImportChoicecard=collections.namedtuple ('Card',['Rank','Suit'])classfrenchdeck:ranks=[STR (N) forNinchRange (2,11)] +list ('Jqka')#the size range of poker Print(ranks) suits='Spades Diamods Clubs Hearts'. Split ()#Spades , diamonds, squares, Hearts Print(suits)def __init__(self): Self._cards=[card (Rank,suit) forSuitinchself.suits forRankinchSelf.ranks]def __len__(self):returnLen (self._cards)def __getitem__(self, position):returnSelf._cards[position] Beer_card=card ('7','Diamonds')#instance a poker point Print(Beer_card) Deck= Frenchdeck ()#Create a Poker object deckPrint(Len (deck))#How many cards are printed in totalPrint(Deck[0])#print the first card of Spades 2Print(Deck[-1])#print the last card of hearts aPrint(Choice (deck))#randomly draw a cardPrint(Deck[:3])#View top three spades 2, 3, 4Print(Deck[12::13])#draw the card with an index of 12, and then draw a card every 13 to exactly 4 a#implements the __getitem__ method, this stack of cards becomes an iterative object forCardinchDeck:Print(Card)Print('=============================== Below is the echo iteration ===============================')#Reverse Iteration forCardinchreversed (deck):Print(Card)#determine if a card instance exists in this deckPrint(Card ('Q','Hearts')inchDeck#TruePrint(Card ('7','Beasts')inchDeck#False#Compare the size of poker to 2 min, A max;#on the judgement of the color, the biggest spades, the Red Peach, the square again, the smallest plum. #here is the function of the order of cards according to this rule, the size of the plum Blossom 2 is 0, the spades A is:Suit_values=dict (spades=3,hearts=2,diamods=1,clubs=0)defSpades_high (Card):
# Get the poker points rank_value=FrenchDeck.ranks.index (Card.rank)
# Returns an example of an index between poker 0-51, based on an instance of an incoming card. Spades a:12*4+3=51returnRank_value * Len (suit_values) +Suit_values[card.suit]#sort the card in ascending order by the function defined abovePrint('=============================== Custom Sort ===============================') forCardinchSorted (Deck, key=Spades_high):Print(card)
Python3 Analog Poker