简体   繁体   中英

How to remove duplicates in card game- python

#-*- coding: UTF-8 -*-
import random
import time
NUM_CARDS = 5
CARD_WIDTH = 8
SUITS = [('Spades', '♠'), ('Hearts', '♥'), ('Clubs', '♣'), ('Diamonds', '♦')]
NAMES = {11: 'Jack', 12: 'Queen', 13: 'King', 14: 'Ace'}
def get_card_name(n):  return NAMES.get(n, "%2d" % n)
def card_row(text=''): return "|" + text.center(CARD_WIDTH) + "|"
cards = [list() for _ in range(7)]
rem={}
if True:
for _ in range(NUM_CARDS):
    cv = random.randint(2,14)#value
    cs = random.randint(0,3)#suit
    cards[0].append('_' * (CARD_WIDTH+2))
    cards[1].append(card_row(''))
    cards[2].append(card_row(get_card_name(cv)))
    cards[3].append(card_row('of'))
    cards[4].append(card_row(SUITS[cs][0]))
    cards[5].append(card_row(SUITS[cs][1]*(CARD_WIDTH)))
    cards[6].append(card_row("_" * CARD_WIDTH))
    rem[cv]=cs
print 'Welcome to SANDCRAB. This is your deck of cards.'
for k in rem:
print k,rem[k]
for lst in cards:
print ''.join(lst)

Hello. I am trying to make a card game but I am not sure how to make all cards unique, just like in a deck of cards. In other words, I don't want any exact duplicates. Another problem I have is that when I try to add info to the dictionary rem, all cards with the same value are not printed. Thanks.

Personally this is what I'd do:

card_type = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"]
card_vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
card_suit = ["Spade", "Clubs", "Diamond", "Hearts"]
internal_deck = []
display_deck = []
for i in range(0, len(card_type), 1):
    for j in range(0, len(card_suit), 1):
        internal_deck.append((card_type[i], card_suit[j], card_vals[i]))
        display_deck.append(card_type[i] + " of " + card_suit[j])
for cards in display_deck:
    print(cards)

If you wondering why have two lists here's why. I'd like one to be more functional for printing the cards to the screen and the other to be more functional for internal things that the user wont see.

You should store the cards that have been picked in a list rather than a dictionary. That way, it can keep track of multiple cards with the same value or the same suit.

The simplest way to adapt your current code is to add a while loop, which keeps picking cards not already stored in hand (the list of cards already picked).

# ...
hand = []
for _ in range(NUM_CARDS):
    while True:
        cv = random.randint(2,14)#value
        cs = random.randint(0,3)#suit
        if (cv, cs) not in hand:
            break
    hand.append((cv, cs))
    cards[0].append('_' * (CARD_WIDTH+2))
    cards[1].append(card_row(''))
    cards[2].append(card_row(get_card_name(cv)))
    cards[3].append(card_row('of'))
    cards[4].append(card_row(SUITS[cs][0]))
    cards[5].append(card_row(SUITS[cs][1]*(CARD_WIDTH)))
    cards[6].append(card_row("_" * CARD_WIDTH))
print 'Welcome to SANDCRAB. This is your deck of cards.'
for card in hand:
    print card
for lst in cards:
    print ''.join(lst)

Alternatively, store the cards in a set rather than a list:

hand = set()
while len(hand) < NUM_CARDS:
    hand.add((random.randint(2,14), random.randint(0, 3))

You could also simplify the output quite a bit, eg like this:

def card_output(value, suit_number):
    suit, symbol = SUITS[suit_number]
    return ['_' * (CARD_WIDTH + 2)] + map(card_row, [
        '', 
        NAMES.get(value, "%2d" % value),
        'of',
        suit,
        symbol * CARD_WIDTH, 
        '_' * CARD_WIDTH])

# method 1. randomly select until we have 5 unique values
hand1 = set()
while len(hand1) < NUM_CARDS:
    hand1.add((random.randint(2, 14), random.randint(0, 3)))

# method 2. make a deck then sample 5 cards from it
deck = [(value, suit_number) for value in range(2, 14) for suit_number in range(3)]
hand2 = random.sample(deck, NUM_CARDS)

print 'Welcome to SANDCRAB. This is your deck of cards.'
for hand in [hand1, hand2]:
    for row in zip(*[card_output(value, suit_number) for value, suit_number in hand]):
        print ''.join(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM