Poker program Python__python

Source: Internet
Author: User
Tags iterable

is the first project of the Udacity course.

The idea is to make a comparison of the size of Texas poker.
First, you abstract a processing function, which gives the result based on the size of the return value.

After we've defined how to compare the sizes of two or more hands, to make it easier to compare sizes, we first preprocess 5 cards and sort them in descending order, as follows:

def card_ranks (hand):

    ranks = ['--23456789tjqka '. INDEX (R) for R, s in hand]
    Ranks.sort (reverse=true) return
    ranks

Then we can enumerate a total of 9 kinds of cases, and use the numbers to represent each level of the situation, using Python's comparison function, put the rank first, if the rank is the same, then compare the back.

def hand_rank (hand):
    "Return a value indicating the ranking of a hand."
    ranks = Card_ranks (hand) 
    if straight (ranks) and flush (hand): Return
        (8, Max (ranks))
    elif Kind (4, ranks):  Return
        (7, Kind (4, ranks), kind (1, ranks))
    elif Kind (3, ranks) and kind (2, ranks): Return (6,
        kind (3, ranks), Kind (2, ranks))
    elif Flush (hand): Return
        (5, ranks)
    elif Straight (ranks): Return
        (4, Max (ranks))
    Elif Kind (3, ranks): Return
        (3, Kind (3, ranks), ranks)
    elif Two_pair (ranks): Return
        (2, Two_pair ( Ranks), ranks)
    elif kind (2, ranks): Return
        (1, Kind (2, ranks), ranks)
    else: return
        (0, ranks)

You can see that if the level is the same, the next comparison is the size of the cards in each set. At the same time we need three functions, representing the same flower, CIS, and kind (n, ranks), which represent the number of points in the ranks that have n cards. The three function implementations here are very ingenious, taking advantage of the set go-heavy feature.

def straight (ranks): Return
    (Max (ranks)-min (ranks)) = = 4 and Len (set (ranks)) = = 5

def flush (hand):
    suit = [s , for R, s in hand] return
    len (set (suit) = = 1
def kind (n, ranks): for
    s in ranks:
        if ranks.count (s) = n : return s return
    None

We found that there was a situation that contained two pairs, so a function is needed to determine whether this is the case, this function calls the kind () function, because the kind () function satisfies the short-circuit characteristics, will only return the first received the number of satisfied, and then flip it on the side of the call kind, If the results are the same, then there is only one pair (or none), otherwise there are two.

def two_pairs (ranks):
    pair = Kind (2, ranks)
    Lowpair = Kind (2, List (reverse (ranks)))
    if pair!= />return (pair, Lowpair)
    else: return
        None

Well, the whole skeleton is done, and the next thing to do with the bug is A2345, which, when sorted because A is counted as 14, needs to be listed separately for this problem if

Processing A is the lowest:
def card_ranks (hand):
    ranks = ['--23456789tjqka '. INDEX (R) for R, s in hand]
    Ranks.sort (reverse=true)
    return  [5, 4, 3, 2, 1] if (ranks = [5, 4, 3, 2] Els E ranks

The

is followed by a further simplification, and the idea is very good

def poker (Hands): Return Allmax (Hands, Key=hand_ranks) def allmax (Iterable, Key=none): result, Maxval = [], None
            Ket = key or lambda (x): X for x in iterable:xval = key (x) If not result or Xval > maxval:

result, Maxval = [x], the Xval elif:result.append (x) return result "" "is greater than is substituted, is equal to joins, is smaller than does not handle" "" "" Import random Mydeck = [r+s for R in ' 23456789TJKQA ' for S in ' SHDC] def deal (Numhands, n=5, deck = [R+s for R in ' 234567 89TJKQA ' to S in ' shdc]): Random.shuffle (deck) return [deck[n*i:n* (i + 1)] for I in range (numhands)] def hand_r  Anks (hand): groups = group['--23456789tjqka '. Index (R) for R, s in hand] counts, ranks = unzip (groups) if Rnaks
    = = (5, 4, 3, 2, 1): Ransk = (5, 4, 3, 2, 1) straight = Len (ranks) = = 5 and Max (ranks)-min (ranks) = 4
          Flush = Len (set ([s for R, S-hand]) ==1 return (9 if (5,) = = Count Else 8 if straight and flush else 7 if (4, 1) = =Counts Else 6 if (3, 2) = = Counts Else 5 if flush else 4 if straight else 3 if (3, 1, 1) = = Counts Else 2 if (5, 1, 1) = = Counts Else 1 if (2, 1, 1, 1) = = Counts else 0), ran KS def group (items): groups = [(Items.Count (x), X) for X to set (items)] return sorted (groups, reverse = True) def
    Unzips (Pairs): return Zip (*pairs) def hand_ranks (hand): groups = group['--23456789tjqka '. Index (R) for R, s in hand] Counts, ranks = unzip (groups) if Rnaks = = (5, 4, 3, 2, 1): Ransk = (5, 4, 3, 2, 1) straight = Len ( Ranks) = = 5 and Max (ranks)-min (ranks) = = 4 Flush = Len (set ([s for R, s in hand]) ==1 return Max (Count_ranks[cou NTS], 4*straight + 5 * flush), ranks count_rankings = {(5,): 10, (4, 1): 7, (3,2): 6, (3,1,1): 3, (2,2,1): 2, 2,1,1,1 , 1,1,1): 0}

To sum up, the thinking steps facing a problem:

Started:understand problems look at specification = if it make sense
Define the piece of problem reuse the piece you have test! >explore
Finally, the program is balanced in every way.
Correctness Elegance Efficienct Featrues

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.