Python 21点模拟器(要检查)

Python 21点模拟器(要检查),python,random,python-2.7,nested-loops,Python,Random,Python 2.7,Nested Loops,我写了一个21点模拟器。目标是估计经销商破产的可能性。请注意,经销商必须在16时提取,并且必须站在17时提取。如果经销商的手牌包含ace,则当总数介于17和21之间时,应将其计为11;否则,ace应计为1。这个计划似乎正在运行,但我不能肯定。这是该计划的核心。你能检查一下代码是否有缺陷吗 def simNGames(n): holds = busts = 0 for i in range(n): score = simOneGame() if sc

我写了一个21点模拟器。目标是估计经销商破产的可能性。请注意,经销商必须在16时提取,并且必须站在17时提取。如果经销商的手牌包含ace,则当总数介于17和21之间时,应将其计为11;否则,ace应计为1。这个计划似乎正在运行,但我不能肯定。这是该计划的核心。你能检查一下代码是否有缺陷吗

def simNGames(n):
    holds = busts = 0
    for i in range(n):
        score = simOneGame()
        if score <= 21:
            holds += 1
        else:
            busts += 1
    return holds, busts

def simOneGame():
    score = 0
    cardsVal = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
    while not gameOver(score):
        dealerScore = choice(cardsVal)

        # in case dealer hits an ace
        if dealerScore == 11:
            if score >= 6 and score <= 10:
                score += 11
            else:
                score += 1
        else:
            score += dealerScore

    return score

def gameOver(score):
    return score >= 17 and score <= 21 or score >=22
def simNGames(n):
保持=爆破=0
对于范围(n)中的i:
分数=simOneGame()
如果分数=6,分数=17,分数=22

总的来说,逻辑似乎很好


我的建议是针对您担心的具体情况写一些。

好的,谢谢您阅读本文。谢谢你的建议,我还没有学习单元测试。如果你连续模拟了很多游戏,你应该考虑到这样一个事实:牌组中的牌已经用完了,庄家抽的特定牌(庄家不太可能再抽同一张牌)。如果你模拟一小部分游戏,那么这个效果在很大程度上可以忽略,因为21点通常是用很多牌组玩的。