Legend:
- Unmodified
- Added
- Removed
-
hand.py
r4 r5 1 1 import deck 2 2 3 hand_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 14 def 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]) 3 18 4 19 #for all these functions, make sure cards are sorted DESCENDING!!!! 20 #will return flush, straight or straight_flush 21 def 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 5 28 29 #we don't need to call this except from is_straight_flush 6 30 def is_flush(cards): 7 31 last=cards[0][1] … … 11 35 flush = False 12 36 break 13 if flush: return ["flush", cards[0][0]]#return highest card37 if flush: return ("flush", [cards[0][0]]) #return highest card 14 38 else: return None 15 39 40 #only is_straight_flush needs to call this 16 41 def is_straight(cards): 17 42 last=cards[0][0] … … 20 45 if a != last-1: 21 46 straight = False 22 last = a23 47 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 25 50 else: return None 26 51 27 def is_n_kinds(cards): 52 #input: only a list of the card vals. No suits please 53 def is_n_kinds(card_vals): 28 54 highest_n = 1 #greatest num in a row 29 55 current_n = 1 30 highest_card = None#highest card for greatest num in a row31 last = card s[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:]: 33 59 if a == last: 34 60 current_n += 1 … … 36 62 highest_n = current_n 37 63 highest_card = a 38 if highest_n == 1: return ["high"]+cards64 if highest_n == 1: return ("high_card",card_vals) 39 65 #check for full house 40 66 elif highest_n == 2: 67 whats_left = [x for x in card_vals if x != highest_card] 41 68 #this is the hand withotu the pairs 42 whats_left = [x for x in cards if x != highest_card]43 69 #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) 49 76 elif highest_n == 3: 50 whats_left = [x for x in card s if x != highest_card]77 whats_left = [x for x in card_vals if x != highest_card] 51 78 #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]) 54 82 #well, it's just a 3 of a kind then 55 else: return ["three_of_a_kind",highest_card]+whats_left83 return ("three_of_a_kind",[highest_card]+whats_left) 56 84 elif highest_n == 4: 57 whats_left = [x for x in card s if x != highest_card]58 return ["four_of_a_kind",highest_card]+whats_left85 whats_left = [x for x in card_vals if x != highest_card] 86 return ("four_of_a_kind",[highest_card]+whats_left) 59 87 #we shouldn't get to this point 60 88 assert(False)
