Merge pull request 'knights_queue' (#12) from knights_queue into master

Reviewed-on: #12
This commit is contained in:
Dawid Korzępa 2022-04-04 21:58:14 +02:00
commit 0d6fdcbfea
5 changed files with 156 additions and 1 deletions

View File

View File

@ -0,0 +1,128 @@
import unittest
from logic.knights_queue import KnightsQueue
from models.knight import Knight
class KnightsQueueTest(unittest.TestCase):
def test_should_skip_dead_knights(self):
knight1 = Knight(None)
knight1.health = 0
knight2 = Knight(None)
knight2.health = 0
knight3 = Knight(None)
knight3.health = 1
knight4 = Knight(None)
knight4.health = 0
knight5 = Knight(None)
knight5.health = 0
knight6 = Knight(None)
knight6.health = 1
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6])
res1 = knights_queue.dequeue_knight()
res2 = knights_queue.dequeue_knight()
self.assertEqual(res1.health, 1)
self.assertEqual(res2.health, 1)
def test_should_return_first_alive_knight(self):
knight1 = Knight(None)
knight1.health = 222
knight2 = Knight(None)
knight2.health = -1
knight3 = Knight(None)
knight3.health = 1
knights_queue = KnightsQueue([knight1, knight2], [knight3])
res1 = knights_queue.dequeue_knight()
res2 = knights_queue.dequeue_knight()
res3 = knights_queue.dequeue_knight()
res4 = knights_queue.dequeue_knight()
self.assertEqual(res1, res3)
self.assertEqual(res2, res4)
def test_should_raise_when_knight_died_and_whole_team_dead(self):
with self.assertRaises(Exception):
knight1 = Knight(None)
knight1.health = 222
knight2 = Knight(None)
knight2.health = 1
knights_queue = KnightsQueue([knight1], [knight2])
knights_queue.dequeue_knight()
knights_queue.dequeue_knight()
knight2.health = -2
knights_queue.dequeue_knight()
knights_queue.dequeue_knight()
def test_should_make_valid_next_turn(self):
knight1 = Knight(None)
knight1.health = 222
knight2 = Knight(None)
knight2.health = 1
knights_queue = KnightsQueue([knight1], [knight2])
previous_turn = knights_queue.team_idx_turn
knights_queue.dequeue_knight()
current_turn = knights_queue.team_idx_turn
self.assertNotEqual(previous_turn, current_turn)
def test_should_raise_when_team_has_dead_knights(self):
with self.assertRaises(Exception):
knight1 = Knight(None)
knight1.health = 0
knight2 = Knight(None)
knight2.health = -1
knight3 = Knight(None)
knight3.health = -2
knight4 = Knight(None)
knight4.health = 20
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4])
knights_queue.dequeue_knight()
knights_queue.dequeue_knight()
def test_should_return_knight_from_any_team_and_add_to_queue_again(self):
knight1 = Knight(None)
knight1.health = 10
knight2 = Knight(None)
knight2.health = 20
knights_queue = KnightsQueue([knight1], [knight2])
result1 = knights_queue.dequeue_knight()
result2 = knights_queue.dequeue_knight()
result3 = knights_queue.dequeue_knight()
self.assertIsNotNone(result1)
self.assertIsNotNone(result2)
self.assertIsNotNone(result3)
self.assertTrue(result1.health == result3.health)
def test_should_raise_when_only_one_team_alive(self):
with self.assertRaises(Exception):
knight = Knight(None)
knight.health = 21
knights_queue = KnightsQueue([knight], [])
knights_queue.dequeue_knight()
def test_should_raise_when_no_team_alive(self):
with self.assertRaises(Exception):
knights_queue = KnightsQueue([], [])
knights_queue.dequeue_knight()
if __name__ == '__main__':
unittest.main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -5,6 +5,7 @@ import pygame
from common.colors import FONT_DARK, WHITE
from common.constants import *
from common.helpers import draw_text
from logic.knights_queue import KnightsQueue
from models.castle import Castle
from models.knight import Knight
from models.monster import Monster
@ -87,7 +88,6 @@ class Game:
stats = Stats()
logs = Logs()
knights_sprite_group = pygame.sprite.Group()
monsters_sprite_group = pygame.sprite.Group()
castle_sprite_group = pygame.sprite.Group()
@ -95,6 +95,8 @@ class Game:
knights_left = self.generate_knights_team(knights_sprite_group)
knights_right = self.generate_knights_team(knights_sprite_group)
knights_queue = KnightsQueue(knights_left, knights_right)
spawn_left_team = Spawner(grid, knights_left, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
pos_row=KNIGHTS_SPAWN_FIRST_ROW, pos_column=KNIGHTS_SPAWN_FIRST_COL)
spawn_right_team = Spawner(grid, knights_right, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,

25
logic/knights_queue.py Normal file
View File

@ -0,0 +1,25 @@
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:
return 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