ブラックジャックシミュレーターを書きました。目標は、ディーラーがバストする可能性を推定することでした。ディーラーは 16 のときに引き、17 のときにスタンドしなければならないことに注意してください。それ以外の場合、エースは 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