简体   繁体   English

找到所有可能的二十一点手的算法

[英]Algorithm to find all possible blackjack hands

I'm trying to write a function in Python that takes a list of card values and a list of values in the players hand and will return a list of all possible hands that the player can end up with that have a value of 17 or higher.我正在尝试用 Python 编写一个函数,该函数获取玩家手中的牌值列表和值列表,并将返回玩家最终可能得到的所有可能的手牌的列表,这些手牌的值是 17 或更高. The cards are represented by an integer that corresponds with the value of the card (2-10, aces are always 11. All face cards are worth 10).卡片由与卡片价值相对应的整数表示(2-10,A 总是 11。所有面卡价值 10)。 This is the function I have tried to write.这是我尝试编写的函数。 deck is a list of integers representing the cards that are still in the deck (3 cards are missing, 2 in the player's hand and one for the dealer). deck是一个整数列表,代表仍在牌组中的牌(缺少 3 张牌,2 张在玩家手中,一张给庄家)。 runninglist is a list of integers representing the cards in the player's hand. runninglist是一个整数列表,代表玩家手中的牌。 playerPossibilities is a list of hands that have a sum of 17 or higher. playerPossibilities是总playerPossibilities为 17 或更高的手牌列表。

def simPlayerHelper(deck, runningList, playerPossibilities):
    for card in deck:    
        runningList.append(card)
        if sum(runningList) > 16:
            temp_rl = runningList.copy()
            playerPossibilities.append(temp_rl)
            runningList.remove(card)
        else:
            deck.remove(card)
            simPlayerHelper(deck, runningList, playerPossibilities)
    for x in playerPossibilities:
        print(x)

The recursion is not working correctly.递归无法正常工作。 For example, if runningList starts with "10" and "2", then the first card in deck will be another 2. Another card is drawn, because we don't have 17 yet, and it's another 2. This is fine.例如,如果runningList以“10”和“2”开头,那么runningList card中的第一card deck将是另外一张 2。另一张牌被抽出,因为我们还没有 17,而它又是另外一张 2。这很好。 We have 16 now, so another card is taken from the deck, which is the last 2 (the cards are in order).我们现在有 16 张,所以从牌组中取出另一张牌,也就是最后 2 张(牌按顺序)。 We now have 18, so [10,2,2,2,2] is added to playerPossibilities .我们现在有 18 个,所以 [10,2,2,2,2] 被添加到playerPossibilities Then [10,2,2,2,3] (four times for four 3's in the deck) is added, [10,2,2,2,4] (four times), and so on.然后添加 [10,2,2,2,3](四次,代表牌组中的四个 3),[10,2,2,2,4](四次),依此类推。 That is fine.那也行。 But now I need it to go back to the fourth card in the hand and fine all possibilities if that were to be a 3, then 4, then 5, and so on.但现在我需要它回到手上的第四张牌,并确定所有可能性,如果它是 3,然后是 4,然后是 5,依此类推。 And then back to the third card, and find the possibilities if that card were a 3, then 4, then 5, for all remaining cards in the deck.然后回到第三张牌,如果这张牌是 3,然后是 4,然后是 5,对于牌组中的所有剩余牌,找出可能性。 Right now, it is only looping through the last card in the hand, so all items in playerPossibilities start with 10,2,2,2.现在,它只遍历手上的最后一张牌,因此playerPossibilities所有项目都以playerPossibilities开头。

I'm sorry if this explanation wasn't very clear.如果这个解释不是很清楚,我很抱歉。 I'm stuck so any help on how I can get this recursion working properly would be greatly appreciated.我被卡住了,所以任何关于如何让这个递归正常工作的帮助将不胜感激。 Thank you!谢谢!

There are a number of problems here.这里有很多问题。

  1. for card in deck - you are iterating over a list that you are then removing objects from. for card in deck - 您正在迭代一个列表,然后从中删除对象。 This is a good way to have things not work correctly.这是使事情无法正常工作的好方法。

  2. if // else - You draw a card, put it in your hand and check for your win condition. if // else - 你抽一张牌,把它放在你的手上并检查你的获胜条件。 If above 16, record the answer, remove from your hand and continue.如果高于 16,请记录答案,从您的手中移开并继续。 Great!伟大的! If not above 16, do your recursion...but you have no mechanism for removing the card from your hand after the recursion is finished.如果不超过 16,请执行递归……但是在递归完成后,您没有将卡从手上移走的机制。 (I believe this to be the source of your current bug) (我相信这是您当前错误的来源)

To solve this, for this kind of problem, you probably want to make a copy of "deck" in your recursive function so that child calls can't pull cards out of your parent deck (where you haven't checked them yet).为了解决这个问题,对于此类问题,您可能希望在递归函数中复制“deck”,以便子调用无法从父甲板(您尚未检查它们)中拉出卡片。 You also want to make sure that the card is removed from your hand after each iteration.您还需要确保每次迭代后都将卡片从您的手中移除。 I would probably suggest doing your loop like this我可能会建议像这样做你的循环

def run_sims(deck):
  current_deck = copy(deck)
  while current_deck:
    card = deck.pop()
    current_hand.append(card)
    # do logic / recurse
    current_hand.pop(card)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM