Advertisement
fkudinov

15. Armies Battle / Вирішуємо задачі на Python CheckIO Українською

Nov 13th, 2023
66
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | Source Code | 1 0
  1. # -------------   tasks/wariors.py   ----------------------
  2.  
  3.  
  4. class Warrior:
  5.     health = 50
  6.     attack = 5
  7.     defense = 0
  8.  
  9.     @property
  10.     def is_alive(self) -> bool:
  11.         return self.health > 0
  12.  
  13.     def deal_dmg(self, unit: "Warrior"):
  14.         unit.health -= max(0, self.attack - unit.defense)
  15.  
  16.  
  17. class Knight(Warrior):
  18.     attack = 7
  19.  
  20.  
  21. class Defender(Warrior):
  22.     health = 60
  23.     attack = 3
  24.     defense = 2
  25.  
  26.  
  27. def fight(unit_1: Warrior, unit_2: Warrior) -> bool:
  28.  
  29.     while unit_1.is_alive:
  30.         unit_1.deal_dmg(unit_2)
  31.         if not unit_2.is_alive:
  32.             break
  33.         unit_2.deal_dmg(unit_1)
  34.  
  35.     return unit_1.is_alive
  36.  
  37.  
  38. # -----------------     tasks/army.py     -------------------------
  39.  
  40.  
  41. from collections import UserList
  42.  
  43. from tasks.wariors import fight
  44.  
  45.  
  46. class Army(UserList):
  47.  
  48.     def add_units(self, unit_type: type, count: int):
  49.         self.data.extend(unit_type() for _ in range(count))
  50.  
  51.  
  52. class Battle:
  53.  
  54.     def fight(self, army1: Army, army2: Army) -> bool:
  55.  
  56.         while army1 and army2:
  57.             unit1, unit2 = army1[0], army2[0]
  58.             if fight(unit1, unit2):
  59.                 army2.pop(0)
  60.             else:
  61.                 army1.pop(0)
  62.         return bool(army1)
  63.  
  64.  
  65.  
  66. # -----------     tests/test_wariors.py     ----------------------
  67.  
  68. from tasks.wariors import Warrior, Knight, fight, Defender
  69. from tasks.army import Army, Battle
  70.  
  71.  
  72. def test_all():
  73.  
  74.     chuck = Warrior()
  75.     bruce = Warrior()
  76.     carl = Knight()
  77.     dave = Warrior()
  78.     mark = Warrior()
  79.  
  80.     assert fight(chuck, bruce) == True
  81.     assert fight(dave, carl) == False
  82.     assert chuck.is_alive == True
  83.     assert bruce.is_alive == False
  84.     assert carl.is_alive == True
  85.     assert dave.is_alive == False
  86.     assert fight(carl, mark) == False
  87.     assert carl.is_alive == False
  88.  
  89.     chuck = Warrior()
  90.     bruce = Warrior()
  91.     carl = Knight()
  92.     dave = Warrior()
  93.     mark = Warrior()
  94.  
  95.     assert fight(chuck, bruce) == True
  96.     assert fight(dave, carl) == False
  97.     assert chuck.is_alive == True
  98.     assert bruce.is_alive == False
  99.     assert carl.is_alive == True
  100.     assert dave.is_alive == False
  101.     assert fight(carl, mark) == False
  102.     assert carl.is_alive == False
  103.  
  104.     # battle tests
  105.     my_army = Army()
  106.     my_army.add_units(Knight, 3)
  107.  
  108.     enemy_army = Army()
  109.     enemy_army.add_units(Warrior, 3)
  110.  
  111.     army_3 = Army()
  112.     army_3.add_units(Warrior, 20)
  113.     army_3.add_units(Knight, 5)
  114.  
  115.     army_4 = Army()
  116.     army_4.add_units(Warrior, 30)
  117.  
  118.     battle = Battle()
  119.  
  120.     assert battle.fight(my_army, enemy_army) == True
  121.     assert battle.fight(army_3, army_4) == False
  122.  
  123.     chuck = Warrior()
  124.     bruce = Warrior()
  125.     carl = Knight()
  126.     dave = Warrior()
  127.     mark = Warrior()
  128.     bob = Defender()
  129.     mike = Knight()
  130.     rog = Warrior()
  131.     lancelot = Defender()
  132.  
  133.     assert fight(chuck, bruce) == True
  134.     assert fight(dave, carl) == False
  135.     assert chuck.is_alive == True
  136.     assert bruce.is_alive == False
  137.     assert carl.is_alive == True
  138.     assert dave.is_alive == False
  139.     assert fight(carl, mark) == False
  140.     assert carl.is_alive == False
  141.     assert fight(bob, mike) == False
  142.     assert fight(lancelot, rog) == True
  143.  
  144.     my_army = Army()
  145.     my_army.add_units(Defender, 1)
  146.  
  147.     enemy_army = Army()
  148.     enemy_army.add_units(Warrior, 2)
  149.  
  150.     army_3 = Army()
  151.     army_3.add_units(Warrior, 1)
  152.     army_3.add_units(Defender, 1)
  153.  
  154.     army_4 = Army()
  155.     army_4.add_units(Warrior, 2)
  156.  
  157.     battle = Battle()
  158.  
  159.     assert battle.fight(my_army, enemy_army) == False
  160.     assert battle.fight(army_3, army_4) == True
  161.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement