You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发Blackjack项目逻辑问题:用户要牌后游戏重启如何解决

Python 二十一点(Blackjack)项目逻辑缺陷修复

核心问题原因

  • 要牌逻辑仅触发一次,没有循环判断用户是否需要继续要牌,单次处理完成后直接回到最外层的游戏启动询问,所以看起来就像游戏从头重新加载
  • 庄家要牌规则写反,且没有在用户停牌后自动补牌到符合规则要求
  • 新增的卡牌没有同步更新到用户/庄家的手牌列表,仅修改了总点数变量,无法正确展示最新手牌
  • 单局结束后错误关闭了全局游戏开关,导致无法正常开启下一局
  • 缺少A(11点)在总点数超过21时自动转为1点的规则处理
  • 结算逻辑零散,存在大量未覆盖的判定场景(比如平局、双方爆牌等)

修复后完整代码

import random
import art
from replit import clear

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def add_card():
    """随机抽一张牌"""
    return random.choice(cards)

def calculate_score(cards_list):
    """计算手牌总点数,自动处理A的点数转换,返回21代表黑杰克"""
    total = sum(cards_list)
    # 处理黑杰克情况
    if total == 21 and len(cards_list) == 2:
        return 21
    # 总点数超过21,且有A的话把A换成1
    if 11 in cards_list and total > 21:
        cards_list.remove(11)
        cards_list.append(1)
        total = sum(cards_list)
    return total

game_on = True
while game_on:
    user_response = input("你想要玩一局二十一点吗?输入'y'开始,输入'n'退出:").lower()
    if user_response == "y":
        clear()
        print(art.logo)
        # 初始化手牌
        user_cards = [add_card(), add_card()]
        com_cards = [add_card(), add_card()]
        game_continue = True

        while game_continue:
            user_score = calculate_score(user_cards)
            com_score = calculate_score(com_cards)
            print(f"你的手牌:{user_cards},当前总点数:{user_score}")
            print(f"庄家的首张手牌:{com_cards[0]}")

            # 判断提前结束的情况:黑杰克或者爆牌
            if user_score == 21 or com_score == 21 or user_score > 21:
                game_continue = False
            else:
                # 循环询问用户是否要牌
                get_another = input("你需要再抽一张牌吗?输入'y'要牌,输入'n'停牌:").lower()
                if get_another == "y":
                    user_cards.append(add_card())
                else:
                    game_continue = False
        
        # 用户停牌后,庄家自动补牌到点数≥17
        while calculate_score(com_cards) < 17:
            com_cards.append(add_card())
        
        # 最终结算
        user_score = calculate_score(user_cards)
        com_score = calculate_score(com_cards)
        print(f"你最终的手牌:{user_cards},总点数:{user_score}")
        print(f"庄家最终的手牌:{com_cards},总点数:{com_score}")

        if user_score > 21:
            print("你爆牌了,你输了")
        elif com_score > 21:
            print("庄家爆牌了,你赢了")
        elif user_score == com_score:
            print("平局")
        elif user_score == 21:
            print("恭喜你拿到黑杰克,你赢了")
        elif com_score == 21:
            print("庄家拿到黑杰克,你输了")
        elif user_score > com_score:
            print("你的点数更高,你赢了")
        else:
            print("庄家点数更高,你输了")

    elif user_response == "n":
        print("游戏结束,欢迎下次再来玩")
        game_on = False
    else:
        print("输入错误,请重新输入")

关键修改说明

  • 新增calculate_score()函数统一处理点数计算逻辑,自动处理A的点数转换,同时识别黑杰克情况
  • 新增内层游戏循环,持续询问用户是否要牌,直到用户停牌、爆牌或者拿到黑杰克为止,不会直接跳回游戏启动页
  • 修正庄家要牌规则:庄家点数小于17时自动补牌,直到点数≥17才停止
  • 统一结算逻辑,在双方都停止要牌后统一判断胜负,覆盖所有可能的场景
  • 单局结束后不关闭全局游戏开关,自动回到启动询问界面,支持连续玩多局
  • 每次抽牌都同步更新手牌列表,保证打印的手牌信息和实际点数一致

内容的提问来源于stack exchange,提问作者python1990

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 18:06:09