2 of Hearts

问题描述 投票:0回答:3
not like this

['1C', '2H']

Showing the total and seperating the cards

Here is the first file that manages the game and the other is the cards

Here is the other file but the task says i cant mod it,, i just added it to make it a little easier to understand

answer has been solved test if the code works

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)

You just have to write a function to convert

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)
to
python blackjack
3个回答
0
投票

1CA 1 of Club helps here, tremendously:

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'))
Test:

0
投票

OR: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]

Make adjustment with Ace when hand > 21.

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

Added following Functions

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]
to first file

0
投票

Example hands

Output

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

我有一个黑杰克游戏,我需要做,它分为2个文件,一个是关于洗牌,另一个是关于实际的游戏。

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

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()

闲家的牌是3:梅花1。

© www.soinside.com 2019 - 2024. All rights reserved.