循环故障时的二十一点

时间:2017-11-10 15:38:06

标签: python random while-loop blackjack

因此,我正在制作一个二十一点模拟器来查找某些手牌获胜与否的概率。我有一个循环循环通过一副52张牌,直到剩下4张牌。在那之内我有一个循环经销商抽奖,直到他的数量是17+就像真正的二十一点。当我运行代码时,它将成功运行一个牌组,但每次只打印同一手牌。这是代码:

# <--- PROGRAM SETUP --->
import random

# Drawing Card Function
def draw_card():
    # Card Values in 1 Deck
    vals = [2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,
            8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,
            10,10,10,10,10,10,10,10,10,11,11,11,11]
    # </--->

    dealer = 0
    player = 0
    bust = 21


    deck_count = 0
    hand_count = 0

    dealer_cards = []

    # <--- // --->

    cards = 52
    # Loop Hands Through Deck
    while(cards > 4):

        # Draw Card Loop
        while(dealer <= 16):
            card = random.choice(vals)
            vals.remove(card)
            dealer_cards.append(card)

            dealer = dealer + card

        hand_count = hand_count + 1
        cards = cards - len(dealer_cards)


        # <--- Results --->
        if 17 <= dealer <= 21:
            print "Hand: {}, Dealer Stands @ {}".format(hand_count, dealer)
            for i in dealer_cards:
                print "Card: {}".format(i)

        else:
            print "Hand: {}, Dealer Busts @ {}".format(hand_count, dealer)
            for i in dealer_cards:
                print "Card: {}".format(i)

    else:
        deck_count = deck_count + 1
        print "Deck {} is finished!".format(deck_count)

# <--- // --->

draw_card()

运行时,我得到:

    Hand: 1, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 2, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 3, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 4, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 5, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 6, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 7, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 8, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 9, Dealer Stands @ 19
Card: 10
Card: 9
Hand: 10, Dealer Stands @ 19
...All the way till the deck has less than 4 cards left.

我可以修复哪一只手是一个独特的平局,而不是多次打印第一手牌?感谢

2 个答案:

答案 0 :(得分:1)

您实际上并没有在循环迭代之间清除经销商的手。

所以,他可以画一张牌,因为他已经达到了19,永远挂在它上面,跳过内部的while dealer < 17循环并抓住你所有的虚拟货币,因为他知道他有足够的手

如果/ else检查,只需在结果后设置dealer = 0

答案 1 :(得分:1)

您忘记在每次迭代前重置dealerdealer_cards,因此您的while(dealer <= 16):块将在第一次之后永远不会运行;它将继续使用相同的分数,但继续减少cards

将初始化移至主while循环:

while(cards > 4):
    dealer_cards = []
    dealer = 0

    # Draw Card Loop
    while(dealer <= 16):
        card = random.choice(vals)
        vals.remove(card)
        dealer_cards.append(card)

        dealer = dealer + card

...