-1

我对此很陌生,我不知道如何解决这个问题。我有一个庄家,然后它在开始时询问有多少玩家将玩。然后每个玩家和经销商从一副牌中获得 2 张牌。问题是,例如,如果我有 3 个玩家,则代码还会列出下一个玩家的前 2 张牌。

4

5 回答 5

1

您可能需要每个玩家的列表。字典可能会完成这项工作,例如:

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])))
于 2012-07-30T15:58:13.700 回答
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
于 2012-07-30T16:45:54.157 回答
0

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

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会继续追加。

于 2012-07-30T15:57:18.250 回答
0

你的手是一个数组,所以你只是将新牌附加到同一个数组中,每次打印手牌时,你都在打印同一个数组,只是起初只有 2 张牌,然后是 4 张,然后是 6 张。

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

于 2012-07-30T15:59:25.663 回答
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
于 2012-07-30T17:32:11.640 回答