在Blackjack(Python)中操纵Ace的值
有人能看一下我的代码,让我知道为什么当玩家的手数小于21时,Ace值没有变为11吗?我在def在Blackjack(Python)中操纵Ace的值,python,python-2.7,Python,Python 2.7,有人能看一下我的代码,让我知道为什么当玩家的手数小于21时,Ace值没有变为11吗?我在defcheckvalue(self)下的FOR循环中实现IF循环时遇到困难。这是最好的方法还是有更好的方法 谢谢 import random rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] suit = ['Diamonds', 'Clubs', 'Hearts', 'Spade']
checkvalue(self)
下的FOR循环中实现IF循环时遇到困难。这是最好的方法还是有更好的方法
谢谢
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 Hand(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):
handvalue = 0
for card in self.value:
handvalue += card_val[card.grab_rank()]
if card.grab_rank() in self.value == 'Ace' and handvalue <= 11:
handvalue = handvalue + 10
return handvalue
playerhand = Hand()
随机导入
排名=['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
套装=[“钻石”、“梅花”、“红心”、“黑桃”]
卡牌号码={'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}
类别卡(对象):
定义初始(自我、等级、套装):
self.rank=等级
西服
定义(自我):
返回str(self.rank)+of'+str(self.suit)
def抓斗套装(自身):
自诉
def抓斗等级(自身):
返回自我等级
def牵引(自):
打印(self.suit+self.rank)
类甲板(对象):
定义初始化(自):
self.cards=[]
就我的职级而言:
对于诉讼中的j:
self.cards.append(卡片(i,j))
定义(自我):
返回str([str(card)表示self.cards中的卡])
def随机播放(自):
随机。洗牌(自我。牌)
def交易(自我):
单卡=self.cards.pop()
退单卡
甲板
班手(对象):
定义初始化(自):
self.value=[]
def命中(自身):
self.value.append(deck.deal())
回归自我价值
定义(自我):
返回str([str(card)表示self.value中的卡])
def校验值(自身):
handvalue=0
对于具有自我价值的卡:
handvalue+=卡片价值[卡片.抓取等级()
如果在计算玩家手牌的值时,self.value=='Ace'和handvalue中的card.grab_rank(),则只需通过迭代self.value
比较最后一张card
,查看它是否是Ace
def checkvalue(self):
handvalue = 0
for card in self.value:
handvalue += card_val[card.grab_rank()]
# Because of how Python scoping works, the card you use here
# is the last card that `for card in self.value` iterated on
# You're not actually comparing every card in `self.value` to 'Ace'
# This conditional is also odd - you probably meant it to be an iteration
# over self.value to see if any card was an Ace
if card.grab_rank() in self.value == 'Ace' and handvalue <= 11:
handvalue = handvalue + 10
return handvalue
def checkvalue(self):
handvalue = 0
has_ace = False
for card in self.value:
handvalue += card_val[card.rank]
if card.rank == 'Ace':
has_ace = True
if has_ace and handvalue <= 11:
handvalue += 10
return handvalue
如果self.value==“Ace”中的card.grab\u rank()是胡言乱语。或者更确切地说,它被解释为
if (card.grab_rank() in self.value) == 'Ace'
card
,这里指的是手中的最后一张卡(因为它在上面的for
循环之外),而不是手中的任何卡。即使是,您也必须删除self.value中的
检查
使代码正常工作的最小更改是:
class Hand(object):
...
def checkvalue(self):
handvalue = 0
for card in self.value:
handvalue += card_val[card.grab_rank()]
if any(card.grab_rank() == 'Ace' for card in self.value) and \
handvalue <= 11:
handvalue += 10
return handvalue
然后您的checkvalue
函数变为
def checkvalue(self):
handvalue = sum(c.value for c in self.value)
if any(c.rank == 'Ace' for c in self.value) and handvalue <= 11:
handvalue += 10
return handvalue
现在调用hand.value
,而不是hand.value()
,这样做更有意义<代码>值
在现实世界中不是一个动作——它是一个名词而不是动词。将其视为属性(或属性,这是@property
装饰器所做的)
注意,我没有在这里定义\uuuuu init\uuuuuu
,因为当您从列表
继承时,这会得到处理,我没有定义\uuuu str\uuuuuu
,因为它或多或少是自己处理的(如果您打印一个列表,它会对其所有成员调用str
),我没有定义“打”
,因为手真的不应该打自己。无论是从封装的角度来看,它都没有意义(它现在依赖于名为deck
的对象,该对象有一个方法deal
),而且“打击”是手的责任也没有意义。也许考虑……/P>
class Player(object):
# has a self.hand attribute and a `self.game.deck` attribute
def hit(self):
self.hand.append(self.game.deck.deal(1))
玩家点击,手牌不点击。您希望if在for循环中,否则您将只检查一张牌(手上最后一张牌)。我们也只关心他们是否至少有一张A,因为使用多张as 11会失败
尝试:
def校验值(自身):
handvalue=0
has_ace=False
对于具有自我价值的卡:
if card.grab_rank()=“A”:
has_ace=True
handvalue+=卡片价值[卡片.抓取等级()
如果有A和handvalue
class Hand(list):
# in other languages, I might literally define this as a type alias of `[]Card`
# in Python, inheriting from list is fine.
@property
def value(self):
total = [c.value for c in self]
if any(c.rank == 'Ace' for c in self) and total <= 11:
total += 10
return total
class Player(object):
# has a self.hand attribute and a `self.game.deck` attribute
def hit(self):
self.hand.append(self.game.deck.deal(1))
def checkvalue(self):
handvalue = 0
has_ace = False
for card in self.value:
if card.grab_rank() == 'Ace':
has_ace = True
handvalue += card_val[card.grab_rank()]
if has_ace and handvalue <= 11:
handvalue += 10
return handvalue