f | import asyncio | f | import asyncio |
| | | |
| class Monster: | | class Monster: |
| | | |
n | def __init__(self, name, position, delay, power): | n | def __init__(self, name, position, interval, strength): |
| self.name = name | | self.name = name |
| self.position = position | | self.position = position |
n | self.delay = delay | n | self.interval = interval |
| self.power = power | | self.strength = strength |
| self.alive = True | | self.is_alive = True |
| | | |
n | async def run(self, start_barrier, end_barrier): | n | async def run(self, start_sync, end_sync): |
| counter = 0 | | step_counter = 0 |
| while True: | | while True: |
n | await start_barrier.wait() | n | await start_sync.wait() |
| counter += 1 | | step_counter += 1 |
| if counter == self.delay: | | if step_counter == self.interval: |
| self.position += 1 | | self.position += 1 |
n | counter = 0 | n | step_counter = 0 |
| await end_barrier.wait() | | await end_sync.wait() |
| | | |
n | async def game(monsters, start_barrier, end_barrier, epochs): | n | async def game(monsters, start_sync, end_sync, rounds): |
| for epoch in range(epochs): | | for _ in range(rounds): |
| await start_barrier.wait() | | await start_sync.wait() |
| await end_barrier.wait() | | await end_sync.wait() |
| alive_monsters = [m for m in monsters if m.alive] | | active_monsters = [m for m in monsters if m.is_alive] |
| if not alive_monsters: | | if not active_monsters: |
| return 'All dead' | | return 'All dead' |
n | fighters = None | n | collision = None |
| for idx, fighter1 in enumerate(alive_monsters[:-1]): | | for i, monster1 in enumerate(active_monsters[:-1]): |
| if fighters: | | if collision: |
| break | | break |
n | | n | for monster2 in active_monsters[i + 1:]: |
| | | if monster1.position == monster2.position: |
| | | collision = [monster1, monster2] |
| | | break |
| | | if collision: |
| | | monster1, monster2 = collision |
| | | if monster1.strength == monster2.strength: |
| | | monster1.is_alive = False |
| | | monster2.is_alive = False |
| | | elif monster1.strength > monster2.strength: |
| | | monster1.strength -= monster2.strength |
| | | monster2.is_alive = False |
| else: | | else: |
n | for fighter2 in alive_monsters[idx + 1:]: | n | monster2.strength -= monster1.strength |
| if fighter1.position == fighter2.position: | | |
| fighters = [fighter1, fighter2] | | |
| break | | |
| if fighters: | | |
| fighter1, fighter2 = fighters | | |
| if fighter1.power == fighter2.power: | | |
| fighter1.alive = False | | monster1.is_alive = False |
| fighter2.alive = False | | |
| elif fighter1.power > fighter2.power: | | |
| fighter1.power -= fighter2.power | | |
| fighter2.alive = False | | |
| else: | | |
| fighter2.power -= fighter1.power | | |
| fighter1.alive = False | | |
| if len(alive_monsters) < 2: | | if len(active_monsters) < 2: |
| break | | break |
n | alive_monsters = [m for m in monsters if m.alive] | n | survivors = [m for m in monsters if m.is_alive] |
| if not alive_monsters: | | if not survivors: |
| return 'All dead' | | return 'All dead' |
t | elif alive_monsters != monsters: | t | elif survivors != monsters: |
| return ', '.join([m.name for m in alive_monsters]) | | return ', '.join([m.name for m in survivors]) |
| else: | | else: |
| return 'All flee' | | return 'All flee' |