我写了一个二十一点模拟器。目标是估计经销商破产的可能性。请注意,经销商必须在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