(Python)平均分配数字

时间:2014-07-13 08:40:16

标签: python

我为标题道歉。如果有人能想到更具描述性的标题,请告诉我,我会重新发布这个问题,或者编辑标题。 (如果可能的话) 您好,我目前正在开发一款游戏。当发起攻击时,攻击者可以从其他玩家获得潜在的资源编号。如果防御者有资源给我,我很难弄清楚如何为攻击者平均分配这些资源。

#this is how many resources the attacker can get, def's resources
attacker_potential = 250
#this is how many resources the defender has
defender.wood = 100
defender.iron = 25
defender.wheat = 76
defender.gold = 50000
#then the attacker would make off with 
attacker.wood += 75
attacker.iron += 25
attacker.wheat += 75
attacker.gold += 75

另一个例子:

defender.wood = 2500
defender.iron = 2500
defender.wheat = 5000
defender.gold = 5000
#here, the attacker would make off with 62 for each resource. (250 / 4 = 62.5)

这是另一个例子:

defender.wood = 100
defender.iron = 0
defender.wheat = 1
defender.gold = 0
#attacker would make off with:
attacker.wood += 100
attacker.iron += 0
attacker.wheat += 1
attacker.gold += 0

然后是最后一个例子:

defender.wood = 50000 #pretend the rest are 0
attacker.wood += 250 

(我知道攻击者获得了多少资源,其余的数学很容易)。 我刚刚在我的代码中达到了这一点,我花了大约20分钟试图弄清楚它是如何工作的。不过,我觉得答案很简单。

2 个答案:

答案 0 :(得分:3)

与您提供的示例一致的一种算法如下:

  1. 让平均战利品成为攻击者的潜力除以防御者非零资源的数量。循环通过防御者的非零资源,如果其中任何一个小于或等于平均战利品,则将其从防御者中移除并将其交给攻击者。

  2. 如果在步骤1中遇到并移动了小于或等于平均战利品的资源,则重新计算平均战利品并重复步骤1.否则,请继续执行步骤3.

  3. 最后,如果防御者还剩下任何资源,只需重新计算平均战利品并将其从每个资源中移除(将其交给攻击者)。

  4. python中可能的实现如下:

    def step1(dres, aloot, potential):
    
        again = False
    
        ndres = {}
    
        if len(dres) > 0:
    
            avgloot = int(potential / len(dres))
    
            for r in dres:
                if dres[r] <= avgloot:
                    potential -= dres[r]
                    aloot[r] += dres[r]
                    again = True
                else:
                    ndres[r] = dres[r]
    
        return (ndres, aloot, potential, again)
    
    def calculate_loot(dres, potential):
    
        aloot = {'wood':0, 'iron':0, 'wheat':0, 'gold':0}
    
        (dres, aloot, potential, again) = step1(dres, aloot, potential)
    
        while again:
            (dres, aloot, potential, again) = step1(dres, aloot, potential)
    
        if len(dres) > 0:
            avgloot = int(potential / len(dres))
            for r in dres:
                aloot[r] += avgloot
    
        return aloot
    
    potential = 250
    
    for dres in [
            {'wood':100,  'iron':25,   'wheat':76,   'gold':50000},
            {'wood':2500, 'iron':2500, 'wheat':5000, 'gold':5000 },
            {'wood':100,  'iron':0,    'wheat':1,    'gold':0    },
            {'wood':0,    'iron':0,    'wheat':0,    'gold':50000}
        ]:
    
        print(dres)
        print(calculate_loot(dres, potential))
        print(' ')
    

    Online Demo

答案 1 :(得分:0)

循环整数资源。如果您真的想要分割资源,可以检查剩余资源的总量是否小于资源数量并均匀分配。

def distribute(potential, defender_resources):
    attacker_resources = [0] * len(defender_resources)

    while potential and any(defender_resources):
        for i in range(len(defender_resources)):
            if potential and defender_resources[i]:
                defender_resources[i] -= 1
                attacker_resources[i] += 1
                potential -= 1

    return attacker_resources

print distribute(250, [100,0,1,0])
print distribute(250, [100,25,76,5000])
print distribute(250, [2500,2500,2500,2500])
print distribute(250, [5000])