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

Python编写Blackjack程序时Ace规则、庄家发牌及卡牌移除问题求解

Python二十一点游戏功能实现方案

核心功能实现思路

  • Ace自动适配点数:新增单独的手牌点数计算函数,先统计手牌中A的数量,默认按11计算总点数,若总点数超过21则每有一张A就减10(即调整为按1计算),直到总点数≤21或所有A都调整完毕。
  • 庄家真实抽牌逻辑:按照标准二十一点规则,庄家初始持有2张牌(1张明牌、1张暗牌),待玩家停止抽牌后,庄家必须持续抽牌直到手牌总点数≥17,停止抽牌后再结算胜负。
  • 抽牌后移除牌组:每次随机选中卡牌后,直接从牌组字典中删除对应key,避免后续重复抽取。

完整修改后代码

牌组生成代码(保留原结构即可)

def create_deck():
    deck = {'A of spades':1,
            '2 of spades':2,
            '3 of spades':3,
            '4 of spades':4,
            '5 of spades':5,
            '6 of spades':6,
            '7 of spades':7,
            '8 of spades':8,
            '9 of spades':9,
            '10 of spades':10,
            'J of spades':10,
            'Q of spades':10,
            'K of spades':10,

            'A of hearts': 1,
            '2 of hearts': 2,
            '3 of hearts': 3,
            '4 of hearts': 4,
            '5 of hearts': 5,
            '6 of hearts': 6,
            '7 of hearts': 7,
            '8 of hearts': 8,
            '9 of hearts': 9,
            '10 of hearts': 10,
            'J of hearts': 10,
            'Q of hearts': 10,
            'K of hearts': 10,

            'A of clubs': 1,
            '2 of clubs': 2,
            '3 of clubs': 3,
            '4 of clubs': 4,
            '5 of clubs': 5,
            '6 of clubs': 6,
            '7 of clubs': 7,
            '8 of clubs': 8,
            '9 of clubs': 9,
            '10 of clubs': 10,
            'J of clubs': 10,
            'Q of clubs': 10,
            'K of clubs': 10,

            'A of diamonds': 1,
            '2 of diamonds': 2,
            '3 of diamonds': 3,
            '4 of diamonds': 4,
            '5 of diamonds': 5,
            '6 of diamonds': 6,
            '7 of diamonds': 7,
            '8 of diamonds': 8,
            '9 of diamonds': 9,
            '10 of diamonds': 10,
            'J of diamonds': 10,
            'Q of diamonds': 10,
            'K of diamonds': 10,
            }
    return deck

主体程序代码

import createdeck
import random

# 计算手牌总点数,自动适配Ace的取值
def calculate_hand_value(hand):
    value = 0
    ace_count = 0
    base_deck = createdeck.create_deck()
    for card in hand:
        if card.startswith('A'):
            ace_count += 1
        value += base_deck[card]
    # 先将所有A按11计算(基础值为1,补10即可)
    value += ace_count * 10
    # 总点数超过21时,逐个将A调整为1
    while value > 21 and ace_count > 0:
        value -= 10
        ace_count -= 1
    return value

# 单次抽牌逻辑,抽完自动从牌组移除
def draw_one_card(deck):
    card = random.choice(list(deck.keys()))
    del deck[card]
    return card

# 庄家抽牌逻辑
def dealer_play(deck, dealer_hand):
    while calculate_hand_value(dealer_hand) < 17:
        dealer_hand.append(draw_one_card(deck))
    return dealer_hand

def main():
    deck = createdeck.create_deck()
    # 初始化玩家、庄家手牌
    player_hand = []
    dealer_hand = []
    # 初始各发2张牌
    for _ in range(2):
        player_hand.append(draw_one_card(deck))
        dealer_hand.append(draw_one_card(deck))
    
    print(f"庄家明牌:{dealer_hand[0]}")
    print(f"你的手牌:{', '.join(player_hand)},当前点数:{calculate_hand_value(player_hand)}")

    # 玩家抽牌阶段
    while True:
        player_value = calculate_hand_value(player_hand)
        if player_value >= 21:
            break
        draw_card = input('是否要牌?输入y要牌,其他键停牌:').strip().lower()
        if draw_card == 'y':
            new_card = draw_one_card(deck)
            player_hand.append(new_card)
            print(f"你抽到了:{new_card},当前手牌:{', '.join(player_hand)},点数:{calculate_hand_value(player_hand)}")
        else:
            break
    
    # 玩家爆牌直接结算
    player_value = calculate_hand_value(player_hand)
    if player_value > 21:
        print('你爆牌了,游戏失败')
        return
    
    # 庄家抽牌阶段
    print('庄家开始抽牌...')
    dealer_hand = dealer_play(deck, dealer_hand)
    dealer_value = calculate_hand_value(dealer_hand)
    print(f"庄家手牌:{', '.join(dealer_hand)},点数:{dealer_value}")

    # 胜负结算
    if dealer_value > 21:
        print('庄家爆牌,你赢了!')
    elif player_value > dealer_value:
        print('恭喜,你赢了!')
    elif player_value == dealer_value:
        print('平局')
    else:
        print('很遗憾,你输了')

if __name__ == "__main__":
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 03:45:09