【问题标题】:Blackjack simulator (to check)二十一点模拟器(检查)
【发布时间】:2013-01-25 18:42:44
【问题描述】:

我写了一个二十一点模拟器。目标是估计经销商破产的可能性。请注意,庄家必须在 16 和必须站在 17 上。如果庄家的手上有一张 A,则当总点数在 17 和 21 之间时,它应该算作 11;否则,王牌应计为 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

【问题讨论】:

  • 如果你连续模拟很多游戏,你应该考虑到牌堆会耗尽庄家抽的特定牌(庄家不太可能再次抽到同一张牌) )。如果您模拟少量游戏,那么这种影响在很大程度上可以忽略不计,因为二十一点通常与许多套牌一起玩。

标签: python random python-2.7 nested-loops


【解决方案1】:

整体逻辑看起来不错。

我的建议是针对您担心的特定情况写一些Unit Tests

【讨论】:

  • 好的,感谢您阅读本文。感谢您的建议,我还没有学习单元测试。
猜你喜欢
  • 1970-01-01
  • 2021-10-10
  • 2014-07-19
  • 1970-01-01
  • 2012-01-03
  • 2011-11-09
  • 2016-11-09
  • 2014-10-27
  • 1970-01-01
相关资源
最近更新 更多