获取偏好(或桥牌)游戏的所有可能交易变体

Get all possible deals variation for Preferance (or Bridge) game

有谁知道在 3 手牌中为偏好游戏(总卡数为 32。)生成所有可能和不同交易变体的最佳方法吗? 例如(其中 ('X', 'Y') 可以是任何牌 -> 例如 ('J', '♠') ):

第一笔交易:

1st hand -> (('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'))
2nd hand -> (('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'))
3rd hand -> (('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'), ('X', 'Y'))
The rest cards - -> (('X', 'Y')*13)
Trump -> (('X', 'Y'))

我的意思是: 第一手交易:

(
(('7', '♦'), ('8', '♥')), # edited line
(('9', '♦'), ('10', '♥')),
(('J', '♦'), ('Q', '♥')),
(('X', 'X')*13,
(('A', '♦'))
)

一手第二笔交易:

(
(('8', '♥'), ('7', '♦')), # edited line
(('9', '♦'), ('10', '♥')),
(('J', '♦'), ('Q', '♥')),
(('X', 'X')*13,
(('A', '♦'))
)

这两笔交易的定义类似于 ONLY 1 deal 但 NOT 2 因为实际上这手牌有相同的牌。

也可以使用“itertools.permutations”库解决,然后过滤所有相同的组合(就像我上面提到的),但我认为还有另一种方法更快?

我认为您应该使用“组合”而不是“排列”,因为您可以获得所有可能的唯一组合和数量。首先,您可以获得第一手的所有组合,然后将这种方法用于其他组合

deck = [('7', '♠'), ('7', '♣'), ('7', '♦'), ('7', '♥'), ('8', '♠'), ('8', '♣'), ('8', '♦'), ('8', '♥'), ('9', '♠'), ('9', '♣'), ('9', '♦'), ('9', '♥'), ('10', '♠'), ('10', '♣'), ('10', '♦'), ('10', '♥'), ('J', '♠'), ('J', '♣'), ('J', '♦'), ('J', '♥'), ('Q', '♠'), ('Q', '♣'), ('Q', '♦'), ('Q', '♥'), ('K', '♠'), ('K', '♣'), ('K', '♦'), ('K', '♥'), ('A', '♠'), ('A', '♣'), ('A', '♦'), ('A', '♥')]
i = 1
for item in itertools.combinations(deck, 7):
    print(item)
    print(i)
    i = i + 1

你也可以使用这样的方法:

def get_hands_from_deck(deck):
    hand_1, hand_2, rest_cards, trump = [], [], [], []
    for index, value in enumerate(deck):
        if index < 6:
            hand_1.append(value)
        if 6 <= index < 12:
            hand_2.append(value)
        if 12 <= index < 31:
            rest_cards.append(value)
        if index > 30:
            trump.append(value)
    return tuple(hand_1), tuple(hand_2), tuple(rest_cards), tuple(trump)

unique_deals = []
deck = [('A', '♠'), ('A', '♦'), ('10', '♠'), ('J', '♦'), ('J', '♣'), ('7', '♦'), ('10', '♥'), ('10', '♣'), ('7', '♠'), ('9', '♣'), ('8', '♥'), ('8', '♣'), ('7', '♥'), ('7', '♣'), ('8', '♦'), ('K', '♥'), ('J', '♥'), ('Q', '♥'), ('Q', '♠'), ('10', '♦'), ('J', '♠'), ('A', '♥'), ('9', '♠'), ('9', '♦'), ('Q', '♣'), ('9', '♥'), ('A', '♣'), ('8', '♠'), ('K', '♠'), ('Q', '♦'), ('K', '♦'), ('K', '♣')]
for item in itertools.permutations(deck, 32):
    split_deck_between_hands = get_hands_from_deck(item)
    if not unique_deals:
        unique_deals.append(split_deck_between_hands)
    if sort_cards(unique_deals[-1][0]) == sort_cards(split_deck_between_hands[0]) \
            and sort_cards(unique_deals[-1][1]) == sort_cards(split_deck_between_hands[1]) \
            and sort_cards(unique_deals[-1][2]) == sort_cards(split_deck_between_hands[2]) \
            and sort_cards(unique_deals[-1][3]) == sort_cards(split_deck_between_hands[3]):
        continue
    unique_deals.append(split_deck_between_hands)
    print(split_deck_between_hands)

但是您唯一应该做的就是创建“sort_cards”方法。从这里获取它

这是一个非常丑陋的解决方案,但有效:-)