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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
86 changes: 86 additions & 0 deletions app/data.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,
}
}
}
}
30 changes: 30 additions & 0 deletions app/knight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations


class Knight:
def __init__(self, knight: dict) -> None:
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
self.knight = knight
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
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
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
return knight
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved

def __isub__(self, other: Knight) -> Knight:
self.hp -= other.power
self.hp = max(self.hp, 0)
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
return self
RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
213 changes: 11 additions & 202 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -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

RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved
# 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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a code quality issue. The function battle is doing too many things: it prepares knights for battle, conducts the battle, and returns the results. Consider breaking it down into smaller functions, each responsible for a single task. This would make your code more readable and easier to maintain.

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()}

RadeonT800 marked this conversation as resolved.
Show resolved Hide resolved

print(battle(KNIGHTS))
Loading