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