二十一点实施 - 如何为卡分配值?

时间:2015-04-22 02:30:28

标签: python user-interface dictionary

我试图用Python编写程序,在用户和经销商之间玩Blackjack。

对于卡片,我有:

suits = ['spades','hearts','diamonds','clubs']
ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']

def create_deck():
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append((suit,rank))
    return deck

def shuffle(deck):
    for i in range(100):
        card = deck.pop(randint(0,51)) 
        deck.append(card)

使用列表中的卡片组,制作字典的最简单方法是将每张卡映射到其面值?

3 个答案:

答案 0 :(得分:2)

看起来您的代码应该有效,是否收到错误?

到目前为止,最有趣的方法是简单地使用数字1-52(不是0-51)并使用模数4表示套装,模数13表示面值。此范围内的数字都不具有共同的模数值。

否则,对象。

答案 1 :(得分:1)

来自the comments of your question

"从根本上说,我想手动构建字典: {('spades','ace'):1, ('spades','two'):2}等等,但有办法吗?  更有效地建立字典?"

您可以通过以下方式完成此操作:

def create_deck(suits,ranks):
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append((suit,rank))
    return deck

suits = ['spades','hearts','diamonds','clubs']
ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']



deck=create_deck(suits,ranks)

d={}                      #empty dictionary
i=1                       #start with i=1
for card in deck:
    d[card]=i             #add to dictionary
    if i==13:              
        i=1               #reverse i to 1 every 13 iterations
    else:
        i+=1              #otherwise increment i

>>> d
{('diamonds', 'eight'): 8, ('hearts', 'five'): 5, ('diamonds', 'two'): 2, ('diamonds', 'nine'): 9, ('diamonds', 'jack'): 11, ('hearts', 'eight'): 8, ('diamonds', 'king'): 13, ('spades', 'jack'): 11, ('clubs', 'jack'): 11, ('spades', 'nine'): 9, ('spades', 'two'): 2, ('clubs', 'two'): 2, ('hearts', 'queen'): 12, ('spades', 'ace'): 1, ('hearts', 'three'): 3, ('diamonds', 'six'): 6, ('hearts', 'ace'): 1, ('spades', 'four'): 4, ('spades', 'three'): 3, ('clubs', 'four'): 4, ('hearts', 'nine'): 9, ('spades', 'seven'): 7, ('spades', 'queen'): 12, ('hearts', 'six'): 6, ('spades', 'ten'): 10, ('clubs', 'seven'): 7, ('diamonds', 'queen'): 12, ('hearts', 'ten'): 10, ('clubs', 'three'): 3, ('diamonds', 'seven'): 7, ('clubs', 'ten'): 10, ('hearts', 'king'): 13, ('hearts', 'seven'): 7, ('diamonds', 'ten'): 10, ('clubs', 'six'): 6, ('clubs', 'nine'): 9, ('spades', 'six'): 6, ('diamonds', 'five'): 5, ('hearts', 'four'): 4, ('spades', 'five'): 5, ('diamonds', 'four'): 4, ('clubs', 'queen'): 12, ('diamonds', 'three'): 3, ('clubs', 'eight'): 8, ('hearts', 'two'): 2, ('clubs', 'ace'): 1, ('clubs', 'king'): 13, ('hearts', 'jack'): 11, ('diamonds', 'ace'): 1, ('spades', 'eight'): 8, ('spades', 'king'): 13, ('clubs', 'five'): 5}

注意:这是有效的,因为您之前创建的deck列表是按数字顺序排列的。

<强>更新

如果deck列表尚未排序,您可以这样做:

def create_deck(suits,ranks):
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append((suit,rank))
    return deck

suits = ['spades','hearts','diamonds','clubs']
ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']
vals = dict(zip(ranks, [i for i in range(1,14)])) #create a value dictionary

deck = create_deck(suits,ranks)

d={}                      #create an empty dictionary
for card in deck:
    d[card]=vals[card[1]] #add card '(spades,king)' as index and vals['king'] (which is 13) as the value

vals词典的格式为:

{'king': 13, 'seven': 7, 'queen': 12, 'ten': 10, 'ace': 1, 'nine': 9, 'six': 6, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'jack': 11, 'eight': 8}

答案 2 :(得分:0)

您还可以编写一个类“卡片”,以便卡片是该类的对象,其中包含suit和rank作为属性。因此,您不需要字典。 这是一个例子:

suits = ['spades','hearts','diamonds','clubs']
ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']


class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

def create_deck():
    deck = []
    for suit in suits:
        for rank in ranks:
            deck.append(Card(rank, suit))
    return deck

当您的函数create_deck()被执行时,它将返回带有52个Card对象的列表'deck',您现在可以访问这些对象并使用例如这样:

>>> from cards import *
>>> deck = create_deck()    # create the deck
>>> len(deck)
52
>>> deck[12]                # show contents are objects of the class
<cards.Card object at 0x1006ba6d8>
>>> deck[12].suit, deck[12].rank
('spades', 'king')
>>> from random import shuffle   # following lines demonstrate shuffle Shashank mentioned
>>> shuffle(deck)
>>> deck[12].rank, deck[12].suit
('seven', 'clubs')
>>> 

HTH