From 71b836424fff992ab18ae806f1040f0d9a840303 Mon Sep 17 00:00:00 2001 From: Anatoliy Date: Tue, 21 Jan 2025 22:58:08 +0200 Subject: [PATCH] Solution --- app/battle.py | 22 +++++ app/knights.py | 86 ++++++++++++++++ app/main.py | 217 +---------------------------------------- app/models/__init__.py | 0 app/models/armour.py | 4 + app/models/knight.py | 29 ++++++ app/models/potion.py | 4 + app/models/weapon.py | 4 + 8 files changed, 154 insertions(+), 212 deletions(-) create mode 100644 app/battle.py create mode 100644 app/knights.py create mode 100644 app/models/__init__.py create mode 100644 app/models/armour.py create mode 100644 app/models/knight.py create mode 100644 app/models/potion.py create mode 100644 app/models/weapon.py diff --git a/app/battle.py b/app/battle.py new file mode 100644 index 00000000..dd91d1af --- /dev/null +++ b/app/battle.py @@ -0,0 +1,22 @@ +from app.models.knight import Knight + + +@staticmethod +def battle(knights: str) -> dict: + lancelot = Knight(**knights["lancelot"]) + arthur = Knight(**knights["arthur"]) + mordred = Knight(**knights["mordred"]) + red_knight = Knight(**knights["red_knight"]) + + lancelot.take_damage(mordred.power) + mordred.take_damage(lancelot.power) + + arthur.take_damage(red_knight.power) + red_knight.take_damage(arthur.power) + + return { + lancelot.name: lancelot.hp, + arthur.name: arthur.hp, + mordred.name: mordred.hp, + red_knight.name: red_knight.hp + } diff --git a/app/knights.py b/app/knights.py new file mode 100644 index 00000000..623c6538 --- /dev/null +++ b/app/knights.py @@ -0,0 +1,86 @@ +KNIGHTS = { + "lancelot": { + "name": "Lancelot", + "power": 35, + "hp": 100, + "armour": [], + "weapon": { + "name": "Metal Sword", + "power": 50, + }, + "potion": None, + }, + "arthur": { + "name": "Arthur", + "power": 45, + "hp": 75, + "armour": [ + { + "part": "helmet", + "protection": 15, + }, + { + "part": "breastplate", + "protection": 20, + }, + { + "part": "boots", + "protection": 10, + } + ], + "weapon": { + "name": "Two-handed Sword", + "power": 55, + }, + "potion": None, + }, + "mordred": { + "name": "Mordred", + "power": 30, + "hp": 90, + "armour": [ + { + "part": "breastplate", + "protection": 15, + }, + { + "part": "boots", + "protection": 10, + } + ], + "weapon": { + "name": "Poisoned Sword", + "power": 60, + }, + "potion": { + "name": "Berserk", + "effect": { + "power": +15, + "hp": -5, + "protection": +10, + } + } + }, + "red_knight": { + "name": "Red Knight", + "power": 40, + "hp": 70, + "armour": [ + { + "part": "breastplate", + "protection": 25, + } + ], + "weapon": { + "name": "Sword", + "power": 45 + }, + "potion": { + "name": "Blessing", + "effect": { + "hp": +10, + "power": +5, + } + } + } +} diff --git a/app/main.py b/app/main.py index 445ffb14..404779fb 100644 --- a/app/main.py +++ b/app/main.py @@ -1,214 +1,7 @@ -KNIGHTS = { - "lancelot": { - "name": "Lancelot", - "power": 35, - "hp": 100, - "armour": [], - "weapon": { - "name": "Metal Sword", - "power": 50, - }, - "potion": None, - }, - "arthur": { - "name": "Arthur", - "power": 45, - "hp": 75, - "armour": [ - { - "part": "helmet", - "protection": 15, - }, - { - "part": "breastplate", - "protection": 20, - }, - { - "part": "boots", - "protection": 10, - } - ], - "weapon": { - "name": "Two-handed Sword", - "power": 55, - }, - "potion": None, - }, - "mordred": { - "name": "Mordred", - "power": 30, - "hp": 90, - "armour": [ - { - "part": "breastplate", - "protection": 15, - }, - { - "part": "boots", - "protection": 10, - } - ], - "weapon": { - "name": "Poisoned Sword", - "power": 60, - }, - "potion": { - "name": "Berserk", - "effect": { - "power": +15, - "hp": -5, - "protection": +10, - } - } - }, - "red_knight": { - "name": "Red Knight", - "power": 40, - "hp": 70, - "armour": [ - { - "part": "breastplate", - "protection": 25, - } - ], - "weapon": { - "name": "Sword", - "power": 45 - }, - "potion": { - "name": "Blessing", - "effect": { - "hp": +10, - "power": +5, - } - } - } -} +from app.battle import battle +from app.knights import KNIGHTS -def battle(knightsConfig): - # BATTLE PREPARATIONS: - - # lancelot - lancelot = knightsConfig["lancelot"] - - # apply armour - lancelot["protection"] = 0 - for a in lancelot["armour"]: - lancelot["protection"] += a["protection"] - - # apply weapon - lancelot["power"] += lancelot["weapon"]["power"] - - # apply potion if exist - if lancelot["potion"] is not None: - if "power" in lancelot["potion"]["effect"]: - lancelot["power"] += lancelot["potion"]["effect"]["power"] - - if "protection" in lancelot["potion"]["effect"]: - lancelot["protection"] += lancelot["potion"]["effect"]["protection"] - - if "hp" in lancelot["potion"]["effect"]: - lancelot["hp"] += lancelot["potion"]["effect"]["hp"] - - # arthur - arthur = knightsConfig["arthur"] - - # apply armour - arthur["protection"] = 0 - for a in arthur["armour"]: - arthur["protection"] += a["protection"] - - # apply weapon - arthur["power"] += arthur["weapon"]["power"] - - # apply potion if exist - if arthur["potion"] is not None: - if "power" in arthur["potion"]["effect"]: - arthur["power"] += arthur["potion"]["effect"]["power"] - - if "protection" in arthur["potion"]["effect"]: - arthur["protection"] += arthur["potion"]["effect"]["protection"] - - if "hp" in arthur["potion"]["effect"]: - arthur["hp"] += arthur["potion"]["effect"]["hp"] - - # mordred - mordred = knightsConfig["mordred"] - - # apply armour - mordred["protection"] = 0 - for a in mordred["armour"]: - mordred["protection"] += a["protection"] - - # apply weapon - mordred["power"] += mordred["weapon"]["power"] - - # apply potion if exist - if mordred["potion"] is not None: - if "power" in mordred["potion"]["effect"]: - mordred["power"] += mordred["potion"]["effect"]["power"] - - if "protection" in mordred["potion"]["effect"]: - mordred["protection"] += mordred["potion"]["effect"]["protection"] - - if "hp" in mordred["potion"]["effect"]: - mordred["hp"] += mordred["potion"]["effect"]["hp"] - - # red_knight - red_knight = knightsConfig["red_knight"] - - # apply armour - red_knight["protection"] = 0 - for a in red_knight["armour"]: - red_knight["protection"] += a["protection"] - - # apply weapon - red_knight["power"] += red_knight["weapon"]["power"] - - # apply potion if exist - if red_knight["potion"] is not None: - if "power" in red_knight["potion"]["effect"]: - red_knight["power"] += red_knight["potion"]["effect"]["power"] - - if "protection" in red_knight["potion"]["effect"]: - red_knight["protection"] += red_knight["potion"]["effect"]["protection"] - - if "hp" in red_knight["potion"]["effect"]: - red_knight["hp"] += red_knight["potion"]["effect"]["hp"] - - # ------------------------------------------------------------------------------- - # BATTLE: - - # 1 Lancelot vs Mordred: - lancelot["hp"] -= mordred["power"] - lancelot["protection"] - mordred["hp"] -= lancelot["power"] - mordred["protection"] - - # check if someone fell in battle - if lancelot["hp"] <= 0: - lancelot["hp"] = 0 - - if mordred["hp"] <= 0: - mordred["hp"] = 0 - - # 2 Arthur vs Red Knight: - arthur["hp"] -= red_knight["power"] - arthur["protection"] - red_knight["hp"] -= arthur["power"] - red_knight["protection"] - - # check if someone fell in battle - if arthur["hp"] <= 0: - arthur["hp"] = 0 - - if red_knight["hp"] <= 0: - red_knight["hp"] = 0 - - # Return battle results: - return { - lancelot["name"]: lancelot["hp"], - arthur["name"]: arthur["hp"], - mordred["name"]: mordred["hp"], - red_knight["name"]: red_knight["hp"], - } - - -print(battle(KNIGHTS)) +if __name__ == "__main__": + result = battle(KNIGHTS) + print(result) diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/models/armour.py b/app/models/armour.py new file mode 100644 index 00000000..96a380ee --- /dev/null +++ b/app/models/armour.py @@ -0,0 +1,4 @@ +class Armour: + def __init__(self, part: str, protection: int) -> None: + self.part = part + self.protection = protection diff --git a/app/models/knight.py b/app/models/knight.py new file mode 100644 index 00000000..5f2ea8e9 --- /dev/null +++ b/app/models/knight.py @@ -0,0 +1,29 @@ +from app.models.armour import Armour +from app.models.weapon import Weapon +from app.models.potion import Potion + + +class Knight: + + def __init__(self, name: str, power: int, hp: int, + armour: list, weapon: dict, potion: dict) -> None: + self.name = name + self.base_power = power + self.hp = hp + self.armour = [Armour(**a) for a in armour] + self.weapon = Weapon(**weapon) + self.potion = Potion(**potion) if potion else None + self.apply_equipment() + + def apply_equipment(self) -> None: + self.protection = sum([a.protection for a in self.armour]) + self.power = self.base_power + self.weapon.power + if self.potion: + self.hp += self.potion.effect.get("hp", 0) + self.power += self.potion.effect.get("power", 0) + self.protection += self.potion.effect.get("protection", 0) + + def take_damage(self, damage: int) -> None: + self.hp -= max(0, damage - self.protection) + if self.hp < 0: + self.hp = 0 diff --git a/app/models/potion.py b/app/models/potion.py new file mode 100644 index 00000000..c8a7313b --- /dev/null +++ b/app/models/potion.py @@ -0,0 +1,4 @@ +class Potion: + def __init__(self, name: str, effect: dict) -> None: + self.name = name + self.effect = effect diff --git a/app/models/weapon.py b/app/models/weapon.py new file mode 100644 index 00000000..66bd4e76 --- /dev/null +++ b/app/models/weapon.py @@ -0,0 +1,4 @@ +class Weapon: + def __init__(self, name: str, power: int) -> None: + self.name = name + self.power = power