【问题标题】:Need help on blackjack game to print out total of two cards在二十一点游戏中需要帮助以打印出总共两张牌
【发布时间】:2020-09-26 06:05:32
【问题描述】:

我需要制作一个黑杰克游戏,它分为 2 个文件,一个处理洗牌,另一个处理实际游戏

我的问题是我可以获得为玩家和庄家打印两张牌的代码,尽管我需要以这样的格式打印

玩家的手牌是 3:1 of Clubs |红心2

不是这样的

['1C', '2H']

显示总数和分牌

这是管理游戏的第一个文件,另一个是卡片

import playing_cards
import random
player_hand = []
dealers_hand = []

#

# Testing code for step 1 
##card = playing_cards.deal_one_card()
##print(card)


# Player

# Deal first card
card = playing_cards.deal_one_card()

# Append it to the player_hand list
player_hand.append(card)
# Deal second card
card = playing_cards.deal_one_card()
# Append it to the player_hand list
player_hand.append(card)
#ADDING UP BOTH CARDS

# Display the player's hand to the screen using a simple print statement
print("Player's hand is ",  player_hand)



#Stage 3 - dealer

# Deal first card
card = playing_cards.deal_one_card()
# Append it to the player_hand list
dealers_hand.append(card)
# Deal second card
card = playing_cards.deal_one_card()
# Append it to the player_hand list
dealers_hand.append(card)
# Display the player's hand to the screen using a simple print statement
print(dealers_hand)

这是另一个文件,但任务说我不能修改它,我只是添加它以使其更容易理解

deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
        'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
        'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
        'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']

# Playing deck in use
playing_deck = []


# Function to determine whether there are any cards left in the
# deck of playing cards
# Parameters: No parameters
# Returns: True if the deck is empty, False otherwise
def is_empty_deck():

    # Check to see whether playing deck is empty
    return len(playing_deck) == 0


# Function to rebuild and shuffle the deck
# Parameters: No parameters
# Returns: Nothing is returned from the function.
def reset_deck():
    global playing_deck

    # Create new playing deck
    playing_deck = deck.copy()

    # Shuffle deck
    random.shuffle(playing_deck)


# Function to deal one card
# Parameters: No parameters
# Returns: A string (containing two characters) representing
# the card delt, i.e. '2H' meaning 2 of Hearts
def deal_one_card():

    # Check to see whether there are any cards left
    if is_empty_deck():

        # Rebuild and shuffle deck
        reset_deck()

    # Return a card (string of two characters)
    return playing_deck.pop(0)

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已在 CC BY-SA license 下授予 SE 分发内容的不可撤销的权利(无论您未来的选择如何)。根据 SE 政策,分发非破坏版本。因此,任何此类破坏性编辑都将被还原。请参阅How does deleting work?,了解有关删除本网站内容如何运作的更多信息。

标签: python blackjack


【解决方案1】:

您只需编写一个函数将1C 转换为1 of Club。您可以使用字典来执行西装的映射。

d = dict()
domain = ['C', 'H', 'D', 'S']
image = ['Club', 'Heart', 'Diamond', 'Spade']
for i in range(4):
    d[domain[i]] = image[i]

def process(x):
    return x[0] + ' of ' + d[x[1]]

print(process('1C'))

【讨论】:

    【解决方案2】:

    dict 非常有帮助:

    def card(code):
        ref = {'C': 'Club', 'H': 'Heart', 'D': 'Diamond', 'S': 'Spade'}
        value = {'A': 'Ace', '2': 'Two', '3': 'Three', '4': 'Four', '5': 'Five',
                 '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine', 'T': 'Ten',
                 'J': 'Jack', 'Q': 'Queen', 'K': 'King'}
        return value[code[0]] + ' of ' + ref[code[-1]]
    

    测试:

    [print(card(i)) for i in deck]
    

    输出:

    Ace of Hearts
    Two of Hearts
    Three of Hearts
    Four of Hearts
    Five of Hearts
    Six of Hearts
    Seven of Hearts
    Eight of Hearts
    ... ...
    Nine of Clubs
    Ten of Clubs
    Jack of Clubs
    Queen of Clubs
    King of Clubs
    

    或:

    import inflect
    
    deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
            'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
            'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
            'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']
    
    def card(code):
        ref = {'C': 'Clubs', 'H': 'Hearts', 'D': 'Diamonds', 'S': 'Spades',
               'A': 'Ace',  'T': 'Ten', 'J': 'Jack', 'Q': 'Queen', 'K': 'King'}
        if code[0] not in [str(i) for i in range(1, 10)]:
            return ref[code[0]] + ' of ' + ref[code[-1]]
        return inflect.engine().number_to_words(int(code[0])) + ' of ' + ref[code[-1]]
    
    [print(card(i)) for i in deck]
    

    【讨论】:

      【解决方案3】:

      当手牌 > 21 时用 Ace 进行调整。

      在第一个文件中添加了以下功能

      def score_hand(player_hand):
        " Scores hand with adjustment for Ace "
        value = {}
        for i in range(10):
          value[str(i)] = i
        value['J'] = 10
        value['Q'] = 10
        value['K'] = 10
        value['A'] = 11
      
        score = sum(int(value[card[0]]) for card in player_hand)
        if score > 21:
          # Adjust for Aces
          adjustment = sum(10 if card[0]=='A' else 0 for card in player_hand)
          score -= adjustment
      
        return score
      
      def show_hand(player_hand):
        suits = {'C': 'Club', 'H': 'Heart', 'D': 'Diamond', 'S': 'Spade'}
        names = {'2': 'Two', '3': 'Three', '4': 'Four', 
                  '5':'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine', 'T': 'Ten','J': 'Jack', 'Q': 'Queen', 'K': 'King', 'A': 'Ace'}
      
        score = f"Player's hand is {score_hand(player_hand)}: "
        cards = ' | '.join([f"{names[card[0]]} of {suits[card[1]]}" for card in player_hand])
      
        return score + cards
      

      测试

      示例牌

      hands = [['3S', '4H'], ['AC', '5D'], ['AC', 'KD', '2C'], ['AC', 'KD', '5C', 'AH'], ['AC', 'AH', 'AD', 'KD', '5C'], ['AC', 'AH', 'AD', 'KD', 'JC']]
      for hand in hands:
        print(f"{hand} shows as ->>>>")
        print(show_hand(hand))
        print()
      

      输出

      ['3S', '4H'] shows as ->>>>
      Player's hand is 7: Three of Spade | Four of Heart
      
      ['AC', '5D'] shows as ->>>>
      Player's hand is 16: Ace of Club | Five of Diamond
      
      ['AC', 'KD', '2C'] shows as ->>>>
      Player's hand is 13: Ace of Club | King of Diamond | Two of Club
      
      ['AC', 'KD', '5C', 'AH'] shows as ->>>>
      Player's hand is 17: Ace of Club | King of Diamond | Five of Club | Ace of Heart
      
      ['AC', 'AH', 'AD', 'KD', '5C'] shows as ->>>>
      Player's hand is 18: Ace of Club | Ace of Heart | Ace of Diamond | King of Diamond | Five of Club
      
      ['AC', 'AH', 'AD', 'KD', 'JC'] shows as ->>>>
      Player's hand is 23: Ace of Club | Ace of Heart | Ace of Diamond | King of Diamond | Jack of Club
      

      【讨论】:

      • 感谢您的帮助,现在很有意义。我尝试了示例手,效果非常好,但我将如何使用随机牌而不是设置示例?
      • 很抱歉询问我如何阻止它打印 5 次。只是困惑我如何实际打印出我真正的游戏的总数
      • @isuckatcode--正如我的示例展示你使用的手:print(show_hand(hand))。因此,您将当前打印的闲家和庄家手牌替换如下。显示玩家的手牌:print(show_hand(player_hand))。对于经销商的手:print(show_hand(dealers_hand))。是吗
      • 得到它的工作谢谢,我注意到一件事,有时我得到错误 T ?任何想法
      • @isuckatcode--你能提供错误的完整堆栈跟踪吗(即它通常显示错误的行号和类型)?
      猜你喜欢
      • 2016-01-08
      • 2013-04-29
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2018-05-18
      • 2018-04-02
      相关资源
      最近更新 更多