更新纸牌游戏中的得分

0 投票
1 回答
582 浏览
提问于 2025-04-16 14:42

我刚开始学习Python。

我需要帮助更新一个纸牌游戏的分数。

这个游戏的得分规则是这样的:

玩家A或B有一对牌: 分数加1
玩家A向玩家B(反之亦然)要一张牌,如果对方有: 分数加1
如果玩家B没有这张牌,玩家A就要抽一张牌。如果抽完后有一对: 分数加2

我明白这个逻辑,但不知道怎么把它们连接起来。

我试着在我的函数里手动加分,但这样变得很麻烦和复杂 :(

我想我需要为分数创建一个新函数,然后在其他函数里调用它吗?

我会很感激你的指导,

谢谢!

1 个回答

1

这里有一些代码可以帮助你入门:

class Player:
  def hasPair(self):
    haveIt = False
    #write logic here to see if you have it
    return haveIt
  def hasCard(self,card):
    haveIt = False
    #write logic here to see if this player has the card
    return haveIt
  def drawCard(self):
    #write logic here
    pass
  def ask(self,player,card):
    return player.hasCard(card)
  def increment_score(self,by=1):
    self.score += by

def updateScores(a,b,card):        
  if a.hasPair(): a.increment_score()
  if b.hasPair(): b.increment_score()
  if a.ask(b,card): 
    a.increment_score()
  else:
    a.drawCard()
    if a.hasPair(): a.increment_score(2)
  if b.ask(a,card):
    b.increment_score()
  else:
    b.drawCard()
    if b.hasPair(): b.increment_score(2)    

撰写回答