![](/img/trans.png)
[英]In python how can you sort a hand of poker (list) and detect if it is a straight or a royal flush
[英]Poker - Finding if hand is straight flush?
我決定通過創建一個撲克游戲來挑戰自己。 我目前對元組感到非常困惑,並試圖正確使用它們。 我目前正試圖檢查手 (h) 是否是同花順。 下面是我用來創建一副牌的代碼。
numbers=(2,3,4,5,6,7,8,9,10,'J','Q','K','A')
faces=("Clubs","Diamond","Spades","Harts")
print(numbers[0],faces[0])
deck=[]
for i in numbers:
for j in faces:
deck.append((j,i))
hand=d[1],d[5],d[9],d[13],d[17]
上面的手牌是(('Diamond', 2), ('Diamond', 3), ('Diamond', 4), ('Diamond', 5), ('Diamond', 6))
。
下面是我遇到問題的代碼。 目前只能檢查所有面是否相同(可以檢查Diamond是否是h中所有卡片的面),但不能檢查數字是否與數字順序一致。 我也想做這樣A
可以循環,就像手一樣(('Diamond', 2), ('Diamond', 3), ('Diamond', 4), ('Diamond', 5), ('Diamond', A))
仍然是有效順子。 我知道我可以使用數字來檢查序列,但我試圖只使用甲板來做到這一點。
def straight_flush(h):
r=True
for (i,j) in h:
for (a,b) in h:
if i==a:
r=True
else:
r=False
return r
print(straight_flush(h))
首先,使用數字作為面卡。 這更容易。 另外,您應該制作一張卡片 class。
關於同花順,你需要檢查這手牌是否同花,然后檢查同花牌是否為順子。 使用 5 張牌(而不是像 Hold'em 中的 7 張)意味着您只需要檢查是否有順子和同花。
同花很容易被發現,只要看任何花色的數量是否≥5。 直道有點難。 可能最簡單的方法是在所有直線中手動編碼(即檢測手是否與代表可能直線的預設元組匹配)。
還有一些用於檢測直道的算法。 這是我為讓您了解結構而制作的一個項目中的一個:
def straight(cls, vset, get_vals=False):
"""Returns the name of a straight hand (string) given a set of the hand's card values.
Returns False if a straight is not present within the hand. Also changes hand strength accordingly.
If get_vals is true, straight() does not change strength and returns the values present in a straight."""
count = 0
if not get_vals:
straight = False
for rank in reversed([14, *range(2, 15)]):
if rank in vset:
count += 1
min_c = rank
if count == 5:
if min_c != 14:
max_c = min_c + 4
else:
min_c, max_c = 1, 5
cls.strength = BaseStrength.STRAIGHT.value + 70*max_c
straight = f'Straight from {value_names[min_c]} to {value_names[max_c]}'
break
else: count = 0
return straight
忽略get_vals
。 我的算法不是最有效的(可能遠非如此)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.