如何删除卡片游戏python中的重复项

时间:2015-03-27 19:39:55

标签: python dictionary

#-*- 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)

您好。我正在尝试制作纸牌游戏,但我不确定如何使所有卡片都独一无二,就像在一副牌中一样。换句话说,我不想要任何确切的重复。我遇到的另一个问题是,当我尝试向字典rem添加信息时,不会打印具有相同值的所有卡。感谢。

2 个答案:

答案 0 :(得分:1)

就个人而言,这就是我要做的事情:

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)

如果你想知道为什么这里有两个列表的原因。我希望有一个功能可以将卡片打印到屏幕上,而另一个功能则更适用于用户不会看到的内部内容。

答案 1 :(得分:0)

您应该将已挑选的卡存储在列表中而不是字典中。这样,它可以跟踪具有相同价值或相同诉讼的多张牌。

调整当前代码的最简单方法是添加一个while循环,这样可以选择尚未存储在hand(已选择的卡列表)中的卡片。

# ...
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)

或者,将卡存储在一个集合而不是列表中:

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

您还可以将输出简化一点,例如:像这样:

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)