【问题标题】:Need assistance on blackjack python project implementing hit/stand feature在二十一点 python 项目实现命中/站立功能需要帮助
【发布时间】:2020-09-30 20:28:18
【问题描述】:

我只是在做一个二十一点项目,而且我是编码新手,所以尝试添加新功能(例如击中/站立功能)有点困难。我曾尝试制作击中/站立功能,但我不确定如何将新卡实际添加到玩家卡和经销商的总数中。我只是盯着添加玩家来尝试获得一些结果,但我只是停留在这个速度上,不知道如何正确实现这一点。我有一个 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】:
    import random
    
    Dealer_Cards=[]
    Player_Cards=[] # im also beginner in programming just little know some C from College
    Total_Player=0   # and watched some tutorial like 5 min and get the whole algorithm 
                      # then start thinking and coding i have little excuse like how to i
    Total_Dealer=0    # stop when player or dealer hit >=21 expect option .
    #Deal the cards
    #Dealer Cards
    while len(Dealer_Cards) != 2:
        Dealer_Cards.append(random.randint(1,11))
        if len(Dealer_Cards) == 2:
            print("Dealer has ",Dealer_Cards)
    
    # Player Cards
    while len(Player_Cards) != 2:
        Player_Cards.append(random.randint(1,11))
        if len(Player_Cards) == 2:
            print("You have ",Player_Cards)
            print("you have 2 option Hit or Stay:[Click '1' for Hit and if you want to Stay click '0']:")
            option= int(input(""))
            if option == 0:
                Total_Dealer+= sum(Dealer_Cards) #Dealer_Cards= i
                print("sum of the Dealer cards:",Total_Dealer)
                Total_Player+= sum(Player_Cards) # Player_Cards= x
                print("sum of the Player cards:",Total_Player)
    
                if Total_Dealer > Total_Player:
                    print("Dealer Wons")
                    break
                elif Total_Player > Total_Dealer:
                    print("You Won")
                    break
                else:
                    print("Scoreless.")
                    break
    
            elif option == 1:
                while len(Player_Cards) != 3:
                    Player_Cards.append(random.randint(1,11))
                    Dealer_Cards.append(random.randint(1,11))
            if len(Player_Cards) == 3:
                print("You have ",Player_Cards)
                print("you have 2 option Hit or Stay:[Click '1' for Stay and if you want to Stay click '0']")
                option= int(input(""))
                if option== 0:
                    Total_Dealer += sum(Dealer_Cards)
                    print("sum of the Dealer cards:", Total_Dealer)
                    Total_Player += sum(Player_Cards)
                    print("sum of the Player cards:", Total_Player)
    
                    if Total_Player > 21 and Total_Dealer <= 21:
                        print("You are BUSTED !")
                        break
                    elif Total_Player == Total_Dealer:
                        print("Scoreless")
                        break
                    elif Total_Player <= 21 and Total_Dealer > 21:
                        print("You WON !")
                        break
                    elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Player > Total_Dealer):
                        print("You WON !")
                    elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Dealer > Total_Player):
                        print("Dealer WON !")
                        break
                elif option == 1:
                    while len(Player_Cards) != 4:
                        Player_Cards.append(random.randint(1,11))
                        Dealer_Cards.append(random.randint(1,11))
            if len(Player_Cards) == 4:
                print("You have ",Player_Cards)
                print("you have 2 option Hit or Stay:[Click '1' for Stay and if you want to Stay click '0']")
                option= int(input(""))
                if option == 0:
                    Total_Dealer += sum(Dealer_Cards)
                    print("sum of the Dealer cards:", Total_Dealer)
                    Total_Player += sum(Player_Cards)
                    print("sum of the Player cards:", Total_Player)
    
                    if Total_Player > 21 and Total_Dealer <= 21:
                        print("You are BUSTED !")
                        break
                    elif Total_Player == Total_Dealer:
                        print("Scoreless")
                        break
                    elif Total_Player <= 21 and Total_Dealer > 21:
                        print("You WON !")
                        break
                    elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Player > Total_Dealer):
                        print("You WON !")
                        break
                    elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Dealer > Total_Player):
                        print("Dealer WON !")
                        break
                elif option == 1:
                    while len(Player_Cards) != 5:
                        Player_Cards.append(random.randint(1, 11))
                        Dealer_Cards.append(random.randint(1,11))
                        if len(Player_Cards) == 5:
                            print("You have ",Player_Cards)
                            Total_Dealer+= sum(Dealer_Cards)
                            print("sum of the Dealer cards:",Total_Dealer)
                            Total_Player+= sum(Player_Cards)
                            print("sum of the Player cards:",Total_Player)
                            if Total_Player > 21 and Total_Dealer <= 21:
                                print("You are BUSTED !")
                                break
                            elif Total_Player == Total_Dealer:
                                print("Scoreless")
                                break
                            elif Total_Player <= 21 and Total_Dealer > 21:
                                print("You WON !")
                                break
                            elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Player > Total_Dealer):
                                print("You WON !")
                                break
                            elif (Total_Player < 21 and Total_Dealer < 21) and (Total_Dealer > Total_Player):
                                print("Dealer WON !")
                                break
    

    【讨论】:

      【解决方案2】:

      也许这会让你走上正轨(我找不到定义的“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
      

      这将解决玩家如何击球,但我没有在您的代码中看到让庄家决定击球或站立的逻辑,这是基于标准的房子规则。

      【讨论】:

      • 感谢随机的帮助,是的,我知道我的代码到处都是。我只是想让点击功能正常工作,即使游戏可能有点乱。关于我是否有可能在上面添加另一张卡的任何建议。我用你的部分来帮助处理定义的命中,但我该怎么做才能真正更新总数并显示第三张卡片?
      • 我注意到您的方法的另一个问题是,例如,您定义了两次 show_hand 或 score_hand,一次使用 player_hand 变量,一次使用 Dealer_hand 变量,但这不是它的工作方式。相反,您只需要定义一个 show_hand 和一个 score_hand,它接受一个从外部传递的参数,然后传入 player_hand 或 Dealer_hand
      猜你喜欢
      • 2016-07-06
      • 2016-01-08
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      相关资源
      最近更新 更多