# 摊牌、结算、问要不要再来一局。 # 无参数(直接读外层的 player_cards / cpu_cards),无返回值。 def done_playing(): player_sum = sum(player_cards) cpu_sum = sum(cpu_cards) print(f"Your hand is: {player_cards}, a total of {player_sum}.") print(f"The computer's hand is {cpu_cards}, a total of {cpu_sum}.") # 判断顺序很关键:先判“爆牌”,再判 21 点,最后比大小。 # 顺序错了就会出现“电脑 25 点却因为比你大而赢”这种荒唐结果。 if sum(cpu_cards) > 21: print("The computer got a bust, you win!") elif player_loss(player_cards, cpu_cards): print("You lose.") elif sum(player_cards) == 21 and sum(cpu_cards) != 21: print("You win!") elif sum(player_cards) > 21: print("It's a bust, you lose.") elif player_sum == cpu_sum: print("It's a draw.") elif player_sum > cpu_sum: print("You win!") else: print("You lose.") play_again = input("Do you want to play again? Type 'y' or 'n': ") if play_again == 'y': clear() # 递归:再调用一次自己,等于“重开一局”。 # 缺点和 Day10 一样——旧的一局还压在调用栈上,玩久了栈会越来越深。 blackjack_game() else: if play_again == 'n': print("Goodbye.")
# “继续要牌”的主流程:发一张给玩家、一张给电脑,算点数,再问要不要接着要。 def keep_playing(): shuffled = start_deal(cards) player_cards.append(shuffled) # append(x):列表方法,把 x 加到末尾,原地修改,返回 None。 cpu_cards.append(start_deal(cards)) player_sum = sum(player_cards) cpu_sum = sum(cpu_cards) # A 的特殊规则:11 点如果让总数超过 21,就自动当 1 点用。 # 11 in player_cards 判断列表里有没有 11; # list.index(x):列表方法,返回 x 第一次出现的下标(找不到抛 ValueError)。 if 11 in player_cards and player_sum >= 21: ace = player_cards.index(11) player_cards[ace] = 1 player_sum = sum(player_cards) print(f"Your hand is: {player_cards}, a total of {player_sum}.") print(f"The computer's hand is {cpu_cards}, a total of {cpu_sum}.") if player_sum > 21: print("It's a bust, you lose.") play_again = input("Do you want to play again? Type 'y' or 'n': ") if play_again == 'y': clear() blackjack_game() elif play_again == 'n': print("Thanks for playing. Goodbye.") if player_sum <= 21: hit = input("Type 'y' to get another card, type 'n' to pass: ") if hit == 'y': # 递归:再要一张,就是再走一遍 keep_playing。 keep_playing() elif hit == 'n': done_playing()
print(logo) # 牌堆:A 记 11,J/Q/K 都记 10,2–10 按面值。 cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] # 两边的牌面列表,一开始都是空的。 player_cards = [] cpu_cards = [] # 开局各发两张(电脑只发一张,另一张“暗着”)。 player_cards.append(start_deal(cards)) player_cards.append(start_deal(cards)) cpu_cards.append(start_deal(cards)) print(f"Your cards: {player_cards}") print(f"The computer's first card: {cpu_cards}") hit = input("Type 'y' to get another card, type 'n' to pass: ") if hit == 'n': done_playing() elif hit == 'y': keep_playing()
Your cards: [10, 7] The computer's first card: [9] Type 'y' to get another card, type 'n' to pass: y Your hand is: [10, 7, 5], a total of 22. It's a bust, you lose.
def done_playing(): player_sum = sum(player_cards) cpu_sum = sum(cpu_cards) print(f"Your hand is: {player_cards}, a total of {player_sum}.") print(f"The computer's hand is {cpu_cards}, a total of {cpu_sum}.") if sum(cpu_cards) > 21: print("The computer got a bust, you win!") elif player_loss(player_cards, cpu_cards): print("You lose.") elif sum(player_cards) == 21 and sum(cpu_cards) != 21: print("You win!") elif sum(player_cards) > 21: print("It's a bust, you lose.") elif player_sum == cpu_sum: print("It's a draw.") elif player_sum > cpu_sum: print("You win!") else: print("You lose.")
if player_sum > 21: print("It's a bust, you lose.") play_again = input("Do you want to play again? Type 'y' or 'n': ") ... if player_sum <= 21: hit = input("Type 'y' to get another card, type 'n' to pass: ") if hit == 'y': keep_playing() elif hit == 'n': done_playing()