Changeset 5 for hand.py

Show
Ignore:
Timestamp:
01/23/08 08:23:37 (4 years ago)
Author:
mike
Message:

Wrote hand detection code
now, just need to write comparison code.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • hand.py

    r4 r5  
    11import deck 
    22 
     3hand_orders = ["straight_flush", 
     4                "four_of_a_kind", 
     5                "full_house", 
     6                "flush", 
     7                "straight", 
     8                "three_of_a_kind", 
     9                "two_pair", 
     10                "pair", 
     11                "high_card"] 
     12 
     13#returns the best combination for the 5 cards 
     14def judge_hand(cards): 
     15    ret = is_straight_flush(cards) 
     16    if ret: return ret 
     17    else: return is_n_kinds([a for (a,b) in cards]) 
    318 
    419#for all these functions, make sure cards are sorted DESCENDING!!!! 
     20#will return flush, straight or straight_flush 
     21def is_straight_flush(cards): 
     22    ret1 = is_flush(cards) 
     23    ret2 = is_straight(cards) 
     24    if ret1 and ret2: return ("straight_flush", [cards[0][0]]) 
     25    elif ret1: return ret1 
     26    elif ret2: return ret2 
     27    else: return None 
    528 
     29#we don't need to call this except from is_straight_flush 
    630def is_flush(cards): 
    731    last=cards[0][1] 
     
    1135            flush = False 
    1236            break 
    13     if flush: return ["flush", cards[0][0]] #return highest card 
     37    if flush: return ("flush", [cards[0][0]]) #return highest card 
    1438    else: return None 
    1539 
     40#only is_straight_flush needs to call this 
    1641def is_straight(cards): 
    1742    last=cards[0][0] 
     
    2045        if a != last-1: 
    2146            straight = False 
    22             last = a 
    2347            break 
    24     if straight: return ["straight", cards[0][0]] #return highest card 
     48        last = a 
     49    if straight: return ("straight", [cards[0][0]]) #return highest card 
    2550    else: return None 
    2651 
    27 def is_n_kinds(cards): 
     52#input: only a list of the card vals.  No suits please 
     53def is_n_kinds(card_vals): 
    2854    highest_n = 1   #greatest num in a row 
    2955    current_n = 1 
    30     highest_card = None  #highest card for greatest num in a row 
    31     last = cards[0][0] 
    32     for (a,b) in cards[1:]: 
     56    highest_card = card_vals[0]  #highest card for greatest num in a row 
     57    last = card_vals[0] 
     58    for a in card_vals[1:]: 
    3359        if a == last: 
    3460            current_n += 1 
     
    3662                highest_n = current_n 
    3763                highest_card = a 
    38     if highest_n == 1: return ["high"]+cards 
     64    if highest_n == 1: return ("high_card",card_vals) 
    3965    #check for full house 
    4066    elif highest_n == 2: 
     67        whats_left = [x for x in card_vals if x != highest_card] 
    4168        #this is the hand withotu the pairs 
    42         whats_left = [x for x in cards if x != highest_card] 
    4369        #now check if it's two pair 
    44         ret = is_n_kinds(whats_left) 
    45  
    46         #if the subset is a pair then this is a two pair 
    47         if ret[0] == "pair": return ["two_pair",highest_card]+ret[1:]  
    48         else: return ["pair",highest_card]+whats_left 
     70        if len(whats_left) >= 2: 
     71            print highest_card 
     72            ret = is_n_kinds(whats_left) 
     73            #if the subset is a pair then this is a two pair 
     74            if ret[0] == "pair": return ("two_pair",[highest_card]+ret[1]) 
     75        return ("pair",[highest_card]+whats_left) 
    4976    elif highest_n == 3: 
    50         whats_left = [x for x in cards if x != highest_card] 
     77        whats_left = [x for x in card_vals if x != highest_card] 
    5178        #now check if it's a full house 
    52         ret = is_n_kinds(whats_left) 
    53         if ret[0] == "pair": return ["full_house",highest_card]+ret[1:]  
     79        if len(whats_left) >= 2: 
     80            ret = is_n_kinds(whats_left) 
     81            if ret[0] == "pair": return ("full_house",[highest_card]+ret[1])  
    5482        #well, it's just a 3 of a kind then 
    55         else: return ["three_of_a_kind",highest_card]+whats_left 
     83        return ("three_of_a_kind",[highest_card]+whats_left) 
    5684    elif highest_n == 4: 
    57         whats_left = [x for x in cards if x != highest_card] 
    58         return ["four_of_a_kind",highest_card]+whats_left 
     85        whats_left = [x for x in card_vals if x != highest_card] 
     86        return ("four_of_a_kind",[highest_card]+whats_left) 
    5987    #we shouldn't get to this point 
    6088    assert(False)