How to separate each list

I am very new to this and I do not know how to fix this problem. I have a dealer, and then he asks at the beginning how many players will play. Then, each player, as well as the dealer, is assigned two cards from the deck. The problem is that, for example, if I have 3 players, the code lists the previous 2 cards from one player in the next as well.

-1
source share
5 answers

You probably need a list for each player. The dictionary is likely to do the job, for example:

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])))
+2
source

You need to reset your hand for each player. So your second loop should look bigger:

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)

Otherwise will handbe added to.

+1
source

, , , , , 2 , 4, 6.

, .

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
0

( ) , . reset .

Since you probably have to follow all the hands during the game, it would be advisable to create a separate list list containing one sub-list for distribution, as well as a separate one for each player. They can be stored in a list of lists and indexed as needed.

You name the players: Player 1, Player 2, etc., by virtue of their numbering from 1, so it would be possible to save the dealer’s hand in index 0 and put the cards of player 1 in the hands of [player + 1], and so on. Here is a sample code:

#### 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 hand
hands.extend([] for player in range(players))  # for each player 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)

Conclusion:

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

All Articles