【问题标题】:Writing a Blackjack program in Python, how to address Ace and Dealer cards?用 Python 编写 Blackjack 程序,如何处理 Ace 和 Dealer 牌?
【发布时间】:2022-01-07 17:52:20
【问题描述】:

我仍在学习 Python 并创建了一个 Blackjack 程序。到目前为止,我已经创建了一个基本的套牌,以及让庄家在 17 到 26 之间随机获得一手牌的二十一点程序本身

我现在想要得到的是:

根据手牌的当前值将 Ace 计数为 1 或 11,就像实际 Ace 一样

让庄家真正抽牌,从 1 张牌开始

从牌组中取出一张牌

甲板:

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

def main():
    current_value = 0
    deck = createdeck.create_deck()
    dealer_card = random.randint(17,26)
    print('Dealer finished drawing')
    draw_card = input('Hit? y for yes: ')
    while draw_card == 'y' or draw_card == 'Y':
        result = deal_cards(deck, current_value)
        draw_card = result[1]
        current_value = result[0]

    print(f"Dealer's hand is {dealer_card}")
    if current_value > 21:
        print('You bust. You lost the game.')
    elif dealer_card > 21:
        print('Dealer bust. You win!')
    elif dealer_card <= 21 and dealer_card < current_value:
        print('Congratulations! You win!')
    else:
        print('Sorry, you lost the game')

def deal_cards(deck, c_value):

    keys = list(deck.keys())
    card = random.choice(keys)
    value = deck[card]
    random.shuffle(keys)
    print(card)
    c_value += value
    print('Current value in hand:', c_value)

    if c_value < 21:
        d_card = input('Hit? Press y to hit. Press other key to stand: ')
    else:
        d_card = 0
    return c_value, d_card

main()

提前致谢

【问题讨论】:

  • 你的问题太模糊了。你究竟需要什么帮助?有什么问题?
  • 问题是我目前只有 Ace 默认为 1,而庄家只是得到一个介于 17 和 26 之间的随机数。尝试输入 Ace = 1 或 11 只会给我一个错误我需要帮助正在根据当前手牌值将 A 计为 1 或 11,并且我需要让庄家抽牌,而不是简单地从预定值之间的手牌开始
  • 我想你已经意识到使用固定分数字典的缺点了。您可以定义一个 hand_score() 函数,它将 Aces 视为 11,除非这会导致失败,否则视为 1。这意味着跟踪手中的牌,而不是分数。此外,每当发牌时,你就是在创建一个新牌组,我不确定这是否是故意的。

标签: python blackjack card


【解决方案1】:

只需跟踪 ace 的数量,然后您可以在之后减少分数(我通常默认 ace 为 11)

def calc_score(cards):
    ttl = 0
    num_aces = 0
    for c in  cards:
        ttl += deck[c]
        if c.startswith("ace"):
           ttl += 10
           num_aces += 1
    while aces and ttl > 21:
        ttl -= 10
        num_aces -= 1
    return ttl

calc_score(['ace of hearts', 'ace of diamonds', '5 of hearts','4 of hearts'])

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 2018-03-26
    • 2013-11-11
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多