From bef17bccd15ebae1916756f217bd233c367adf25 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 4 Apr 2022 21:07:40 +0200 Subject: [PATCH 1/4] feat: round queue --- logic/__tests__/__init__.py | 0 logic/__tests__/knights_queue_test.py | 54 ++++++++++++++++++ logic/__tests__/resources/textures/knight.png | Bin 0 -> 1719 bytes logic/knights_queue.py | 25 ++++++++ 4 files changed, 79 insertions(+) create mode 100644 logic/__tests__/__init__.py create mode 100644 logic/__tests__/knights_queue_test.py create mode 100644 logic/__tests__/resources/textures/knight.png create mode 100644 logic/knights_queue.py diff --git a/logic/__tests__/__init__.py b/logic/__tests__/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/logic/__tests__/knights_queue_test.py b/logic/__tests__/knights_queue_test.py new file mode 100644 index 0000000..0772a2c --- /dev/null +++ b/logic/__tests__/knights_queue_test.py @@ -0,0 +1,54 @@ +import unittest + +from logic.knights_queue import KnightsQueue +from models.knight import Knight + + +class KnightsQueueTest(unittest.TestCase): + 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() diff --git a/logic/__tests__/resources/textures/knight.png b/logic/__tests__/resources/textures/knight.png new file mode 100644 index 0000000000000000000000000000000000000000..0024f644831608e2f833ded2a471333abf485b26 GIT binary patch literal 1719 zcmV;o21xmdP)xOMxgqhDNCFupNPLRkGQ2@9)XCgc?58w%A<0Uj(yLkTaM1ah? z0b^-d=ijnnMAwZ)@>l1q&uqMendt<=;VA$>I6Q@!=>%ruB|e74@>(t`p+x|h{LFO1 zM*QPJ$@Wb`%uFYSE`o9aoXCs#cu>O8o@Me!dj=PQAd8?IS}fo+^5pa~>xQcc1l?Ak zRDfhE&HR!VBY=|#!?FS`7T|~cY`o+tg18iPT7fbF#Mu|&Mjn7~5p+UXvOxmeO6&rH zhhKU8ijR5y;wj&bChp@?`EN9vhX8;FzdqsTvTLgdPt5bE@o#A!nRNr(n;XiLBQ1&0 zPblh`2L&h zOYNjZC^0G7Ny#&g9#PTkowl7ITa)T#aONf8l)sSGsA!Ta$Fo9-51$>_rX;R^(Gp}Kf?{-Z6pcpBk-W+!pfnGFux6do;UEx;CL*sR4^VnTohhph@=)aj z-8w%jk6+nRUjT?iqJ7sl8a2MXUrr#iZXmn1$}PZx^&6^%Q04=~Iv_kT&xwldEfPZ% zq0y*u(gZPiYPPJ@TJ!_w#s|pcug4Y~#|7!}iAj`*O0}p>gn^MvrCGTUWgjX9=7nS` z?K$Q{rNEM@wC6o&T)7Zs!|E5FiqnXRgS>zI1I8b}0{0w`GmUdXPkx-JbFm1@ye z33@@*5>Va`xVuorPBDhN3so3P%P{jf-2XA@>mGmpz76zzhMCXt`|K2B+WG;?K&DXg@uT>{guh*+AnM&LC2>?`ld_!qN$eX`@@zho>i)k+;@^0lW zkvu*=aDXMOwS1hQ);RvrVuNr*G^Iccnw|KHf$;74ZTr9P6MKeh19wF%oX zr*VFvr6(vu>=U>ZLu3WegbhW&B^x9?TS3Hs)xq$lW> zzZ*--Slrq~1}Q%K*|pWaaVN=-0bs*+j?7(#cw81E2!`^O*y7eE-#&79-nY1Qz_+&@ zuK)b0<+?8~hoI0V06DwgeopaYi(8xc9Mi2 zY!kx?W=0+2JiREwS)n}& zICX)<@FzOZMx%zw$r#%0V_d#GhMi)JM*^oXC!V7$FlZFe3b*>61amr11x=@G6;$Oy zl-281{&Ctb7ox1wWdjvMS|5%Aa5QT#lna)nbUR=8thyyRbX=+d0?od50fN5ZKxREU zI_@cgOg?B8NYE~T-);~g5{V+2N&_Auc#sdQ!dH7;-UmXkL|_2WZXaXh(ir~vE2e}v zLAwRWs8oxd`wk!>07bJdVyFU8Z(qdGp5a>rBCU!%hA9A%m!0C(IddQpRKpyC-03sm17f5h&f?}wQM51{8;wc{VGCJMQ0+b3s z#OXyxx%<;-)Ot?v#1S6t8E8z{YXuUa+1ka5W+9ZmDhuIJm{aSEN@PFBBatYs#}>F1 zotp=uZt(L2xLkJ#RAy&Aa4@e7q^74#2=2E6717r1~r{112FU2*D6g}(p* N002ovPDHLkV1oAVIuif@ literal 0 HcmV?d00001 diff --git a/logic/knights_queue.py b/logic/knights_queue.py new file mode 100644 index 0000000..42960eb --- /dev/null +++ b/logic/knights_queue.py @@ -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: + self.dequeue_knight() + else: + self.queues[self.team_idx_turn].append(knight) + self.next_round() + 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_round(self): + self.team_idx_turn = (self.team_idx_turn + 1) % 2 From f06ea9e5b0dd4b71cc73f9369a588e886065cadd Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 4 Apr 2022 21:23:32 +0200 Subject: [PATCH 2/4] tests: added test case --- logic/__tests__/knights_queue_test.py | 13 +++++++++++++ logic/game.py | 4 +++- logic/knights_queue.py | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/logic/__tests__/knights_queue_test.py b/logic/__tests__/knights_queue_test.py index 0772a2c..782be15 100644 --- a/logic/__tests__/knights_queue_test.py +++ b/logic/__tests__/knights_queue_test.py @@ -5,6 +5,19 @@ from models.knight import Knight class KnightsQueueTest(unittest.TestCase): + 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) diff --git a/logic/game.py b/logic/game.py index bee0c6c..3545d8b 100644 --- a/logic/game.py +++ b/logic/game.py @@ -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, diff --git a/logic/knights_queue.py b/logic/knights_queue.py index 42960eb..cb3ba2c 100644 --- a/logic/knights_queue.py +++ b/logic/knights_queue.py @@ -14,12 +14,12 @@ class KnightsQueue: self.dequeue_knight() else: self.queues[self.team_idx_turn].append(knight) - self.next_round() + 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_round(self): + def next_turn(self): self.team_idx_turn = (self.team_idx_turn + 1) % 2 From 4caba91c7e14e3e47f40cc38e50edb37f60d4415 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 4 Apr 2022 21:27:47 +0200 Subject: [PATCH 3/4] tests: added edge case --- logic/__tests__/knights_queue_test.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/logic/__tests__/knights_queue_test.py b/logic/__tests__/knights_queue_test.py index 782be15..99ba7da 100644 --- a/logic/__tests__/knights_queue_test.py +++ b/logic/__tests__/knights_queue_test.py @@ -5,6 +5,20 @@ from models.knight import Knight class KnightsQueueTest(unittest.TestCase): + + 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() + def test_should_make_valid_next_turn(self): knight1 = Knight(None) knight1.health = 222 From 621a36461c5593b4996b6be108d8978fe5d945bc Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 4 Apr 2022 21:51:32 +0200 Subject: [PATCH 4/4] fixed bug :~DDD --- logic/__tests__/knights_queue_test.py | 47 +++++++++++++++++++++++++++ logic/knights_queue.py | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/logic/__tests__/knights_queue_test.py b/logic/__tests__/knights_queue_test.py index 99ba7da..ecf9548 100644 --- a/logic/__tests__/knights_queue_test.py +++ b/logic/__tests__/knights_queue_test.py @@ -5,6 +5,52 @@ 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): @@ -18,6 +64,7 @@ class KnightsQueueTest(unittest.TestCase): 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) diff --git a/logic/knights_queue.py b/logic/knights_queue.py index cb3ba2c..4b63e1f 100644 --- a/logic/knights_queue.py +++ b/logic/knights_queue.py @@ -11,7 +11,7 @@ class KnightsQueue: if self.both_teams_alive(): knight = self.queues[self.team_idx_turn].popleft() if knight.health <= 0: - self.dequeue_knight() + return self.dequeue_knight() else: self.queues[self.team_idx_turn].append(knight) self.next_turn()