views:

181

answers:

2

I am trying to calculate gamescores for a bunch over users and I haven't really got it yet. It is a pyramid game where you can invite people, and the people you invite is placed beneth you in the relations tree.

So if i invite X and X invites Y i get kickback from both of them. Let's say 10%^steps...

So from X i get 10% of his score and 1% from Y, and X get 10% from Y.

So to calculate this i was thinking that each "player" had a function that calculated his total score. This function had to be recursive and "know" how far in the tree it was so that it would kick back the right values.

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

But this doesnt work proper, it gives what i believe is the right values in some levels but not in others. So think my function is broken. Anybody got an idea on how to solve this?

+2  A: 

I doubt these two lines

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

this is a simple recursive structure so i think something like below will suffice

score += get_score(child)*.1

and recursive beauty will take care of itself

you also do not need 'if children:' check so does it help

def get_score(player):
    score = player.points
    for child in children:
        score += get_score(child)*.1
    return score
Anurag Uniyal
You're so absolutely correct, doh how can i have missed this, I definitly need some recursive practice. Thanks alot.
Espen Christensen
recursion is basically so simple that it trips our complex minds
Anurag Uniyal
A: 

This can have very different implementations, depending on the way the score must be calculating :

  • Do you need to propagate the result of each gain, in real time ? In that case , you start from the bottom of the pyramid and give the feedback until the top.

  • Can you afford the result to be calculated at the end of the game for everybody ? In that cas you can just set a method on each player and only call the one at the top.

E.G for the second option

You used a functional approach. While this is valid, I am more into OO so I'll go this way :

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()

You may want to use itertools to make it less CPU and memory intensive.

e-satis