Python实现比较扑克牌大小程序代码示例
是Udacity课程的第一个项目。
先从宏观把握一下思路,目的是做一个比较德州扑克大小的问题
首先,先抽象出一个处理的函数,它根据返回值的大小给出结果。
之后我们在定义如何比较两个或者多个手牌的大小,为方便比较大小,我们先对5张牌进行预处理,将其按照降序排序,如下:
def card_ranks(hand): ranks = ['--23456789TJQKA'.INDEX(r) for r, s in hand] ranks.sort(reverse=True) return ranks
然后我们可以枚举出一共有9种情况,并用数字代表每一种情况的等级,利用Python的比较功能,将等级放在第一位,如果等级相同,那么再比较后面的。
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)
可以看到,如果等级相同,接下来比较的是每套牌中牌的大小了。同时我们需要三个函数,代表同花,顺子,以及kind(n, ranks),代表ranks有n张牌的点数。这里的三个函数实现非常巧妙,利用了set去重的特性。
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
我们发现,有一种情况是含有两个对,于是需要一个函数来判断是否是这种情况,这个函数中调用了kind()函数,由于kind()函数满足短路特性,只会返回先得到的满足情况的点数,于是将其翻转后,在调用一边kind,若得到的结果相同,那么就只有一个对(或者没有),否则就有两个。
def two_pairs(ranks): pair = kind(2, ranks) lowpair = kind(2, list(reverse(ranks))) if pair != lowpair: return (pair, lowpair) else: return None
好了,整体的骨架算是搭完了,接下来处理会产生bug的情况,首先是A2345,当排序时由于A被算作14,所以针对这个问题需要单独列一个if
处理A是最低: 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 = [14, 5, 4, 3, 2] else ranks
之后就是进一步的简化了,思路挺好的
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], xval elif: result.append(x) return result """大于就取代,等于就加入,小于不作处理""" import random mydeck = [r+s for r in '23456789TJKQA' for s in'SHDC] def deal(numhands, n=5, deck = [r+s for r in '23456789TJKQA' for s in'SHDC]): random.shuffle(deck) return [deck[n*i:n*(i + 1)] for i in range(numhands)] def hand_ranks(hand): groups = group['--23456789TJQKA'.index(r) for r, s in hand] counts, ranks = unzip(groups) if rnaks == (14, 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(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), ranks def group(items): groups = [(items.count(x), x) for x in 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 == (14, 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[counts], 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,1,1,1):0}
总结下,面对一个问题的思维步骤:
started:understand problems look at specification See if it make sense define the piece of problem reuse the piece you have test! >explore 最后是是的程序在各个方面达到均衡 correctness elegance efficienct featrues
总结
以上就是本文关于Python实现比较扑克牌大小程序代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!