【发布时间】: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