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:
unt(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
python新手代码示例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 = [(unt(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实现⽐较扑克牌⼤⼩程序代码⽰例的全部内容,希望对⼤家有所帮助。感兴趣的朋友可以继续参阅本站:
如有不⾜之处,欢迎留⾔指出。感谢朋友们对本站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论