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 #781

Open
wants to merge 7 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/__init__.py
Empty file.
Empty file added app/battle/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions app/battle/battle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from app.battle_preparations.utils import (apply_weapon,
apply_potion,
apply_armour)


def count_hp(knight: dict, opponent: dict) -> dict:
knight["hp"] -= (opponent["power"] - knight["protection"])
if knight["hp"] <= 0:
knight["hp"] = 0
return {knight["name"]: knight["hp"]}


def battle(knights: dict) -> dict:
apply_weapon(knights)
apply_armour(knights)
apply_potion(knights)
result = {}
knights_list = list(knights.values())
for stats in range(0, len(knights_list) // 2):
first_knight = knights_list[stats]
second_knight = knights_list[stats + 2]
result.update(count_hp(first_knight, second_knight))
result.update(count_hp(second_knight, first_knight))

return result
Empty file.
24 changes: 24 additions & 0 deletions app/battle_preparations/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def apply_weapon(knights: dict) -> None:
for knight, stats in knights.items():
stats["power"] += stats["weapon"]["power"]


def apply_potion(knights: dict) -> None:
for knight in knights.values():
if knight["potion"] is not None:
potion = knight["potion"]["effect"]
if "power" in potion:
knight["power"] += potion["power"]

if "protection" in potion:
knight["protection"] += potion["protection"]

if "hp" in potion:
knight["hp"] += potion["hp"]


def apply_armour(knights: dict) -> None:
for stats in knights.values():
stats["protection"] = 0
for armour in stats["armour"]:
stats["protection"] += armour["protection"]
134 changes: 6 additions & 128 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
KNIGHTS = {
from app.battle.battle import battle


knights_config = {
"lancelot": {
"name": "Lancelot",
"power": 35,
Expand Down Expand Up @@ -85,130 +88,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"]

# 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__":
print(battle(knights_config))
Empty file added app/stats/__init__.py
Empty file.
Loading