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? #778

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE
exclude = venv, tests
exclude = .venv, tests
83 changes: 83 additions & 0 deletions app/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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,
},
},
},
}
37 changes: 37 additions & 0 deletions app/knights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations


class Knight:
def __init__(self, params: dict) -> None:
self.name = params["name"]
self.power = params["power"]
self.hp = params["hp"]
self.armour = params["armour"]
self.weapon = params["weapon"]
self.potion = params["potion"]
self.protection = 0

def apply_armor(self) -> None:
self.protection = sum(armor["protection"] for armor in self.armour)

def apply_weapon(self) -> None:
self.power += self.weapon["power"]

def apply_potion(self) -> None:
if "power" in self.potion["effect"]:
self.power += self.potion["effect"]["power"]

if "protection" in self.potion["effect"]:
self.protection += self.potion["effect"]["protection"]

if "hp" in self.potion["effect"]:
self.hp += self.potion["effect"]["hp"]

def damage(self, other: Knight) -> None:
self.hp -= other.power - self.protection
other.hp -= self.power - other.protection

if self.hp < 0:
self.hp = 0
elif other.hp < 0:
other.hp = 0
222 changes: 20 additions & 202 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,214 +1,32 @@
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"]
from __future__ import annotations

if "hp" in arthur["potion"]["effect"]:
arthur["hp"] += arthur["potion"]["effect"]["hp"]
from app.knights import Knight

# mordred
mordred = knightsConfig["mordred"]

# apply armour
mordred["protection"] = 0
for a in mordred["armour"]:
mordred["protection"] += a["protection"]
def pre_battle(knight: dict) -> Knight:

# apply weapon
mordred["power"] += mordred["weapon"]["power"]
fighter = Knight(knight)
fighter.apply_armor()
fighter.apply_weapon()

# apply potion if exist
if mordred["potion"] is not None:
if "power" in mordred["potion"]["effect"]:
mordred["power"] += mordred["potion"]["effect"]["power"]
if fighter.potion is not None:
fighter.apply_potion()

if "protection" in mordred["potion"]["effect"]:
mordred["protection"] += mordred["potion"]["effect"]["protection"]
return fighter

if "hp" in mordred["potion"]["effect"]:
mordred["hp"] += mordred["potion"]["effect"]["hp"]

# red_knight
red_knight = knightsConfig["red_knight"]
def battle(knights_config: dict) -> dict:
lancelot = pre_battle(knights_config["lancelot"])
arthur = pre_battle(knights_config["arthur"])
mordred = pre_battle(knights_config["mordred"])
red_knight = pre_battle(knights_config["red_knight"])

# apply armour
red_knight["protection"] = 0
for a in red_knight["armour"]:
red_knight["protection"] += a["protection"]
lancelot.damage(mordred)
arthur.damage(red_knight)

# 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"],
lancelot.name: lancelot.hp,
mordred.name: mordred.hp,
arthur.name: arthur.hp,
red_knight.name: red_knight.hp,
}


print(battle(KNIGHTS))
Loading