Python 21点游戏重新洗牌问题编辑
我正在尝试制作一个21点游戏,在每一轮新游戏之前,程序会检查以确保每个玩家有7张牌。如果没有,甲板将被清理、重新填充和重新洗牌。我已经解决了大部分问题,但由于某种原因,在每笔交易开始时,它都会不止一次地重新洗牌,我不知道为什么。请帮忙。 以下是我目前掌握的情况: (另外,导入的卡和游戏模块不是问题的一部分,我相当确定我的问题在于我的BJ_Deck类的deal()函数。) 班手(对象): “一手扑克牌。” def初始化(自身): self.cards=[]Python 21点游戏重新洗牌问题编辑,python,Python,我正在尝试制作一个21点游戏,在每一轮新游戏之前,程序会检查以确保每个玩家有7张牌。如果没有,甲板将被清理、重新填充和重新洗牌。我已经解决了大部分问题,但由于某种原因,在每笔交易开始时,它都会不止一次地重新洗牌,我不知道为什么。请帮忙。 以下是我目前掌握的情况: (另外,导入的卡和游戏模块不是问题的一部分,我相当确定我的问题在于我的BJ_Deck类的deal()函数。) 班手(对象): “一手扑克牌。” def初始化(自身): self.cards=[] def __str__(self):
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + "\t"
else:
rep = "<empty>"
return rep
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
如果name==“main”:
print“这是一个包含扑克牌类的模块。”
原始输入(“\n\n按enter键退出”)
这是游戏模块:
class Player(object):
""" A player for a game. """
def __init__(self, name, score = 0):
self.name = name
self.score = score
def __str__(self):
rep = self.name + ":\t" + str(self.score)
return rep
def询问是否(问题):
“问一个是或不是的问题。”
响应=无
当响应不在“y”、“n”中时:
响应=原始输入(问题).lower()
返回响应
def ask_编号(问题、低、高):
“”“请求一个范围内的数字。”“”
响应=无
响应不在范围内时(低、高):
响应=int(原始输入(问题))
返回响应
如果name==“main”:
打印“您直接运行此模块(但未“导入”它)。”
原始输入(“\n\n按enter键退出。”)您在循环内部反复检查,当您分发卡片时,我认为卡片组正在减少(看不到
卡片组。请给出代码的方法以确定)
您可能只想检查一次,请将检查移动到循环外部
def deal(self, hands, per_hand=1):
for rounds in range(per_hand):
if len(self.cards) <= 7 * len(hands):
print "Reshuffling the deck."
self.cards = []
self.populate()
self.shuffle()
for hand in hands:
top_card=self.cards[0]
self.give(top_card, hand)
def交易(self,hands,per_hand=1):
对于范围内的回合(每手):
如果len(self.cards)您在循环内部反复检查,并且在分发卡片时,卡片组正在减少,我认为(无法看到卡片组。请给出代码上的
方法以确定)
您可能只想检查一次,请将检查移动到循环外部
def deal(self, hands, per_hand=1):
for rounds in range(per_hand):
if len(self.cards) <= 7 * len(hands):
print "Reshuffling the deck."
self.cards = []
self.populate()
self.shuffle()
for hand in hands:
top_card=self.cards[0]
self.give(top_card, hand)
def交易(self,hands,per_hand=1):
对于范围内的回合(每手):
如果len(self.cards)用于手牵手:
您真的要为每只手运行该逻辑吗?对于手牵手:
你真的想为每只手运行该逻辑吗?Nosklo指出了一个问题(在循环中检查),但还有第二个问题
状况
if len(self.cards)>=7*(len(hands)):
正在检查牌的数量是否大于所需的数量,如果是,则清除牌组、填充和洗牌
当与循环内的检查相结合时,它将在每次开始下一轮时重新填充和洗牌牌
因此,您可能需要以下内容:
if len(self.cards) <= 7*(len(hands)):
print "Reshuffling the deck."
self.cards=[]
self.populate()
self.shuffle()
for rounds in range(per_hand):
for hand in hands:
top_card=self.cards[0]
self.give(top_card, hand)
if len(self.cards)Nosklo指出了一个问题(在循环内部检查),但还有第二个问题
状况
if len(self.cards)>=7*(len(hands)):
正在检查牌的数量是否大于所需的数量,如果是,则清除牌组、填充和洗牌
当与循环内的检查相结合时,它将在每次开始下一轮时重新填充和洗牌牌
因此,您可能需要以下内容:
if len(self.cards) <= 7*(len(hands)):
print "Reshuffling the deck."
self.cards=[]
self.populate()
self.shuffle()
for rounds in range(per_hand):
for hand in hands:
top_card=self.cards[0]
self.give(top_card, hand)
如果len(self.cards)没问题,我觉得我检查得太多了。我还以为这会解决问题,但它仍然告诉我,它在每一手牌之后会重新调整两次。好吧,我觉得我检查得太多了。我也认为这会解决问题,但它仍然告诉我,它在每一手牌之后都会重新调整两次。