考虑长度为7的排序列表,其中每个条目x是数字2 <= x <= 14且任何条目的重复计数不能超过4.这里的假设是高性能算法已经确定我们有一个非直/非冲洗手。
在线扑克网站会对高性能算法感兴趣,这些算法负责在这种情况下获得5张最佳牌的下一步。
在Python中编写一个不导入模块的程序是制作这些算法原型的好方法。
很可能“怪物大小”在线扑克网站不需要我们的任何帮助,但看到为速度设计的算法会很有趣。在问题中
从2010年开始对此进行了检查,但很多链接都被打破了。很高兴知道今天使用的最快的已知算法的状态。
问题:下面讨论的算法是否已知?是否有一些算法被确定为性能出众?
我的工作
我注意到长度为7的列表有一个中点,并且存在算法可以合并的“组合对称性”和结构。我们在以下代码中实现此逻辑。人们可以想到用汇编程序编写的闪电快速程序,用解决方案计算GOTO
数偏移量。
注意:我还有一个单程排序程序,它可以接收任何7张牌并确定是否可以进行直接或冲洗。但我被建议让我的问题更集中,所以这里不讨论。
Python程序:
hand=[2,2,7,7,8,11,12]
hand=[2,3,4,7,7,7,11]
start_spot = 3
end_spot = 3
if hand[3] == hand[4]:
if hand[4] == hand[5]:
if hand[5] == hand[6]:
end_spot = 6
else:
end_spot = 5
else:
end_spot = 4
if hand[3] == hand[2]:
if hand[2] == hand[1]:
if hand[1] == hand[0]:
start_spot = 0
else:
start_spot = 1
else:
start_spot = 2
if end_spot - start_spot == 3:
if end_spot == 6:
Kick = hand[start_spot-1]
else:
Kick = hand[6]
best5 = [Kick,hand[start_spot],hand[start_spot+1],hand[start_spot+2],hand[start_spot+3]]
print(hand, best5, 'four of a kind')
raise SystemExit
else:
pass
def multCount(c1,c2,c3):
pc = 0
if c1 == c2: pc = pc + 1
if c2 == c3: pc = pc + 10
return pc
pc_r = multCount(hand[4],hand[5],hand[6])
pc_l = multCount(hand[2],hand[1],hand[0])
if start_spot == 3 and end_spot == 3:
if pc_l == 0 and pc_r == 0:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'no pair')
raise SystemExit
elif pc_l == 0 and pc_r == 1:
best5 = [hand[2],hand[3],hand[6],hand[4],hand[5]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 0 and pc_r == 10:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 0 and pc_r == 11:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'trips')
raise SystemExit
elif pc_l == 1 and pc_r == 0:
best5 = [hand[4],hand[5],hand[6],hand[1],hand[2]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 1 and pc_r == 1:
best5 = [hand[6],hand[1],hand[2],hand[4],hand[5]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 1 and pc_r == 10:
best5 = [hand[4],hand[1],hand[2],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 1 and pc_r == 11:
best5 = [hand[1],hand[2],hand[4],hand[5],hand[6]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 10 and pc_r == 0:
best5 = [hand[4],hand[5],hand[6],hand[0],hand[1]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 10 and pc_r == 1:
best5 = [hand[6],hand[0],hand[1],hand[4],hand[5]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 10:
best5 = [hand[4],hand[0],hand[1],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 11:
best5 = [hand[0],hand[1],hand[4],hand[5],hand[6]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11 and pc_r == 0:
best5 = [hand[5],hand[6],hand[0],hand[1],hand[2]]
print(hand, best5, 'trips')
raise SystemExit
elif pc_l == 11 and pc_r == 1:
best5 = [hand[4],hand[5],hand[0],hand[1],hand[2]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11 and pc_r == 10:
best5 = [hand[5],hand[6],hand[0],hand[1],hand[2]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11 and pc_r == 11:
best5 = [hand[1],hand[2],hand[4],hand[5],hand[6]]
print(hand, best5, 'full house')
raise SystemExit
else:
pass
if start_spot == 3 and end_spot == 4:
if pc_l == 0 and pc_r == 0:
best5 = [hand[2],hand[5],hand[6],hand[3],hand[4]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 0 and pc_r == 1:
print("ERROR 1")
pass # can't happen
raise SystemExit
elif pc_l == 0 and pc_r == 10:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 0 and pc_r == 11:
print("ERROR 2")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 0:
best5 = [hand[6],hand[1],hand[2],hand[3],hand[4]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 1 and pc_r == 1:
print("ERROR 3")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 10:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 1 and pc_r == 11:
print("ERROR 4")
pass # can't happen
raise SystemExit
elif pc_l == 10 and pc_r == 0:
best5 = [hand[6],hand[0],hand[1],hand[3],hand[4]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 1:
print("ERROR 5")
pass # can't happen
raise SystemExit
elif pc_l == 10 and pc_r == 10:
best5 = [hand[4],hand[0],hand[1],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 11:
print("ERROR 6")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 0:
best5 = [hand[3],hand[4],hand[0],hand[1],hand[2]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11 and pc_r == 1:
print("ERROR 7")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 10:
best5 = [hand[5],hand[6],hand[0],hand[1],hand[2]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11 and pc_r == 11:
print("ERROR 8")
pass # can't happen
raise SystemExit
else:
pass
if start_spot == 2 and end_spot == 3:
if pc_l == 0 and pc_r == 0:
best5 = [hand[4],hand[5],hand[6],hand[2],hand[3]]
print(hand, best5, 'one pair')
raise SystemExit
elif pc_l == 0 and pc_r == 1:
best5 = [hand[6],hand[2],hand[3],hand[4],hand[5]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 0 and pc_r == 10:
best5 = [hand[4],hand[2],hand[3],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 0 and pc_r == 11:
print("ERROR 9")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 0:
print("ERROR 10")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 1:
print("ERROR 11")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 10:
print("ERROR 12")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 11:
print("ERROR 13")
pass # can't happen
raise SystemExit
elif pc_l == 10 and pc_r == 0:
best5 = [hand[6],hand[0],hand[1],hand[2],hand[3]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 1:
best5 = [hand[6],hand[2],hand[3],hand[4],hand[5]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 10:
best5 = [hand[4],hand[2],hand[3],hand[5],hand[6]]
print(hand, best5, 'two pair')
raise SystemExit
elif pc_l == 10 and pc_r == 11:
print("ERROR 14")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 0:
print("ERROR 15")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 1:
print("ERROR 16")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 10:
print("ERROR 17")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 11:
print("ERROR 18")
pass # can't happen
raise SystemExit
else:
pass
if start_spot == 2 and end_spot == 4:
if pc_l == 0 and pc_r == 0:
best5 = [hand[5],hand[6],hand[2],hand[3],hand[4]]
print(hand, best5, 'trips')
raise SystemExit
elif pc_l == 0 and pc_r == 1:
print("ERROR 19")
pass # can't happen
raise SystemExit
elif pc_l == 0 and pc_r == 10:
best5 = [hand[5],hand[6],hand[2],hand[3],hand[4]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 0 and pc_r == 11:
print("ERROR 20")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 0:
print("ERROR 21")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 1:
print("ERROR 22")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 10:
print("ERROR 23")
pass # can't happen
raise SystemExit
elif pc_l == 1 and pc_r == 11:
print("ERROR 24")
pass # can't happen
raise SystemExit
elif pc_l == 10 and pc_r == 0:
best5 = [hand[0],hand[1],hand[2],hand[3],hand[4]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 10 and pc_r == 1:
print("ERROR 25")
pass # can't happen
raise SystemExit
elif pc_l == 10 and pc_r == 10:
best5 = [hand[5],hand[6],hand[2],hand[3],hand[4]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 10 and pc_r == 11:
print("ERROR 26")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 0:
print("ERROR 27")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 1:
print("ERROR 28")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 10:
print("ERROR 29")
pass # can't happen
raise SystemExit
elif pc_l == 11 and pc_r == 11:
print("ERROR 30")
pass # can't happen
raise SystemExit
else:
pass
if start_spot == 1 and end_spot == 3:
if pc_r == 0:
best5 = [hand[5],hand[6],hand[1],hand[2],hand[3]]
print(hand, best5, 'trips')
raise SystemExit
elif pc_r == 1:
best5 = [hand[4],hand[5],hand[1],hand[2],hand[3]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_r == 10:
best5 = [hand[5],hand[6],hand[1],hand[2],hand[3]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_r == 11:
best5 = [hand[2],hand[3],hand[4],hand[5],hand[6]]
print(hand, best5, 'full house')
raise SystemExit
else:
pass
if start_spot == 3 and end_spot == 5:
if pc_l == 0:
best5 = [hand[2],hand[6],hand[3],hand[4],hand[5]]
print(hand, best5, 'trips')
raise SystemExit
elif pc_l == 1:
best5 = [hand[1],hand[2],hand[3],hand[4],hand[5]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 10:
best5 = [hand[0],hand[1],hand[3],hand[4],hand[5]]
print(hand, best5, 'full house')
raise SystemExit
elif pc_l == 11:
best5 = [hand[1],hand[2],hand[4],hand[5],hand[6]]
print(hand, best5, 'full house')
raise SystemExit
else:
pass
print("ERROR 99")
pass # can't happen
raise SystemExit
我相信我已经实现了你指定的算法,但它很容易出错,所以如果有任何问题请告诉我。我只是做了四个核心评估
从这些中我确定5张牌的特定组合可以在一系列if语句中做出的最佳牌,并相应地对牌进行排序。然后我从一组7中创建所有5张牌组合的列表,使用此逻辑确定最大/最佳牌(我重新排列得分和手牌以完成此操作),并返回该牌。
from collections import Counter
from itertools import combinations
def num_of_kind(cards):
return Counter(c[0] for c in cards)
def count_pairs(cards):
return sum(i > 1 for i in num_of_kind(cards).values())
def largest_pair(cards):
return max(num_of_kind(cards).values())
def is_straight(cards):
values = [c[0] for c in cards]
index = "A23456789TJQKA"["K" in values:].index
indices = sorted(index(v) for v in values)
return all(x == y for x, y in enumerate(indices, indices[0]))
def is_flush(cards):
suit_pop = Counter(c[1] for c in cards)
return any(s > 4 for s in suit_pop.values())
def straight_sort(cards):
values = [c[0] for c in cards]
index = "A23456789TJQKA"["K" in values:].index
return sorted(cards, key=lambda x:index(x[0]), reverse=True)
def flush_sort(cards):
suit_pop = Counter(c[1] for c in cards)
return sorted(cards, key=lambda x: suit_pop[x[1]], reverse=True)
def pair_sort(cards):
num = num_of_kind(cards)
return sorted(cards, key=lambda x: num[x[0]], reverse=True)
def card_vals(cards):
return [c[0] for c in cards]
def score_hand(cards):
pairs = count_pairs(cards)
largest = largest_pair(cards)
straight = is_straight(cards)
flush = is_flush(cards)
cards = straight_sort(cards)
hand_score = 0
if flush and straight:
hand_score, cards = 8, flush_sort(cards)
elif largest == 4:
hand_score, cards = 7, pair_sort(cards)
elif pairs == 2 and largest == 3:
hand_score, cards = 6, pair_sort(cards)
elif flush:
hand_score, cards = 5, flush_sort(cards)
elif straight:
hand_score = 4
elif largest == 3:
hand_score, cards = 3, pair_sort(cards)
else:
hand_score, cards = pairs, pair_sort(cards)
return hand_score, card_vals(cards), cards
def best_hand(cards):
cards = max(list(combinations(cards, 5)), key=score_hand)
score, _, hand = score_hand(cards)
return hand[::-1], score
def main():
hand = ['2c','Ah','3d', '5s','4h','5d', '3h']
print(*best_hand(hand))
if __name__ == "__main__":
main()
我可以向你介绍每种方法的细节,但我觉得这一切都是相当不言自明的。唯一棘手的一点是在is_straight()
,这只是一些索引技巧,以确定卡值是否可以按顺序排序。
在设计算法以找到最佳手时,您可以考虑以下一些想法:
itertools
模块的技巧。Card
的课程,并将您的卡片作为“卡片”类型的对象提供。然后你可以让班级中的函数返回卡片的套装或数值。