编程语言
首页 > 编程语言> > 在发第二张牌之前先向每位玩家发一张牌-Python中的21点游戏

在发第二张牌之前先向每位玩家发一张牌-Python中的21点游戏

作者:互联网

我在Python中编写了以下代码.其中deck = deck_of_cards().我的问题是如何修复第二功能,以便在发第二张牌之前必须向每位玩家发第一张牌.目前的方式似乎是先将两张牌发给一名玩家,然后再将另一张牌发给其他玩家,但我不知道该如何解决.

import random
def deck_of_cards():
    suits = ['D', 'C', 'H', 'S']
    denominations = ['2', '3', '4', '5', '6', '7', '8', 
                     '9', 'T', 'J', 'Q', 'K', 'A']
    deck = []
    for suit in suits:
        for denomination in denominations:
            ds = denomination + suit
            deck.append(ds)

    random.shuffle(deck)      
    return deck

def deal_cards(deck, players):
    hands = []

    for j in range(players):
        player_hand = []
        for i in range(2):
            cards = deck.pop(0)
            player_hand.append(cards)

        hands.append(player_hand)  
    return hands 

解决方法:

您需要交换for循环,以便您遍历所有玩家的第一辆车,然后遍历第二辆车:

def deal_cards(deck, players):
    hands = [[] for player in range(players)]

    for _ in range(2):
        for hand in hands:
            hand.append(deck.pop(0))

    return hands 

标签:python-3-x,blackjack,python
来源: https://codeday.me/bug/20191110/2014515.html