WMICraft/logic/knights_queue.py
2022-04-04 21:23:32 +02:00

26 lines
795 B
Python

import random
from collections import deque
class KnightsQueue:
def __init__(self, blue_team, red_team):
self.queues = deque(blue_team), deque(red_team)
self.team_idx_turn = random.randint(0, 1)
def dequeue_knight(self):
if self.both_teams_alive():
knight = self.queues[self.team_idx_turn].popleft()
if knight.health <= 0:
self.dequeue_knight()
else:
self.queues[self.team_idx_turn].append(knight)
self.next_turn()
return knight
raise Exception('Game has just ended')
def both_teams_alive(self):
return len(self.queues[0]) > 0 and len(self.queues[1]) > 0
def next_turn(self):
self.team_idx_turn = (self.team_idx_turn + 1) % 2