因此,我一直在尝试编写一种功能,该功能将在纸牌游戏 Big 2 中将所有纸牌组合排列在手中。
在此游戏中,西服顺序为:Diamonds --> Clubs --> Hearts --> Spades
一只手通常看起来像这样:
hand = ['3D', '4D', '4H', '7D', '8D', '8H', '0D', '0C', 'JH', 'QC', 'QS', 'KH', 'AS']
完成所有可能的配对,三元组和单身后,我使用以下功能对其进行排名:
rank = {}
suits = {}
count = 3
suit = 0
for i in "34567890JQKA2":
rank[i] = count
count += 1
for i in "DCHS":
suits[i] = suit
suit += 2
def percentileCalc(card_input, pair):
n = 0
if pair:
for card in card_input:
if "S" in card:
n = 4
card = card_input[0]
else:
n = suits[card_input[1]]
card = card_input
percentile = math.floor(8 * (rank[card[0]] - 3) + n)
return percentile
def all_pairs(hand):
pairs = []
for i in itertools.combinations(hand, 2):
result = is_pair(i[0], i[1])
if result:
pairs.append(list(i))
return pairs
def all_triples(hand):
triples = []
for i in itertools.combinations(hand, 3):
result = is_triple(i[0], i[1], i[2])
if result:
triples.append(list(i))
return triples
def buildHand(hand):
tempList = []
pairs = ES.all_pairs(hand)
triples = ES.all_triples(hand)
final = []
for select in triples:
percentile = percentileCalc(select, True)
tempList.append([3, percentile, select])
for selection in pairs:
percentile = percentileCalc(selection, True)
tempList.append([2, percentile, selection])
for card in hand:
percentile = percentileCalc(card, False)
tempList.append([1, percentile, [card]])
tempList.sort(key=lambda x : x[1], reverse=True)
return tempList
如您所见,buildHand返回我的手可能拥有的所有可能的卡组合的列表,并且它们会相应地排名。
但是,当我尝试建立进一步的功能来构造所有可能的合法手牌(没有重复的牌/组合)并将它们的百分位数进行比较时,我最终合并在一起,无法提供正确数量的牌,也不合法。
我知道该函数很糟糕,但是我不知道该怎么做(假设有可能)。
当前功能是:
def constructor(combinations):
allHands = []
for comb in itertools.combination(combinations, 13):
comb = list(comb)
for i in comb:
card = i[1]
for permutes in comb:
if card in permutes:
comb.delete(permutes)
allHands.append(comb)
return allHands
此功能的几个问题:
hand_sizes
)它不能准确删除所有重复的卡组合,例如:
[["AS", "AH"], ["AS", "AC"]]
任何帮助将不胜感激! :)