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:
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.