WMICraft/logic/knights_queue.py

26 lines
802 B
Python
Raw Permalink Normal View History

2022-04-04 21:07:40 +02:00
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:
2022-04-04 21:51:32 +02:00
return self.dequeue_knight()
2022-04-04 21:07:40 +02:00
else:
self.queues[self.team_idx_turn].append(knight)
2022-04-04 21:23:32 +02:00
self.next_turn()
2022-04-04 21:07:40 +02:00
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
2022-04-04 21:23:32 +02:00
def next_turn(self):
2022-04-04 21:07:40 +02:00
self.team_idx_turn = (self.team_idx_turn + 1) % 2