如何在 Python 中确定扑克牌中的顺子(顺序)
How to determine a straight (sequence) in a poker hand in Python
在 python 我有一个扑克手的分数跟踪器。
可能的卡牌等级:
A23456789TJQK
3 张或更多张牌的顺子每张牌得 1 分。 4 张牌的顺子示例给您 4 分。
所以我有一个分数:
score = 0
A sorted 字符串列表(所以我不必担心列表末尾的卡片有直开头)代表我的特定手牌的等级:
L4
['6', '8', '8', '9', 'T']
# Note: 8, 9, T is a straight. So I want to add 3 to score
但是我如何确定我是否有顺子?
到目前为止我尝试过的:
我尝试将字符串转换为整数,但我该如何处理 T、J、K、Q 和 A?它们不是数字。
使用字符串时出现错误:
for i in range(len(L4) - 1):
if(L4[i] is L4[i + 1] + 1):
sC = sC + 1
if(sC >= 3):
score += sC
L4
['6', '8', '8', '9', 'T']
Traceback (most recent call last):
File "myFile.py", line 66, in <module>
if(L4[i] is L4[i + 1] + 1):
TypeError: must be str, not int
我应该将它们转换为整数还是尝试其他方法?预先感谢您的帮助。
您可以找到当前手牌的所有子串,并过滤结果以找到以1
:
增量排序的那些
def stringify_result(f):
def wrapper(_d):
cards = {10:'T', 11:'J', 12:'Q', 13:'K'}
return list(map(lambda x:cards.get(x, str(x)), f(_d)))
return wrapper
@stringify_result
def has_straight(d):
cards = {'J': 11, 'K': 13, 'T': 10, 'Q': 12}
subs = list(filter(None, [d[b:i] for i in range(len(d)+1) for b in range(len(d)+1)]))
possibilities = list(filter(lambda x:all(x[i+1] - x[i] == 1 for i in range(len(x)-1)), [[int(cards.get(b, b)) for b in i] for i in subs]))
return [] if not possibilities else max(possibilities, key=len)
straight = has_straight(['6', '8', '8', '9', 'T'])
score = len(straight)
输出:
['8', '9', 'T']
3
编辑:考虑到多次运行,您可以使用 itertools.groupby
:
import itertools
def has_straight(d):
cards = {'J': 11, 'K': 13, 'T': 10, 'Q': 12}
mutated_cards = list(map(lambda x:int(cards.get(x, x)), d))
_grouped = [list(b) for _, b in itertools.groupby(mutated_cards)]
subs = list(filter(None, [_grouped[b:i] for i in range(len(_grouped)+1) for b in range(len(_grouped)+1)]))
final_filtered = list(filter(lambda x:all(x[i+1][0] - x[i][0] == 1 for i in range(len(x)-1)), subs))
new_subs = [] if not final_filtered else max(final_filtered, key=lambda x:sum(len(i) for i in x))
return sum(len(i) for i in new_subs if len(i) > 1), list(map(lambda x:x[0], new_subs))
print(has_straight(['6', '8', '8', '9', 'T']))
print(has_straight(['4', '4', '5', '5', '6']))
输出:
(2, [8, 9, 10])
(4, [4, 5, 6])
在 python 我有一个扑克手的分数跟踪器。
可能的卡牌等级:
A23456789TJQK
3 张或更多张牌的顺子每张牌得 1 分。 4 张牌的顺子示例给您 4 分。
所以我有一个分数:
score = 0
A sorted 字符串列表(所以我不必担心列表末尾的卡片有直开头)代表我的特定手牌的等级:
L4
['6', '8', '8', '9', 'T']
# Note: 8, 9, T is a straight. So I want to add 3 to score
但是我如何确定我是否有顺子?
到目前为止我尝试过的:
我尝试将字符串转换为整数,但我该如何处理 T、J、K、Q 和 A?它们不是数字。
使用字符串时出现错误:
for i in range(len(L4) - 1):
if(L4[i] is L4[i + 1] + 1):
sC = sC + 1
if(sC >= 3):
score += sC
L4
['6', '8', '8', '9', 'T']
Traceback (most recent call last):
File "myFile.py", line 66, in <module>
if(L4[i] is L4[i + 1] + 1):
TypeError: must be str, not int
我应该将它们转换为整数还是尝试其他方法?预先感谢您的帮助。
您可以找到当前手牌的所有子串,并过滤结果以找到以1
:
def stringify_result(f):
def wrapper(_d):
cards = {10:'T', 11:'J', 12:'Q', 13:'K'}
return list(map(lambda x:cards.get(x, str(x)), f(_d)))
return wrapper
@stringify_result
def has_straight(d):
cards = {'J': 11, 'K': 13, 'T': 10, 'Q': 12}
subs = list(filter(None, [d[b:i] for i in range(len(d)+1) for b in range(len(d)+1)]))
possibilities = list(filter(lambda x:all(x[i+1] - x[i] == 1 for i in range(len(x)-1)), [[int(cards.get(b, b)) for b in i] for i in subs]))
return [] if not possibilities else max(possibilities, key=len)
straight = has_straight(['6', '8', '8', '9', 'T'])
score = len(straight)
输出:
['8', '9', 'T']
3
编辑:考虑到多次运行,您可以使用 itertools.groupby
:
import itertools
def has_straight(d):
cards = {'J': 11, 'K': 13, 'T': 10, 'Q': 12}
mutated_cards = list(map(lambda x:int(cards.get(x, x)), d))
_grouped = [list(b) for _, b in itertools.groupby(mutated_cards)]
subs = list(filter(None, [_grouped[b:i] for i in range(len(_grouped)+1) for b in range(len(_grouped)+1)]))
final_filtered = list(filter(lambda x:all(x[i+1][0] - x[i][0] == 1 for i in range(len(x)-1)), subs))
new_subs = [] if not final_filtered else max(final_filtered, key=lambda x:sum(len(i) for i in x))
return sum(len(i) for i in new_subs if len(i) > 1), list(map(lambda x:x[0], new_subs))
print(has_straight(['6', '8', '8', '9', 'T']))
print(has_straight(['4', '4', '5', '5', '6']))
输出:
(2, [8, 9, 10])
(4, [4, 5, 6])