简体   繁体   English

Python Blackjack Game如何在while循环中携带变量

[英]Python Blackjack Game how to carry a variable in a while loop

I'm still deep into the making of my Blackjack game in Python, my aim is to allow the computer to 'twist' (Take another card) until the total value is greater than 15. From then onwards I want it so that as the total of the cards gets higher, the chance of the computer taking another card gets smaller just like a human (taking a risk if the number isn't TOO big).The following code occurs after the computer has been dealt two values. 我仍在用Python制作二十一点游戏,我的目标是让计算机“扭曲”(换另一张卡),直到总值大于15。从那时起,我希望它成为卡的总数越高,计算机拿走另一张卡的机会就越小,就像人一样(如果数量不是太大,则要冒风险)。对计算机进行两个值处理后,将发生以下代码。

if (computerCardValueTotal < 15): print ("The computer has decided to twist") ranCompCard3 = random.choice(availableCards) computerCardValueTotal = ranCompCard + ranCompCard2 + ranCompCard3 if (computerCardValueTotal < 15): print ("The computer has chosen to twist again")

My aim is to have this piece of code loop if the total value is less than 15 so that the computer twists until it is over 15. I have considered using a while loop but I'm unsure on how to carry the current total to the start of the loop so that the next card value is added to the current total. 我的目标是在总值小于15的情况下进行这段代码循环,以使计算机扭曲直到超过15。我曾考虑过使用while循环,但不确定如何将当前总数传递给循环开始,以便将下一张卡的值添加到当前总数中。 Does anyone have a solution they could help me out with? 有没有人可以帮助我的解决方案?

Also, secondly, it's not the biggest of the two issues in this question, but how would you recommend doing the computer so that the chance of it twisting again is smaller as the total value of cards get bigger? 另外,第二,它不是这个问题中两个问题中最大的问题,但是您将如何建议您使用计算机,以便随着卡的总价值变大而使其再次扭曲的机会变小? For example, for if the value of the cards totals at 17, there's a 1/10 chance of the computer twisting but if the total value of the cards is 19 the chance of the computer twisting is 1/20. 例如,如果卡的总价值为17,则有1/10的机会发生计算机扭曲,但如果卡的总值为19,则有可能发生计算机扭曲。

All valuable help will be voted up and as always, thanks in advance! 我们将一如既往地对所有宝贵的帮助进行投票,在此先感谢您! :) :)

You have hobbled yourself by making eg ranCompCard , ranCompCard2 , ranCompCard3 , ... when a list would make life much easier: 您通过制作ranCompCardranCompCard2ranCompCard3 ,...来使自己步履蹒跚, ranCompCard3 list将使生活变得更加轻松:

compCards = [ranCompCard, ranCompCard2]
while sum(compCards) < 15:
    compCards.append(random.choice(availableCards))

For adjusting the probability of picking another card, you could do something like: 为了调整领取另一张牌的可能性,您可以执行以下操作:

while True:
    gap = 21 - sum(compCards)
    if gap > 0 and random.random() < (1 / (40 / float(gap))):
        compCards.append(random.choice(availableCards))
    else:
        break

Note that this is a linear relationship between gap and the probability of picking another card: 请注意,这是gap与选择另一张牌的可能性之间的线性关系:

>>> for card_sum in range(15, 21):
    print card_sum, 1 / (40 / float(21 - card_sum))


15 0.15
16 0.125
17 0.1
18 0.075
19 0.05
20 0.025

but you can adjust the equation as needed. 但是您可以根据需要调整方程式。


While you're editing your code, take a look at (and consider following) the official style guide ; 在编辑代码时,请查看(并考虑遵循) 官方样式指南 variable names should be lowercase_with_underscores . 变量名称应为lowercase_with_underscores

Simply reassign the value when it changes. 只需在值更改时重新分配它。

while computerCardValueTotal < 15:
  # other code
  computerCardValueTotal = <new value>

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

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