有没有人知道最佳的方法,以产生所有可能的和不同的交易为优惠游戏(总卡数量为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', '♦'))
)这两种交易的定义都类似于,1和,而不是2,因为实际上手上有相同的牌。
另外,它可以使用"itertools.permutations“库进行解析,然后过滤所有相同的组合(就像我前面提到的),但是我认为还有另一种方法更快吗?
发布于 2021-12-18 21:28:28
我认为你应该使用“组合”而不是“排列”,因为你得到了所有可能的独特组合和数量。一开始,你可以得到第一只手的所有组合,而其他的则使用这样的方法。
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“方法。从这里拿来吧,Sort cards according to its suite and values using python
这是相当难看的解决方案,但有效:-)
https://stackoverflow.com/questions/70403481
复制相似问题