Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 5bcfa9b

Browse files
committed
Updated WRW game enums
Added a dedicated function to get fight results.
1 parent 07f07ae commit 5bcfa9b

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

src/wrw_game/enums.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,49 @@ def __str__(self) -> str:
3131

3232
return self.name
3333

34-
def __sub__(self, other) -> FightResult:
35-
"""Return a fight result"""
36-
37-
if not isinstance(other, FightChoice):
38-
raise TypeError(
39-
f"unsupported operand type(s) for -: "
40-
f"{self.__class__.__name__} and {other.__class__.__name__}"
41-
)
42-
43-
if self == other:
44-
return FightResult.DRAW
45-
46-
success_attacks = (
47-
FightChoice.WARRIOR.value - FightChoice.ROBBER.value,
48-
FightChoice.ROBBER.value - FightChoice.WIZARD.value,
49-
FightChoice.WIZARD.value - FightChoice.WARRIOR.value,
34+
35+
def get_fight_result(attack: FightChoice, defence: FightChoice) -> FightResult:
36+
"""Return a fight result based on attack and defence choices
37+
38+
This function performs argument types validation first and raises
39+
TypeError in case of failure. After that the attack choice is compared
40+
with the defence choice. The comparison rules are:
41+
42+
- if attack and defence are the same - draw result is returned
43+
- if attack and defence pair is in successful attacks preset, the attack
44+
is considered to be successful
45+
- otherwise attack is considered to be failed
46+
47+
:param attack: attack choice
48+
:type attack: :class: `FightChoice`
49+
:param defence: defence choice
50+
:type defence: :class: `FightChoice`
51+
52+
:return:
53+
:rtype: :class: `FightResult`
54+
55+
:raise: TypeError
56+
57+
"""
58+
59+
# perform type validation
60+
if not isinstance(attack, FightChoice) or \
61+
not isinstance(defence, FightChoice):
62+
raise TypeError(
63+
f"unsupported argument type(s): "
64+
f"'{attack.__class__.__name__}' and '{defence.__class__.__name__}'"
5065
)
51-
if self.value - other.value in success_attacks:
52-
return FightResult.SUCCESS
5366

54-
return FightResult.FAILURE
67+
# calculate result
68+
if attack == defence:
69+
return FightResult.DRAW
70+
71+
successful_attacks = (
72+
(FightChoice.WARRIOR, FightChoice.ROBBER),
73+
(FightChoice.ROBBER, FightChoice.WIZARD),
74+
(FightChoice.WIZARD, FightChoice.WARRIOR),
75+
)
76+
if (attack, defence) in successful_attacks:
77+
return FightResult.SUCCESS
78+
79+
return FightResult.FAILURE

src/wrw_game/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from abc import ABC, abstractmethod
99

1010
from wrw_game import settings
11-
from wrw_game.enums import FightChoice, FightResult
11+
from wrw_game.enums import FightChoice, FightResult, get_fight_result
1212
from wrw_game.exceptions import EnemyDown, GameOver
1313
from wrw_game.loggers import stream_handler
1414

@@ -148,9 +148,9 @@ def select_defence(self) -> FightChoice: # pragma: no cover
148148

149149
@staticmethod
150150
def fight(attack: FightChoice, defence: FightChoice) -> FightResult:
151-
"""Return a fight result"""
151+
"""Implements a fight result calculation interface"""
152152

153-
return attack - defence
153+
return get_fight_result(attack, defence)
154154

155155
def add_score_points(self, score_points: int) -> None:
156156
"""Add score points"""

tests/wrw_game/unit/test_fight.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from wrw_game.enums import FightChoice, FightResult
3+
from wrw_game.enums import FightChoice, FightResult, get_fight_result
44

55

66
class TestFightChoice(unittest.TestCase):
@@ -32,45 +32,46 @@ def test_draw_str(self):
3232

3333
class TestFights(unittest.TestCase):
3434
def test_draw_warrior(self):
35-
fight_result = FightChoice.WARRIOR - FightChoice.WARRIOR
35+
fight_result = get_fight_result(FightChoice.WARRIOR,
36+
FightChoice.WARRIOR)
3637
self.assertEqual(FightResult.DRAW, fight_result)
3738

3839
def test_draw_robber(self):
39-
fight_result = FightChoice.ROBBER - FightChoice.ROBBER
40+
fight_result = get_fight_result(FightChoice.ROBBER, FightChoice.ROBBER)
4041
self.assertEqual(FightResult.DRAW, fight_result)
4142

4243
def test_draw_wizard(self):
43-
fight_result = FightChoice.WIZARD - FightChoice.WIZARD
44+
fight_result = get_fight_result(FightChoice.WIZARD, FightChoice.WIZARD)
4445
self.assertEqual(FightResult.DRAW, fight_result)
4546

4647
def test_success_warrior(self):
47-
fight_result = FightChoice.WARRIOR - FightChoice.ROBBER
48+
fight_result = get_fight_result(FightChoice.WARRIOR, FightChoice.ROBBER)
4849
self.assertEqual(FightResult.SUCCESS, fight_result)
4950

5051
def test_success_robber(self):
51-
fight_result = FightChoice.ROBBER - FightChoice.WIZARD
52+
fight_result = get_fight_result(FightChoice.ROBBER, FightChoice.WIZARD)
5253
self.assertEqual(FightResult.SUCCESS, fight_result)
5354

5455
def test_success_wizard(self):
55-
fight_result = FightChoice.WIZARD - FightChoice.WARRIOR
56+
fight_result = get_fight_result(FightChoice.WIZARD, FightChoice.WARRIOR)
5657
self.assertEqual(FightResult.SUCCESS, fight_result)
5758

5859
def test_failure_warrior(self):
59-
fight_result = FightChoice.WARRIOR - FightChoice.WIZARD
60+
fight_result = get_fight_result(FightChoice.WARRIOR, FightChoice.WIZARD)
6061
self.assertEqual(FightResult.FAILURE, fight_result)
6162

6263
def test_failure_robber(self):
63-
fight_result = FightChoice.ROBBER - FightChoice.WARRIOR
64+
fight_result = get_fight_result(FightChoice.ROBBER, FightChoice.WARRIOR)
6465
self.assertEqual(FightResult.FAILURE, fight_result)
6566

6667
def test_failure_wizard(self):
67-
fight_result = FightChoice.WIZARD - FightChoice.ROBBER
68+
fight_result = get_fight_result(FightChoice.WIZARD, FightChoice.ROBBER)
6869
self.assertEqual(FightResult.FAILURE, fight_result)
6970

7071
def test_exception(self):
71-
self.assertRaises(TypeError, FightChoice.WARRIOR.__sub__, 1)
72-
self.assertRaises(TypeError, FightChoice.WARRIOR.__sub__, "1")
73-
self.assertRaises(TypeError, FightChoice.WARRIOR.__sub__, None)
72+
self.assertRaises(TypeError, get_fight_result, None, None)
73+
self.assertRaises(TypeError, get_fight_result, FightChoice.WARRIOR, 1)
74+
self.assertRaises(TypeError, get_fight_result, 1, FightChoice.WARRIOR)
7475

7576

7677
if __name__ == "__main__":

0 commit comments

Comments
 (0)