【问题标题】:Trying to combine a list into sublists for players in a blackjack game using python尝试使用python将列表组合成二十一点游戏中玩家的子列表
【发布时间】:2020-06-20 16:23:56
【问题描述】:

我正在制作自己的二十一点游戏,而不使用其他解决方案。这只是代码的一部分,因为我正在逐步工作。我想合并一张卡片列表,以便为每个玩家制作一个新列表。我有第一对牌发给 4 位玩家:

shuffled_deck = [['A', '♦'], ['6', '♣'], ['2', '♥'], ['Q', '♦'], ['K', '♦'], ['3', '♠'], ['8', '♠'], ['9', '♠']]

players = [[1], [2], [3], [4]]

想要的输出>>

[[['A', '♦'], ['K', '♦']], [['6', '♣'], ['3', '♠']], [['2', '♥'], ['8', '♠']], [['Q', '♦'], ['9', '♠']]]

它只是向 4 名玩家发两张牌,就像在二十一点中一样。 我可以使用它来创建它:

#EXAMPLE 1    
for i in range(0,4):
    players[i] = [shuffled_deck[i]]

for j in range(0,4):
    players[j].append(shuffled_deck[j+4])

但我想用这样的东西来做,但不知道怎么做。

count = 0
people = []

def combine_2_cards(people=[], count=count):
    while count < 8:
        people.append(shuffled_deck.pop(0))
        count += 1 
        return combine_2_cards(people, count)
    return people

players = combine_2_cards(people, count)

这只是创建了原始的 shuffled_cards 列表。也许还有更好的方法?

我需要让它像所需的输出一样附加。

【问题讨论】:

    标签: python list recursion append blackjack


    【解决方案1】:

    这是如何工作的: 首先,递归的基本情况是卡片为空时,而不是 在经典的“if (basecase) else (do stuff)”中,我们正在检查卡片列表是否不为空,我们填充玩家

    玩家如何填充:从cards列表中弹出2张卡片,当i为0时,弹出第一个元素,当i为1时,弹出中间的卡片,然后将两者作为列表添加到卡片列表中

    一遍又一遍地这样做,直到我们达到基本情况。

    def combine_2_cards(players, cards):
        if cards: 
            # assign 2 cards to 1 players 
            current_cards = [card for card in [cards.pop(0 if i % 2 == 0 else len(cards) // 2) for i in range(2)]]
            players.append(current_cards)
            combine_2_cards(players, cards)
        return players
    
    print(combine_2_cards([], shuffled_deck))
    # prints: [[['A', '♦'], ['K', '♦']], [['6', '♣'], ['3', '♠']], [['2', '♥'], ['8', '♠']], [['Q', '♦'], ['9', '♠']]]
    

    【讨论】:

    • 如果我已经设置了一组玩家骨架,比如players = [[], [], [], []]
    • 如果我尝试将其传递给函数,结果不正确。一旦达到播放器限制,停止该功能的最佳方法是什么?
    • 另外,函数如何从洗好的牌组中选出前n*2张牌,其中n是玩家的数量?
    【解决方案2】:

    非常感谢@pidgeyUsedGust 和@Fibi。虽然我还没有使用理解,但我想我现在可以更好地了解它们的要点。 我还运行了我的函数两次,并像这样创建了所需的输出,

    players = [[], [], [], []]
    count = 0
    people = []
    
    def combine_2_cards(people=[], count=count): 
        while count < 4:
            players[count].append(shuffled_deck.pop(0))
            count += 1
            return combine_2_cards(players, count)
        return people
    
    
    players = combine_2_cards(people, count)
    players = combine_2_cards(people, count)
    

    【讨论】:

    • 只是一些补充说明。首先,don't give a list as a default argument。其次,我建议不要在方法中更改全局变量。要么将其作为参数提供,要么在方法内创建列表。第三,.pop() 方法会编辑 shuffled_deck 列表,这可能会产生意想不到的后果。
    • 我想我理解你的第一句话。关于在这种情况下使用.pop() 方法,我需要这样做,因为 shuffled_deck 就像在二十一点中使用的真正的纸牌。
    【解决方案3】:

    您可以使用slice(i, None, x) 获取每个xth 元素,从ith 元素开始。

    例如,

    [shuffled_deck[slice(i, None, 4)] for i in range(4)]
    

    将根据您想要的输出将洗好的牌组发给 4 名玩家。 slice 的第二个参数可以用来控制你想要处理多少套牌(这里None 用来洗牌)。

    这是一个更通用的函数。

    def deal(deck, n_players, n_cards=None):
        """Deals a deck to players.
    
        Args:
            n_players (int): Number of players.
            n_cards (int): Number of cards from the deck to deal.
    
        Returns:
            A list containing the hands for each player.
    
        """
        return [deck[slice(i, n_cards, n_players)] for i in range(n_players)]
    

    如果你有包含玩家数量的列表,你可以使用

    players = deal(shuffled_deck, len(players))
    

    给他们一个新手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2020-12-18
      • 2018-05-18
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多