需要协助21点python项目实现hitstand功能。

问题描述 投票:0回答:1

我正在做一个21点的项目,我是一个新的编码,所以它是一个有点困难,试图添加新的功能,如hitstand功能。我试着做了一个hitstand功能,但我不知道如何实际添加一个新的牌到玩家和庄家的牌的总数。我有一个sep文件,存储的卡组和洗牌的列表,但进口

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



#STAGE 1 - testing purpose only

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




#Stage 2 - testing purpose only   (PLAYERS HAND)

# 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 4 and 5

suits = {'C': 'Club', 'H': 'Heart', 'D': 'Diamond', 'S': 'Spade'}
names = {'2': '2', '3': '3', '4': '4', 
            '5':'5', '6': '6', '7': '7', '8': '8', '9': '9', 'T': '10','J': 'Jack', 'Q': 'Queen', 'K': 'King', 'A': '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
  value['T'] = 10

  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

#ask about this

def show_result(player_hand):

    player_score = int(score_hand(player_hand))

    if player_score > 21:
        print('*** Player Bust! ***')
    elif player_score == 21:
        print('*** Blackjack! Player Wins! ***')


##else:
##            push(player_hand,dealer_hand)
##def push(player_score,dealers_score):
##    print("Dealer and Player tie! It's a push.")        
##else:
##    // Logic to continue game





def hit_stand(show_hand,player_hand):

    while True:
      x = input("Would you like to Hit or Stand? Enter 'h' or 's'")

      if x[0].lower() == 'h':
           hit(show_hand,player_hand)  # hit() function defined above

      elif x[0].lower() == 's':
           print("Player stands. Dealer is playing.")
           playing = False









def show_hand(player_hand):

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





for hand in hands:
  print(show_hand(player_hand))
  show_result(player_hand)




#Stage 3 - Testing purpose only       (DEALERS HAND)

### 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


#Stage 4 and 5

def score_hand(dealers_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
  value['T'] = 10

  score = sum(int(value[card[0]]) for card in dealers_hand)
  if score > 21:
    # Adjust for Aces
    adjustment = sum(10 if card[0]=='A' else 0 for card in dealers_hand)
    score -= adjustment

  return score

#ask about this

def show_result(dealers_hand):

    dealers_score = int(score_hand(dealers_hand))

    if dealers_score > 21:
        print('*** Dealer Bust! ***')
    elif dealers_score == 21:
        print('*** Blackjack! Dealer Wins! ***')


def show_hand(dealers_hand):

  score = f"Dealers's hand is {score_hand(dealers_hand)}: "
  cards = ' | '.join([f"{names[card[0]]} of {suits[card[1]]}" for card in dealers_hand])

  return score + cards


for hand in hands:
  print(show_hand(dealers_hand))
  print('')
  show_result(dealers_hand)
  hit_stand(show_hand, player_hand)
python blackjack
1个回答
0
投票

也许这将使你走上正确的道路(我找不到一个定义的'hit'函数,所以你可以在hit_stand中完成所有的工作,如下)。

def hit_stand(show_hand,player_hand):

    while True:
      x = input("Would you like to Hit or Stand? Enter 'h' or 's'")

      if x[0].lower() == 'h':
           player_hand.append(playing_cards.deal_one_card())

      elif x[0].lower() == 's':
           print("Player stands. Dealer is playing.")
           playing = False

这样就能解决玩家如何出牌的问题 但我在你的代码中没有看到让庄家决定出牌或站牌的逻辑 这是基于标准的内部规则

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