Can';t用Python从Euchre hand中移除卡
我试图用Python编程纸牌游戏Euchre,但我遇到了一些错误。我将在下面发布我的代码,然后解释我当前的问题:Can';t用Python从Euchre hand中移除卡,python,list,playing-cards,Python,List,Playing Cards,我试图用Python编程纸牌游戏Euchre,但我遇到了一些错误。我将在下面发布我的代码,然后解释我当前的问题: import random 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__(se
import random
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
#Next four definitions haven't been used yet. Goal was to use these
#to define a numerical vaule to each card to determine which one wins the trick
def getSuit(self):
return self.suit
def value(self):
v=Card.RANK.index(self.rank)
return v
def getValue(self):
print(self.value)
def changeValue(self,newValue):
self.value=newValue
return self.value
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 reshuffle(self):
self.clear()
self.populate()
self.shuffle()
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.")
#These two are the total scores of each team, they haven't been used yet
player_score=0
opponent_score=0
#These keep track of the number of tricks each team has won in a round
player_tricks=0
opponent_tricks=0
deck1=Deck()
#defines the hands of each player to have cards dealt to
player_hand=Hand()
partner_hand=Hand()
opp1_hand=Hand()
opp2_hand=Hand()
trump_card=Hand() #This is displayed as the current trump that players bid on
played_cards=Hand() #Not used yet. Was trying to have played cards removed from
#their current hand and placed into this one in an attempt to
#prevent playing the same card more than once. Haven't had
#success with this yet
hands=[player_hand,opp1_hand,partner_hand,opp2_hand]
deck1.populate()
deck1.shuffle()
print("\nPrinting the deck: ")
print(deck1)
deck1.deal(hands,hand_size=5)
deck1.give(deck1.cards[0],trump_card)
def redeal(): #just to make redealing cards easier after each round
player_hand.clear()
partner_hand.clear()
opp1_hand.clear()
opp2_hand.clear()
trump_card.clear()
deck1.reshuffle()
deck1.deal(hands,hand_size=5)
deck1.give(deck1.cards[0],trump_card)
print("\nPrinting the current trump card: ")
print(trump_card)
while player_tricks+opponent_tricks<5:
#Converts players hand into a list that can have its elements removed
Player_hand=[str(player_hand.cards[0]),str(player_hand.cards[1]),str(player_hand.cards[2]),\
str(player_hand.cards[3]),str(player_hand.cards[4])]
print("\nYour hand: ")
print(Player_hand)
played_card=str(raw_input("What card will you play?: "))#converts input into a string
if played_card==Player_hand[0]: #crudely trying to remove the selected card
Player_hand.remove(Player_hand[0]) #from player's hand
if played_card==Player_hand[1]:
Player_hand.remove(Player_hand[1])
if played_card==Player_hand[2]:
Player_hand.remove(Player_hand[2])
if played_card==Player_hand[3]:
Player_hand.remove(Player_hand[3])
if played_card==Player_hand[4]: #received the 'list index out of range' error
Player_hand.remove(Player_hand[4]) #here. Don't know why this is an error since
#Player_hand has 5 elements in it.
opp1_card=opp1_hand.cards[0] #just having a card chosen to see if the game works
#will fix later so that they select the best card
#to play
partner_card=partner_hand.cards[0]
opp2_card=opp2_hand.cards[0]
print("First opponent plays: ")
print(opp1_card)
print("Your partner plays: ")
print(partner_card)
print("Second opponent plays: ")
print(opp2_card)
trick_won=[0,1] #Just used to randomly decide who wins trick to make sure score is
#kept correctly
Trick_won=random.choice(trick_won)
if Trick_won==0:
print("\nYou win the trick!")
player_tricks+=1
if Trick_won==1:
print("\nOpponent wins the trick!")
opponent_tricks+=1
if player_tricks>opponent_tricks:
print("\nYou win the round!")
if opponent_tricks>player_tricks:
print("\nOpponont wins the round!")
print("\nGOOD GAME") #Just to check that game breaks loop once someone wins the round
随机导入
类别卡(对象):
''定义卡类''
等级=['9'、'10'、'J'、'Q'、'K'、'A']#制作卡片的等级和套装列表
西装=['c'、's'、'd'、'h']
定义初始(自我、等级、套装):
self.rank=rank
西服
定义(自我):
代表=自我排名+自我套装
退货代表
#接下来的四个定义尚未使用。我们的目标是使用这些工具
#为每张牌定义一个数字值,以确定哪张牌赢了这个把戏
def getSuit(自我):
自诉
def值(自身):
v=卡片等级索引(self.RANK)
返回v
def getValue(自身):
打印(自我价值)
def更改值(自身、新值):
self.value=newValue
回归自我价值
班手(对象):
定义初始化(自):
self.cards=[]
定义(自我):
如果是自助卡:
代表=“”
对于self.cards中的卡:
rep+=str(卡片)+'\t'
其他:
rep=“空”
退货代表
def清除(自):
self.cards=[]
def添加(自身、卡):
self.cards.append(卡片)
def赠送(自己、卡片、另一只手):
self.cards.remove(卡)
另一手添加(卡片)
def移除(自身、卡):
self.cards.remove(卡)
舱面(手动):
def填充(自我):
对于卡片中的套装。套装:
对于Card.rank中的等级:
自我添加(卡片(等级、套装))
def随机播放(自):
随机。洗牌(自我。牌)
def改组(自我):
self.clear()
self.populate()
self.shuffle()
def交易(自身、手部、手部尺寸=1):
对于范围内的轮次(手部尺寸):
手牵手:
如果是自助卡:
顶级卡片=自我卡片[0]
自我给予(顶牌,手牌)
其他:
打印(“卡片用完”)
#这两个是每个队的总分,还没有使用过
球员得分=0
对手得分=0
#这些记录了每队在一轮比赛中赢得的花招数量
玩家技巧=0
对手技巧=0
甲板1=甲板()
#定义每个玩家要发牌的手
玩家手=手()
搭档手=手()
opp1_hand=hand()
opp2_hand=hand()
王牌牌=手牌()#这显示为玩家竞价的当前王牌
已玩的牌=手()#尚未使用。试图从游戏中移除纸牌
#他们现在的手放在这只手上,试图
#防止多次玩同一张牌。没有
#成功了吗
手牌=[玩家手牌,对手1手牌,搭档手牌,对手2手牌]
deck1.populate()
deck1.shuffle()
打印(“\n打印组:”)
印刷品(1)
deck1.交易(手,手大小=5)
deck1.给予(deck1.牌[0],王牌)
def redeal():#只是为了在每一轮之后更容易地重新调整卡片的大小
游戏者的手。清除()
伙伴_hand.clear()
opp1_hand.clear()
opp2_手部清洁()
王牌
1.改组
deck1.交易(手,手大小=5)
deck1.给予(deck1.牌[0],王牌)
打印(“\n打印当前王牌:”)
打印(王牌)
当玩家耍花招+对手耍花招时:
打印(“\n您赢了这一轮!”)
如果对手的技巧>玩家的技巧:
打印(“\nPonont赢了这一轮!”)
打印(“\nGOOD GAME”)#只是为了检查一旦有人赢了这一轮,游戏是否会中断循环
到目前为止,我能做到的是创建一副牌,给四名玩家每人发一张五张牌,然后让玩家问他们想玩什么牌。一旦他们玩了一张牌,其他三个玩家(两个对手和一个搭档)就玩他们的牌,然后我随机决定谁赢了这个“把戏”,只是为了看看分数是否保持正确
目前我试图解决的问题是,一旦玩家玩了一张牌,并且把戏结束了,在下一个把戏中,当牌被显示时,他们的手上应该少一张牌,但我无法移除之前玩过的牌,所以玩家的手上仍然有五张牌
你们中有谁知道我做错了什么,以及我如何将所选的卡移除?感谢您的帮助。您得到的
索引器是因为您使用了多个if
s而不是elif
if played_card==Player_hand[0]:
Player_hand.remove(Player_hand[0])
# The next if is still evaluated, with the shortened Player_hand
if played_card==Player_hand[1]:
Player_hand.remove(Player_hand[1])
使用:
但是,是的,使用你创建的类,并使用\uuuuueq\uuuuuu
进行比较。不要发布整个游戏的代码,然后询问如何实现下一个功能,试着将问题归结为如何实现所述功能,以便我们可以调试它。这在我看来是太大了。很抱歉,我担心我没有包含足够的代码来提供帮助,但看起来我是意外地走到了另一个极端。我的问题是以“原始输入”开头的代码块。我的代码中列出了我当前删除所选卡的尝试。我尝试的另一种方法是在原始输入提示后立即输入“Player\u hand.remove(Player\u card)”,不包括我当前的任何if条件。我的想法是,既然我已经玩了现在定义的卡,我可以将它与玩家手上正确的一块匹配,然后移除它,但这也不起作用。另外,有时签出,值得一读。如果我像你建议的那样将接下来的四个“如果”更改为“elifs”,我的代码仍然不会移除所选的卡。如果在原始输入提示之前我有玩家手牌移除(玩家手牌[0])或任何其他索引,我可以移除该卡而不会出现问题。出于某种原因,如果我试图移除玩家输入的特定卡,程序将无法识别任何要移除的内容,并且他们仍然会说我手中有五张卡。嗯,是吗?这是你的程序遇到的错误,我
if played_card==Player_hand[0]:
Player_hand.remove(Player_hand[0])
elif played_card==Player_hand[1]:
Player_hand.remove(Player_hand[1])