Python3 Analog Poker

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.