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