我试图将纸牌游戏Euchre编程为python,我在我的代码中遇到了障碍。在每轮开始时,玩家轮流决定他们是否希望当前面朝上的牌代表该轮的特朗普(如果心脏正面朝上,那么心脏将成为问题的王牌)。
我给玩家第一个选择,决定他们是否想要调用当前的特朗普,如果他们通过,那么让其他三个玩家(全部由计算机控制)决定是否要打电话给特朗普。目前,我想把它放在哪里,如果电脑里至少有一张牌与他们手中的王牌套装相匹配,那么他们就会调出特朗普,否则我也会让它们过去。
我的问题是这样的:即使一台电脑手里拿着一张特朗普西装的卡片,他们仍会传递特朗普,好像他们没有特朗普套装的卡牌一样。因此,由于某些原因,我的代码没有正确检查计算机手中每张卡的套装。你们任何人都可以弄清楚这里出了什么问题:
if 'h' in Trump_card[0]:
trump='h'
print("Hearts is trump.")
if 'd' in Trump_card[0]:
trump='d'
print("Diamonds is trump.")
if 's' in Trump_card[0]:
trump='s'
print("Spades is trump.")
if 'c' in Trump_card[0]:
trump='c'
print("Clubs is trump.")
Called_trump=raw_input("Will you call trump? (y/n): ").lower()
if Called_trump=='y':
print("\nPlayer calls trump.")
if Called_trump=='n':
if trump==Opp1_hand[0]:
print("First opponent calls trump.")
elif trump==Opp1_hand[1]:
print("First opponent calls trump.")
elif trump==Opp1_hand[2]:
print("First opponent calls trump.")
elif trump==Opp1_hand[3]:
print("First opponent calls trump.")
elif trump==Opp1_hand[4]:
print("First opponent calls trump.")
else:
print("First opponent passes.")
if trump in Partner_hand:
print("Partner calls trump.")
else:
print("Partner passes.")
if trump in Opp2_hand:
print("Second opponent calls trump.")
else:
print("Second opponent passes.")
我用Opp1_hand尝试的是明确地拿出每张牌并检查是否找到了王牌套装,如果是,那么他们就会打电话给特朗普。然后,如果Opp1通过,它将给我的伴侣一个机会,看看他们是否在任何地方都有特朗普套装。然后如果伙伴通过,第二个对手将有机会打电话给特朗普。感谢任何人提供的任何帮助。
我构建Opp1_hand和Trump_card的方式是包含套装和卡片值的字符串列表(例如,黑桃王牌被写为As)。我将列出用于创建所有卡片的代码以及我如何创建Opp1_hand和Trump_card以及以下内容:
class Card(object):
'''defines card class'''
RANK=['9','10','J','Q','K','A'] #list of ranks & suits to make cards
SUIT=['c','s','d','h']
def __init__(self,rank,suit):
self.rank=rank
self.suit=suit
def __str__(self):
rep=self.rank+self.suit
return rep
class Hand(object):
def __init__(self):
self.cards=[]
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)
def remove(self,card):
self.cards.remove(card)
class Deck(Hand):
def populate(self):
for suit in Card.SUIT:
for rank in Card.RANK:
self.add(Card(rank,suit))
def shuffle(self):
random.shuffle(self.cards)
def deal(self,hands,hand_size=1):
for rounds in range(hand_size):
for hand in hands:
if self.cards:
top_card=self.cards[0]
self.give(top_card,hand)
else:
print("Out of cards.")
deck1=Deck()
player_hand=Hand()
partner_hand=Hand()
opp1_hand=Hand()
opp2_hand=Hand()
trump_card=Hand()
hands=[player_hand,opp1_hand,partner_hand,opp2_hand]
deck1.populate()
deck1.shuffle()
deck1.deal(hands,hand_size=5)
deck1.give(deck1.cards[0],trump_card) #this becomes the trump card players bid on
print("\nPrinting the current trump card: ")
print(trump_card)
#Converts all hands into lists that can have its elements removed
#I'm only including Opp1_hand and Trump_card here but the other three
#are made the same way
Opp1_hand=[str(opp1_hand.cards[0]),str(opp1_hand.cards[1]),str(opp1_hand.cards[2]),\
str(opp1_hand.cards[3]),str(opp1_hand.cards[4])]
Trump_card=[str(trump_card.cards[0])]
所以如何显示Opp1_hand和Trump_hand的示例是:
Opp1_hand=['As','10d','9c','Kh','Jh']
Trump_card=['Qc']
让我感到困惑的是,当我标记什么"特朗普"是(在最上面的第一段代码中)该程序能够正确地挑选出哪种诉讼(' h'''' c' ,' d')在Trump_card中,然后相应地定义了特朗普。但是,当我尝试检查Opp1_hand,Partner_hand和Opp2_hand时,由于某种原因,代码无法识别每张卡的适用性。如果这不是解决这个问题的最佳方式,那么你们中的一个人是否知道更好的方法来确定每张卡的套装是什么?
答案 0 :(得分:0)
您的代码假定指针的内容仅为套装的单个字符。看看你确定特朗普和阅读Euchre规则的方式意味着手可能显示为套装和价值(例如钻石王牌)。如果是这种情况,则Opp_hand [0]可以是'ad'而不是'd',这将使==测试失败。检查特朗普在手中也会失败,因为
x = ['a1', 'b2', 'c3', 'd4', 'e5']
>>> 'b' in x
False
>>> 'b2' in x
True
你需要展示手的构造方式。在你的测试中,还要确保用小写字母构建指针。