有人可以告訴我如何爲玩家手中的牌分配數值嗎?我卡在self.checkvalue
,因爲我目前設置的是我使用字符串來顯示排名和西裝。我似乎無法弄清楚如何獲得玩家手中每張牌的等級並使用它來查找字典中的數值。指定數值在Python中舉行二十一點遊戲
謝謝!
這是我到目前爲止有:
import random
rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
suit = ['Diamonds', 'Clubs', 'Hearts', 'Spade']
card_val = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':1}
class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def grab_suit(self):
return self.suit
def grab_rank(self):
return self.rank
def draw(self):
print(self.suit + self.rank)
class Deck(object):
def __init__(self):
self.cards = []
for i in rank:
for j in suit:
self.cards.append(Card(i,j))
def __str__(self):
return str([str(card) for card in self.cards])
def shuffle(self):
random.shuffle(self.cards)
def deal(self):
single_card = self.cards.pop()
return single_card
deck = Deck()
class PlayerHand(object):
def __init__(self):
self.value = []
def hit(self):
self.value.append(deck.deal())
return self.value
def __str__(self):
return str([str(card) for card in self.value])
def checkvalue(self):
for card in self.value:
handvalue += card_val[(self.value)]
hand = PlayerHand()
謝謝,但我仍然無法實現您在Python 2中建議的更改。更具體地說,打印功能。你知道我可以在Python 2中做到這一點嗎? – paulnsn
@paulnsn從'print'調用中刪除括號,或者在文件的頂部添加'from __future__ import print_function'。 – FMc