Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #776

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added app/battle/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions app/battle/battle_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from app.knight_info.knights import Knight


class Battle:
@staticmethod
def battle(first_knight: Knight, second_knight: Knight) -> None:
first_knight.hp -= second_knight.power - first_knight.protection
second_knight.hp -= first_knight.power - second_knight.protection

first_knight.hp = max(first_knight.hp, 0)
second_knight.hp = max(second_knight.hp, 0)
Empty file added app/knight_info/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions app/knight_info/armour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Armour:
def __init__(self, part: str, protection: int) -> None:
self.part = part
self.protection = protection


def total_armour(armours: list[Armour]) -> int:
return sum(armour.protection for armour in armours)
86 changes: 86 additions & 0 deletions app/knight_info/config.py
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
}
45 changes: 45 additions & 0 deletions app/knight_info/knights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from app.knight_info.armour import Armour, total_armour
from app.knight_info.potion import Potion
from app.knight_info.weapon import Weapon


class Knight:
def __init__(
self,
name: str,
hp: int,
power: int,
weapon: dict,
armour: list,
potion: dict
) -> None:
self.name = name
self.hp = hp
self.power = power
self.protection = 0
self.weapon = Weapon(**weapon)
self.armour = [Armour(**part_armour) for part_armour in armour]

if potion is not None:
self.potion = Potion(**potion)
self.hp += self.potion.hp
self.power += self.potion.power
self.protection += self.potion.protection

self.power += self.weapon.power
self.protection += total_armour(self.armour)

def knight_info(self) -> None:
print(self.name, self.hp, self.power, self.protection)


def get_knight(knights_config: dict) -> list:
knights = []

for knight_key, knight_value in knights_config.items():
current_knight = Knight(**knight_value)

current_knight.knight_info()
knights.append(current_knight)

return knights
8 changes: 8 additions & 0 deletions app/knight_info/potion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Potion:
def __init__(self, name: str, effect: dict) -> None:
self.name = name
self.effect = effect

self.hp = self.effect.get("hp", 0)
self.power = self.effect.get("power", 0)
self.protection = self.effect.get("protection", 0)
4 changes: 4 additions & 0 deletions app/knight_info/weapon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Weapon:
def __init__(self, name: str, power: int) -> None:
self.name = name
self.power = power
221 changes: 12 additions & 209 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,214 +1,17 @@
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,
}
}
}
}


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"]
from app.battle.battle_file import Battle
from app.knight_info.knights import get_knight

# 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"]
def battle(knights_config: dict) -> dict:
knights = get_knight(knights_config)
first_battle = Battle()
first_battle.battle(knights[0], knights[2])
second_battle = Battle()
second_battle.battle(knights[1], knights[3])

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"],
knights[0].name: knights[0].hp,
knights[2].name: knights[2].hp,
knights[1].name: knights[1].hp,
knights[3].name: knights[3].hp,
}


print(battle(KNIGHTS))
Loading