2

im new at learning python (first programming language learned at university was C) and want to write a little card-game step by step as a practice. More specifically it's about a Texas Holdem Poker game.

I wrote a class cPlayer with all relevant attributes.

class cPlayer(object):
    def __init__ (self, id=0, name='', hand=[], credit=1000):
        self.id=id
        self.name=name
        self.hand=hand
        self.credit=credit

Then I made a list of playing cards, and shuffled it.

deck=[]
for i in range(0,4):
    for j in range(0,13):
        deck.append([value[j], suit[i]])
shuffle(deck)

Further I have a List, which is filled with 6 cPlayer via a for-loop.

list_of_players=[]    
for i in range(PLAYERS):
    x=cPlayer()
    x.id=i+1
    x.name=i+1
    for j in range(2):
        x.hand.append([0,0])
    list_of_players.append(x)

In the first step, i just want to give each cPlayer a hand, which is a list of two cards, which again is a list of two values (value and suit). Therefor I wrote a function:

def deal_hole_cards():
    for i in range(2):
        for j in range(len(list_of_players)):
            list_of_players[j].hand[i]=deck.pop()

To check everything I made some print functions. The relevant one is this one:

def print_hands():
    print '==HANDS=='
    for i in range(len(list_of_players)):
        print dict_player[list_of_players[i].name] +'   ',
        print_card(list_of_players[i].hand[0])
        print_card(list_of_players[i].hand[1])
        print ''

What I get now, is something like this:

output

Each "Player" gets the same hand after the loop. Somehow all previous hands get overwritten by the last latest use of pop(). This must be a reference problem. But how can I fix it, so that each player has a different hand of cards? I googled and found some similiar problems but couldn't find the right solution for me.

Note: For the prints I translate the card-values and the player-names via dictionaries that work fine.

awesoon
  • 32,469
  • 11
  • 74
  • 99
Maarrco
  • 23
  • 3

1 Answers1

0

So as noted you should not use a mutable as default value in your signature, but this is not your main problem. I think you are not unpacking the values correctly. You should not append, but overwrite the values. During creation you should set the default:

x.hand = [[0,0], [0,0]]
list_of_players.append(x)

And when adding popping the deck, use this method instead of append:

for j in range(len(list_of_players)):
    list_of_players[j].hand = [deck.pop(),deck.pop()]

By using append you are referencing the same list as you thought.

Whole version:

from random import shuffle

PLAYERS = 6

class cPlayer(object):
    def __init__ (self, id=0, name='', hand=[], credit=1000):
        self.id=id
        self.name=name
        self.hand=hand
        self.credit=credit


deck = []
for i in range(0,4):
   for j in range(0,13):
      deck.append([i,j])

shuffle(deck)

list_of_players = []
for i in range(PLAYERS):
    x=cPlayer()
    x.id=i+1
    x.name=i+1
    x.hand = [[0,0], [0,0]]
    list_of_players.append(x)


def deal_hole_cards():
    for j in range(len(list_of_players)):
        list_of_players[j].hand = [deck.pop(),deck.pop()]


deal_hole_cards()

for i in list_of_players:
    print i.id, i.hand

Output:

1 [[0, 12], [0, 7]]
2 [[3, 9], [1, 1]]
3 [[1, 6], [3, 3]]
4 [[3, 5], [2, 9]]
5 [[2, 3], [1, 5]]
6 [[1, 11], [2, 4]]

I know I'm missing the unicode stuff, but that should be trivial.

salparadise
  • 5,699
  • 1
  • 26
  • 32
  • Thanks a lot! Changing "x.hand.append([0,0])" to "x.hand = [[0,0], [0,0]]" was just the right solution. – Maarrco Oct 18 '15 at 09:29