如何分隔每个列表

时间:2012-07-30 15:49:55

标签: python python-2.7

我对此很新,我不知道如何解决这个问题。我有一个经销商然后它在一开始就询问将要玩多少玩家。然后每个玩家和经销商从一个牌组中分配2张牌。问题是,例如,如果我有3个玩家,代码也会列出下一个玩家的前两张牌。

5 个答案:

答案 0 :(得分:2)

你可能想要每个玩家一个列表。字典可能会完成这项工作,例如:

hands = {}
...
for player in players:
    hand = hands[player] = []
    for j in range(2):
         c = deck.pop()
         hand.append(c)


for player in players:
    print('{}: {}'.format(player, ', '.join(hands[player])))

答案 1 :(得分:1)

您需要为每位玩家重置手牌。所以你的第二个循环看起来应该更像:

for i in range(players):
   hand = []
   for j in range(2):
      c = deck.pop()
      hand.append(c)
   print "Player " + str(i+1) + ":"
   print "Cards: " + cards.hand_display(cards)

否则hand将继续附加到。

答案 2 :(得分:0)

你的手是一个单独的阵列,所以你只是将新卡附加到同一个阵列,每次你打印手,你打印相同的阵列,只有一开始它只有2张卡,然后4,那么6。

你应该做的是使用字典或数组数组。

答案 3 :(得分:0)

import random

class Cards(object):
    suit  = ['H', 'D', 'S', 'C']
    value = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

    def __init__(self):
        self.deck = [v+s for s in Cards.suit for v in Cards.value]

    def shuffle(self):
        random.shuffle(self.deck)

    def deal(self, n):
        res, self.deck = self.deck[:n], self.deck[n:]
        return res

show_hand = ' '.join

def main():
    num_players = 3
    deck = Cards()
    deck.shuffle()

    dealer  = deck.deal(2)
    players = [deck.deal(2) for i in xrange(num_players)]

    print('Hands:')
    print('  Dealer: {}'.format(show_hand(dealer)))
    for i in xrange(num_players):
        print('Player {}: {}'.format(i+1, show_hand(players[i])))

if __name__=="__main__":
    main()

结果

Hands:
  Dealer: JH 9H
Player 1: AS 2D
Player 2: QD 8H
Player 3: 10H 6D

答案 4 :(得分:0)

您的代码(现已删除)的问题在于您将每个玩家手牌都附加到同一个列表中。简单的解决方法是在循环顶部重置变量。

由于您可能需要在玩游戏时跟踪所有牌局,因此创建一个单独的列表列表是有意义的,该列表包含经销商手牌的一个子列表,以及每个玩家的单独列表。这些可以存储在列表列表中,并根据需要编入索引。

你正在调用玩家:玩家1,玩家2等,实际上是从1开始编号,所以可以将经销商的牌存放在索引0中,并将玩家1的牌放在手中[玩家+1] ], 等等。这是一些示例代码:

#### testing scaffold #######################
import random

class Cards(object):
    def __init__(self):
        deck = self._deck = []
        for rank in "A23456789JQK":
            for suit in "CDHS":
                deck.append(rank+suit)

    def deck(self):
        return self._deck

    def hand_display(self, player_num):
        return ' '.join(hands[player_num])

cards = Cards()
players = 3
##########################################

deck = cards.deck()
random.shuffle(deck)
hands = [[]]  # for dealer's hand
hands.extend([] for player in range(players))  # for each player's hand
DEALER = 0
CARDS_PER_HAND = 2

print "Hands:"
for i in xrange (CARDS_PER_HAND):
    cd = deck.pop()
    hands[DEALER].append(cd)
print "Dealer: " + cards.hand_display(DEALER)

for player_num in [player+1 for player in range(players)]:
    for j in xrange(CARDS_PER_HAND):
        c = deck.pop()
        hands[player_num].append(c)
    print "Player " + str(player_num) + ":"
    print "Cards: " + cards.hand_display(player_num)

输出:

Hands:
Dealer: 6H 5H
Player 1:
Cards: 7H 3H
Player 2:
Cards: 4D JD
Player 3:
Cards: 3S 4S