在此 OOP 示例中如何使所有对象保持更新?

How can I keep all objects updated in this OOP example?

我不知道具体怎么解释,但我会尽力的。

这是一个简单的 OOP 示例,您可以在其中创建玩家来杀死一条龙。重点是为每个玩家分配特定数量的伤害以对龙造成伤害,以杀死它。

例如,如果一条龙有 200 点生命值,并选择 2 名玩家攻击它,则将分配玩家每人造成 100 点伤害。

OOP 的结构如下:

class Player:

    def __init__(self, name):
        self.name = name
        self.damage_to_deal = {} #Format: {'dragon': damage to deal to him}

class Dragon:

    def __init__(self, name, health):
        self.name = name
        self.health = health
        self.number_of_players_attacking = 0

    #Add a player as someone who'll attack this dragon
    def add_player(self, player):
        self.number_of_players_attacking += 1
        player.damage_to_deal[self.name] = self.calculate_partition_damage()

    def calculate_partition_damage(self):
        if self.number_of_players_attacking == 0:
            self.partition_damage = 0
        else:
            self.partition_damage = self.health/self.number_of_players_attacking #health / number of players = damage to do for each player
        return self.partition_damage

以下是我正在测试的操作:

#Create 3 players
player_A = Player(name='Josh')
player_B = Player(name='Steven')
player_C = Player(name='Robert')

#Create 2 Dragons
dragon_A = Dragon(name='Gamacial', health=475)
dragon_B = Dragon(name='Nibiru', health=150)

#All 3 players will attack the dragon A
dragon_A.add_player(player_A)
dragon_A.add_player(player_B)
dragon_A.add_player(player_C)

#Only player A and B will attack dragon B
dragon_B.add_player(player_A)
dragon_B.add_player(player_B)

#The damage each player will have to... {'to this dragon': x damage} to kill the dragons
print(f'{player_A.name} will have to do: {player_A.damage_to_deal}')
print(f'{player_B.name} will have to do: {player_B.damage_to_deal}')
print(f'{player_C.name} will have to do: {player_C.damage_to_deal}')

这将打印以下内容:

Josh will have to do: {'Gamacial': 475.0, 'Nibiru': 150.0}
Steven will have to do: {'Gamacial': 237.5, 'Nibiru': 75.0}
Robert will have to do: {'Gamacial': 158.33333333333334}

问题在于,由于 Josh 是第一个被派去杀死 Gamacial 和 Nibiru 的玩家,因此他被指派对这些龙造成全部伤害,尽管史蒂文和罗伯特也被派去杀死那条龙.(与龙尼比鲁同样的问题).

换句话说,它永远是最后一个有实际正确伤害要处理的玩家,而其他人总是比计算落后一步,因为他们在考虑之前就被分配了任务数下一位玩家。

因此应该打印:

Josh will have to do: {'Gamacial': 158.33333333333334, 'Nibiru': 75.0}
Steven will have to do: {'Gamacial': 158.33333333333334, 'Nibiru': 75.0}
Robert will have to do: {'Gamacial': 158.33333333333334}

有什么方法可以让所有玩家保持同步吗?所以在 Steven 被分配到与 Josh 相同的龙之后,Josh 得到更新等等?

也许你应该稍后计算,毕竟玩家注册了龙:

class Player:

    def __init__(self, name):
        self.name = name
        self.damage_to_deal = {} #Format: {'dragon': damage to deal to him}

    def deal_damage(self):
        for name in self.damage_to_deal:
            target = self.damage_to_deal[name] 
            self.damage_to_deal[name] = target.health / target.number_of_players_attacking
        return self.damage_to_deal 
        
class Dragon:

    def __init__(self, name, health):
        self.name = name
        self.health = health
        self.number_of_players_attacking = 0

    #Add a player as someone who'll attack this dragon
    def add_player(self, player):
        self.number_of_players_attacking += 1
        player.damage_to_deal[self.name] = self #  calculate later

注册:

#Create 3 players
player_A = Player(name='Josh')
player_B = Player(name='Steven')
player_C = Player(name='Robert')

#Create 2 Dragons
dragon_A = Dragon(name='Gamacial', health=475)
dragon_B = Dragon(name='Nibiru', health=150)

#All 3 players will attack the dragon A
dragon_A.add_player(player_A)
dragon_A.add_player(player_B)
dragon_A.add_player(player_C)

#Only player A and B will attack dragon B
dragon_B.add_player(player_A)
dragon_B.add_player(player_B)

然后计算:

#The damage each player will have to... {'to this dragon': x damage} to kill the dragons
print(f'{player_A.name} will have to do: {player_A.deal_damage()}')
print(f'{player_B.name} will have to do: {player_B.deal_damage()}')
print(f'{player_C.name} will have to do: {player_C.deal_damage()}')