【问题标题】:Calculate score in a pyramid score system在金字塔分数系统中计算分数
【发布时间】:2010-11-07 13:29:51
【问题描述】:

我正在尝试为大量用户计算游戏分数,但我还没有真正得到它。这是一个金字塔游戏,您可以在其中邀请人,而您邀请的人在关系树中位于您的下方。

所以,如果我邀请 X 和 X 邀请 Y,我会从他们俩那里得到回扣。假设 10%^steps...

所以我从 X 那里得到他分数的 10%,从 Y 那里得到 1%,X 从 Y 那里得到 10%。

所以要计算这个,我认为每个“玩家”都有一个计算他的总分的函数。这个函数必须是递归的,并且“知道”它在树中的距离,以便它能够返回正确的值。

def get_score(player):
    if children:
        score = player.points
        for child in children:
            score += child.points*math.pow(.1, get_ancestors(child))
            score += get_score(child)
        return score
    else:
        return player.points

但这不能正常工作,它给出了我认为在某些级别但在其他级别不正确的值。所以认为我的功能被破坏了。有人知道如何解决这个问题吗?

【问题讨论】:

  • 我希望你不会全力以赴麦道夫 :)
  • 不抱歉,这其实是一款ARG游戏,有钱,玩家不用花钱,所以不用担心......

标签: python recursion


【解决方案1】:

我怀疑这两行

score += child.points*math.pow(.1, get_ancestors(child))
score += get_score(child)

这是一个简单的递归结构,所以我认为像下面这样就足够了

score += get_score(child)*.1

递归美会自己照顾自己

你也不需要'if children:'检查 有帮助吗

def get_score(player):
    score = player.points
    for child in children:
        score += get_score(child)*.1
    return score

【讨论】:

  • 你说的太对了,我怎么会错过这个,我肯定需要一些递归练习。非常感谢。
  • 递归基本上是如此简单,以至于它绊倒了我们复杂的头脑
【解决方案2】:

这可以有非常不同的实现,取决于分数的计算方式:

  • 您是否需要实时传播每个增益的结果?在这种情况下,你从金字塔的底部开始,直到顶部给出反馈。

  • 你能负担得起在游戏结束时为每个人计算的结果吗?在这种情况下,您只需为每个玩家设置一个方法,然后只调用顶部的那个。

第二个选项的 E.G

您使用了函数式方法。虽然这是有效的,但我更喜欢 OO,所以我会这样:

class Player(object) :

    def __init__(self) :
        self.score = 0;
        self.children = []

    def updateScore(self) :
        self.score = self.score + sum(((children.score * 10 / 100) for children in self.children))


class Pyramid(object) :

    def __init__(self) :
        self.top_child = Player()

    def updateScore(self, player = None) :

      if player == None :
          player = self.top_child

      for child in player.children :
           self.updateScore(child)
           child.updateScore()

您可能希望使用 itertools 来减少 CPU 和内存密集型。

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 2019-03-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多