From b625cbb09b9be8724202008f4b3a0382fd2437a1 Mon Sep 17 00:00:00 2001 From: Radeon Date: Tue, 15 Oct 2024 12:00:45 +0300 Subject: [PATCH 1/4] Solution --- app/data.py | 86 ++++++++++++++++++++ app/knight.py | 30 +++++++ app/main.py | 213 +++----------------------------------------------- 3 files changed, 127 insertions(+), 202 deletions(-) create mode 100644 app/data.py create mode 100644 app/knight.py diff --git a/app/data.py b/app/data.py new file mode 100644 index 00000000..05031335 --- /dev/null +++ b/app/data.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/knight.py b/app/knight.py new file mode 100644 index 00000000..4eec1216 --- /dev/null +++ b/app/knight.py @@ -0,0 +1,30 @@ +from __future__ import annotations + + +class Knight: + def __init__(self, knight: dict) -> None: + self.knight = knight + self.name = knight["name"] + self.power = (knight["power"] + + (knight["weapon"].get("power", 0) + if knight["weapon"] else 0) + + (knight["potion"]["effect"].get("power", 0) + if knight["potion"] else 0)) + self.hp = (knight["hp"] + + (knight["potion"]["effect"].get("hp", 0) + if knight["potion"] else 0)) + self.protection = ((sum(protec["protection"] + for protec in knight["armour"]) + if knight["armour"] else 0) + + (knight["potion"]["effect"].get("protection", 0) + if knight["potion"] else 0)) + + def __sub__(self, other: Knight) -> Knight: + knight = Knight(self.knight) + knight.power = self.power - other.protection + return knight + + def __isub__(self, other: Knight) -> Knight: + self.hp -= other.power + self.hp = max(self.hp, 0) + return self diff --git a/app/main.py b/app/main.py index 445ffb14..b19de045 100644 --- a/app/main.py +++ b/app/main.py @@ -1,214 +1,23 @@ -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.data import KNIGHTS +from app.knight import Knight -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"] +def battle_between(knight1: Knight, knight2: Knight) -> None: + knight1 -= knight2 - knight1 + knight2 -= knight1 - knight2 - # 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"] +def battle(knights_config: dict) -> dict: + # BATTLE PREPARATIONS: + knights = {name: Knight(value) for name, value in knights_config.items()} # ------------------------------------------------------------------------------- # 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 - + battle_between(knights["lancelot"], knights["mordred"]) # 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 - + battle_between(knights["arthur"], knights["red_knight"]) # Return battle results: - return { - lancelot["name"]: lancelot["hp"], - arthur["name"]: arthur["hp"], - mordred["name"]: mordred["hp"], - red_knight["name"]: red_knight["hp"], - } + return {value.name: value.hp for value in knights.values()} print(battle(KNIGHTS)) From 96cbeb8d26a9c7b61267100818ec0fdc5a362788 Mon Sep 17 00:00:00 2001 From: Radeon Date: Tue, 15 Oct 2024 13:40:13 +0300 Subject: [PATCH 2/4] change __init__, __sub__, __isub__, battle --- app/knight.py | 33 ++++++++++++++++++--------------- app/main.py | 17 +++++++++++------ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/app/knight.py b/app/knight.py index 4eec1216..fa753fd0 100644 --- a/app/knight.py +++ b/app/knight.py @@ -4,27 +4,30 @@ class Knight: def __init__(self, knight: dict) -> None: self.knight = knight - self.name = knight["name"] - self.power = (knight["power"] - + (knight["weapon"].get("power", 0) - if knight["weapon"] else 0) - + (knight["potion"]["effect"].get("power", 0) - if knight["potion"] else 0)) - self.hp = (knight["hp"] - + (knight["potion"]["effect"].get("hp", 0) - if knight["potion"] else 0)) + + def preparation(self) -> None: + self.name = self.knight["name"] + self.power = (self.knight["power"] + + (self.knight["weapon"].get("power", 0) + if self.knight["weapon"] else 0) + + (self.knight["potion"]["effect"].get("power", 0) + if self.knight["potion"] else 0)) + self.hp = (self.knight["hp"] + + (self.knight["potion"]["effect"].get("hp", 0) + if self.knight["potion"] else 0)) self.protection = ((sum(protec["protection"] - for protec in knight["armour"]) - if knight["armour"] else 0) - + (knight["potion"]["effect"].get("protection", 0) - if knight["potion"] else 0)) + for protec in self.knight["armour"]) + if self.knight["armour"] else 0) + + (self.knight["potion"]["effect"].get("protection" + , 0) + if self.knight["potion"] else 0)) - def __sub__(self, other: Knight) -> Knight: + def reduce_power(self, other: Knight) -> Knight: knight = Knight(self.knight) knight.power = self.power - other.protection return knight - def __isub__(self, other: Knight) -> Knight: + def fight(self, other: Knight) -> Knight: self.hp -= other.power self.hp = max(self.hp, 0) return self diff --git a/app/main.py b/app/main.py index b19de045..cba7334d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,21 +1,26 @@ from app.data import KNIGHTS from app.knight import Knight +battle_table = [ + ("lancelot", "mordred"), + ("arthur", "red_knight") +] + def battle_between(knight1: Knight, knight2: Knight) -> None: - knight1 -= knight2 - knight1 - knight2 -= knight1 - knight2 + knight1.fight(knight2.reduce_power(knight1)) + knight2.fight(knight1.reduce_power(knight2)) def battle(knights_config: dict) -> dict: # BATTLE PREPARATIONS: knights = {name: Knight(value) for name, value in knights_config.items()} + for knight in knights.values(): + knight.preparation() # ------------------------------------------------------------------------------- # BATTLE: - # 1 Lancelot vs Mordred: - battle_between(knights["lancelot"], knights["mordred"]) - # 2 Arthur vs Red Knight: - battle_between(knights["arthur"], knights["red_knight"]) + for name in battle_table: + battle_between(knights[name[0]], knights[name[1]]) # Return battle results: return {value.name: value.hp for value in knights.values()} From 8b3ff22f5e632a3c229eeb37d39ec745fd034439 Mon Sep 17 00:00:00 2001 From: Radeon Date: Wed, 16 Oct 2024 18:46:31 +0300 Subject: [PATCH 3/4] change class knight --- app/knight.py | 50 +++++++++++++++++++++++++++++++++----------------- app/main.py | 4 ++-- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/app/knight.py b/app/knight.py index fa753fd0..9c45ed19 100644 --- a/app/knight.py +++ b/app/knight.py @@ -3,27 +3,43 @@ class Knight: def __init__(self, knight: dict) -> None: - self.knight = knight + self.knight_property = knight + self.name = knight["name"] + self.power = 0 + self.hp = 0 + self.protection = 0 + + def is_item(self, key: str) -> bool: + if self.knight_property[key] is not None: + return True + return False + + def set_power(self) -> int: + return (self.knight_property["power"] + + (self.knight_property["weapon"].get("power", 0) + if self.is_item("weapon") else 0) + + (self.knight_property["potion"]["effect"].get("power", 0) + if self.is_item("potion") else 0)) + + def set_hp(self) -> int: + return (self.knight_property["hp"] + + (self.knight_property["potion"]["effect"].get("hp", 0) + if self.is_item("potion") else 0)) + + def set_protection(self): + return ((sum(protec["protection"] + for protec in self.knight_property["armour"]) + if self.is_item("armour") else 0) + + (self.knight_property["potion"]["effect"].get("protection", 0) + if self.is_item("potion") else 0)) def preparation(self) -> None: - self.name = self.knight["name"] - self.power = (self.knight["power"] - + (self.knight["weapon"].get("power", 0) - if self.knight["weapon"] else 0) - + (self.knight["potion"]["effect"].get("power", 0) - if self.knight["potion"] else 0)) - self.hp = (self.knight["hp"] - + (self.knight["potion"]["effect"].get("hp", 0) - if self.knight["potion"] else 0)) - self.protection = ((sum(protec["protection"] - for protec in self.knight["armour"]) - if self.knight["armour"] else 0) - + (self.knight["potion"]["effect"].get("protection" - , 0) - if self.knight["potion"] else 0)) + self.power = self.set_power() + self.hp = self.set_hp() + self.protection = self.set_protection() def reduce_power(self, other: Knight) -> Knight: - knight = Knight(self.knight) + knight = Knight(self.knight_property) knight.power = self.power - other.protection return knight diff --git a/app/main.py b/app/main.py index cba7334d..56ff1ff6 100644 --- a/app/main.py +++ b/app/main.py @@ -19,8 +19,8 @@ def battle(knights_config: dict) -> dict: knight.preparation() # ------------------------------------------------------------------------------- # BATTLE: - for name in battle_table: - battle_between(knights[name[0]], knights[name[1]]) + for knight1, knight2 in battle_table: + battle_between(knights[knight1], knights[knight2]) # Return battle results: return {value.name: value.hp for value in knights.values()} From 9a3f3a7f08f75c19543c68a9cafc85fb7e9a874e Mon Sep 17 00:00:00 2001 From: Radeon Date: Wed, 16 Oct 2024 18:50:19 +0300 Subject: [PATCH 4/4] change class Knight --- app/knight.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/knight.py b/app/knight.py index 9c45ed19..569be5a2 100644 --- a/app/knight.py +++ b/app/knight.py @@ -26,11 +26,12 @@ def set_hp(self) -> int: + (self.knight_property["potion"]["effect"].get("hp", 0) if self.is_item("potion") else 0)) - def set_protection(self): + def set_protection(self) -> int: return ((sum(protec["protection"] for protec in self.knight_property["armour"]) if self.is_item("armour") else 0) - + (self.knight_property["potion"]["effect"].get("protection", 0) + + (self.knight_property["potion"]["effect"].get("protection", + 0) if self.is_item("potion") else 0)) def preparation(self) -> None: