【问题标题】:Python Blackjack lists and special valuesPython Blackjack 列表和特殊值
【发布时间】:2016-09-02 16:05:02
【问题描述】:

我正在尝试在 python 中调试二十一点游戏,但是一旦发牌,我无法弄清楚如何通过 King 实现 Ace。我需要一个提示或什么...我是否忽略了一些明显的东西?

MAX = 21


# main function

def main():
    # Local variables

    hand1 = 0

    hand2 = 0

    deck = create_deck()

    while hand1 <= 21 and hand2 <= 21:
        card1, value1 = deck.popitem()

        hand1 = update_hand_value(hand1, value1, card1)

        card2, value2 = deck.popitem()

        hand2 = update_hand_value(hand2, value2, card2)

        print('Player 1 was dealt', card1)

        print('Player 2 was dealt', card2)

        print()

    # Determine the winner.

    if hand1 > MAX and hand2 > MAX:

        print("There is no winner.")

    elif hand1 > 21:

        print("Player 2 wins.")

    else:

        print("Player 1 wins.")


def create_deck():
    # Set up local variables

    suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']

    # unused currently, needs to be implemented
    special_values = {'Ace': 1, 'King': 10, 'Queen': 10, 'Jack': 10}

    numbers = ['Ace', 'King', 'Queen', 'Jack']

    for i in range(2, 11):
        numbers.append(str(i))

    # Initialize deck

    deck = {}

    for suit in suits:

        for num in numbers:

            # Values 2-10.

            if num.isnumeric():
                deck[num + ' of ' + suit] = int(num)
            else:
                deck[num + ' of ' + suit] = str(num)
            # values 1 and 11

    return deck


def update_hand_value(hand, value, card):
    if not card == 'Ace':

        return hand, value

    # Adding 11 would cause to go over the maximum.

    elif hand > 10:

        # Value is 1 by default.

        return hand, value

    else:

        return hand + 11


# Call the main function.

main()


# Call the main function.

main()

在玩家获胜之前一直发牌。

示例输出:

玩家 1 获得了 2 颗钻石 玩家 2 得到了黑桃 8

玩家 1 获得了 6 颗钻石 玩家 2 获得了 5 颗钻石

玩家 1 得到了 4 个俱乐部 玩家 2 获得了 8 个方块

玩家 1 得到了 7 个俱乐部 玩家 2 得到了黑桃 3

玩家 1 获胜。

编辑: 我已经修改了我的程序,但出现了一个新错误。

Traceback(最近一次调用最后一次): 文件“C:/Users/Sean/PycharmProjects/SeanPython/ISY150/Demo/Blackjack.py”,第 99 行,在 主要的() 文件“C:/Users/Sean/PycharmProjects/SeanPython/ISY150/Demo/Blackjack.py”,第 17 行,在 main 而hand1

我不明白,据我所知,在第 17 行,我没有将元组与 int 进行比较...

【问题讨论】:

  • 不得不编辑我的程序。另外,我想这个程序必须以固定的方式完成。我实际上有另一个工作二十一点程序。这只需要以这种方式完成。我必须调试它作为练习。

标签: python list function blackjack playing-cards


【解决方案1】:

主要问题是您的update_hand_value 函数有不同的返回类型。您在 ifelif 语句中返回一个元组,但在 else 中返回一个整数。

不过还有很多其他问题,所以我将介绍一些建议来帮助您。

1) 您正在使用按顺序创建的dict 中的.pop()。因此,您将始终获得相同的卡片顺序。在从这些列表创建字典之前,尝试使用 np.random.shuffle 洗牌。在create_deck() 中执行for 循环之前,请特别执行np.random.shuffle(suits)np.random.shuffle(numbers)

2) 使用这个: if type(num) == int: deck[str(num) + ' of ' + suit] = int(num) else: deck[num + ' of ' + suit] = str(num) #here ace, queen, and king, have strings as value in key:value pairs

3) 你从未真正更新update_hand_value() 中的手牌值,所以试试这个:

    if card != 'Ace': #complete all cases where card was not an ace 
      if type(value) == int: #a number was drawn - add number to hand
        hand = hand + value
        return hand
      elif type(value) == str: #this is a jack, queen, or king - add ten to hand
        hand = hand + 10
        return hand

    else: #card was an ace
        if hand + 11 == 21: #stop
            hand = hand + 11
            return hand
        elif hand + 11  > 21: #only add one
            hand = hand + 1
            return hand

4) 最后,您需要跟踪玩家拥有的总牌数。如果一张牌超过 21,A 的 11 可以转换为 1。我将把这个留给你来解决,但请尝试传递一个列表来更新手牌并在其中搜索 A。

干杯

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2017-06-24
    • 1970-01-01
    • 2021-08-15
    • 2015-01-24
    相关资源
    最近更新 更多