uniqxx
uniqxx

Reputation: 13

Move item in one list to another for a card game Python

Ok so I'm trying to make a poker game in Python.. and I want to move the used cards from the list to another list where I will store the used cards. I'm using this code :

deck = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\
         'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\
         'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\
         'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H']

used = []
p1 = []
p2 = []
a = 0

while (a < 2):
    drawn_card = random.choice(deck)
    deck.append(drawn_card)
    deck = [f for f in deck if f not in used]
    p1.append(drawn_card)
    a+=1

# Don't read anything below this line

card1 = p1[0]
card2 = p1[1]
f_card1 = "Name/Suit"
f_card2 = "Name/Suit"

# Formatting Player1's first drawn card

if card1[0] == "A":
    f_card1 = "Ace"
elif card1[0] == "K":
    f_card1 = "King"
elif card1[0] == "Q":
    f_card1 = "Queen"
elif card1[0] == "J":
    f_card1 = "Jack"
else:
    f_card1 = card1[0]


if card1[1] == "S":
    f_card1 = f_card1 + " of Spades"
elif card1[1] == "D":
    f_card1 = f_card1 + " of Diamonds"
elif card1[1] == "C":
    f_card1 = f_card1 + " of Clubs"
else:
    f_card1 = f_card1 + " of Hearts"

# Formatting Player1's second drawn card

if card2[0] == "A":
    f_card2 = "Ace"
elif card2[0] == "K":
    f_card2 = "King"
elif card2[0] == "Q":
    f_card2 = "Queen"
elif card2[0] == "J":
    f_card2 = "Jack"
else:
    f_card2 = card2[0]


if card2[1] == "S":
    f_card2 = f_card2 + " of Spades"
elif card2[1] == "D":
    f_card2 = f_card2 + " of Diamonds"
elif card2[1] == "C":
    f_card2 = f_card2 + " of Clubs"
else:
    f_card2 = f_card2 + " of Hearts"

print(f_card1)
print(f_card2)

The while function is where my problem is. I can sometimes get the same cards when I print them. How can I fix that and where is my mistake ? thanks in advance

Upvotes: 1

Views: 456

Answers (3)

Jordan Kelwick
Jordan Kelwick

Reputation: 46

while (a < 2):
    drawn_card = random.choice(deck)
    used.append(drawn_card)
    deck = [f for f in deck if f not in used]
    p1.append(drawn_card)
    a+=1

used.append(drawn_card)

Upvotes: 1

mhawke
mhawke

Reputation: 87124

The simple fix is to use used.append(drawn_card) as others have suggested. A better way is to randomly shuffle your deck and then slice the required number of cards from it:

CARDS_PER_HAND = 2

random.shuffle(deck)
hand = deck[:CARDS_PER_HAND]

However, you are probably dealing multiple hands from the same deck. You can handle that by keeping track of the offset into the shuffled deck and then slice from there, but that's a hassle. Or you can make a copy of deck, shuffle it, and pop off the required number of cards for each hand:

shuffled_deck = deck[:]    # make a copy of deck
random.shuffle(shuffled_deck)
hand1 = [shuffled_deck.pop() for i in range(CARDS_PER_HAND)]
hand2 = [shuffled_deck.pop() for i in range(CARDS_PER_HAND)]
... etc.

Possibly the cleanest way is to use itertools.islice() to take the first n items from an iterator:

from itertools import islice

random.shuffle(deck)
shuffled_deck = iter(deck)    # returns an iterator
hand1 = list(islice(shuffled_deck, CARDS_PER_HAND))
hand2 = list(islice(shuffled_deck, CARDS_PER_HAND))
... etc.

Upvotes: 1

Keiwan
Keiwan

Reputation: 8291

You just need to append the drawn cards to your used list, and not back into deck:

used.append(drawn_card)   #instead of deck.append(..)

Upvotes: 1

Related Questions