mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-14 05:06:55 +01:00
module 7 ex 0 to 2
This commit is contained in:
12
07/ex2/Combatable.py
Normal file
12
07/ex2/Combatable.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Combatable(ABC):
|
||||
@abstractmethod
|
||||
def attack(self, target: str) -> dict: ...
|
||||
|
||||
@abstractmethod
|
||||
def defend(self, incoming_damage: int) -> dict: ...
|
||||
|
||||
@abstractmethod
|
||||
def get_combate_stats(self) -> dict: ...
|
||||
87
07/ex2/EliteCard.py
Normal file
87
07/ex2/EliteCard.py
Normal file
@@ -0,0 +1,87 @@
|
||||
from ex0.Card import Card
|
||||
from .Combatable import Combatable
|
||||
from .Magical import Magical
|
||||
from typing import Union
|
||||
|
||||
|
||||
class EliteCard(Card, Combatable, Magical):
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
cost: int,
|
||||
rarity: str,
|
||||
damage: int,
|
||||
health: int,
|
||||
shield: int,
|
||||
initial_mana: int,
|
||||
spell_cost: int,
|
||||
) -> None:
|
||||
super().__init__(name, cost, rarity)
|
||||
self.damage = damage
|
||||
self.health = health
|
||||
self.shield = shield
|
||||
self.mana = initial_mana
|
||||
self.spell_cost = spell_cost
|
||||
|
||||
def play(self, game_state: dict) -> dict:
|
||||
try:
|
||||
res: dict[str, int | str] = {}
|
||||
if game_state["mana"] < 5:
|
||||
raise Exception("Not enough mana")
|
||||
res["card_played"] = self.name
|
||||
res["mana_used"] = 5
|
||||
res["effect"] = "Creature summoned to battlefield"
|
||||
return res
|
||||
except Exception as err:
|
||||
print(err)
|
||||
return {}
|
||||
|
||||
def attack(self, target: str) -> dict:
|
||||
return {
|
||||
"attacker": self.name,
|
||||
"target": target,
|
||||
"damage_dealt": self.damage,
|
||||
"combat_type": "melee",
|
||||
}
|
||||
|
||||
def defend(self, incoming_damage: int) -> dict:
|
||||
res: dict[str, Union[str, int, bool]] = {}
|
||||
res["defender"] = self.name
|
||||
if incoming_damage <= self.shield:
|
||||
res["damage_blocked"] = incoming_damage
|
||||
res["damage_taken"] = 0
|
||||
else:
|
||||
res["damage_taken"] = incoming_damage - self.shield
|
||||
res["damage_blocked"] = self.shield
|
||||
self.health -= incoming_damage - self.shield
|
||||
res["still_alive"] = self.health > 0
|
||||
return res
|
||||
|
||||
def get_combate_stats(self) -> dict:
|
||||
return {
|
||||
"damage": self.damage,
|
||||
"health": self.health,
|
||||
"shield": self.shield,
|
||||
}
|
||||
|
||||
def cast_spell(self, spell_name: str, targets: list) -> dict:
|
||||
try:
|
||||
if self.mana < self.spell_cost:
|
||||
raise Exception("Not enough mana")
|
||||
self.mana -= self.spell_cost
|
||||
return {
|
||||
"caster": self.name,
|
||||
"spell": spell_name,
|
||||
"targets": targets,
|
||||
"mana_used": self.spell_cost,
|
||||
}
|
||||
except Exception as err:
|
||||
print(err)
|
||||
return {}
|
||||
|
||||
def channel_mana(self, amount: int) -> dict:
|
||||
self.mana += amount
|
||||
return {"channeled": amount, "total_mana": self.mana}
|
||||
|
||||
def get_magic_stats(self) -> dict:
|
||||
return {"mana": self.mana, "spell cost": self.spell_cost}
|
||||
12
07/ex2/Magical.py
Normal file
12
07/ex2/Magical.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Magical(ABC):
|
||||
@abstractmethod
|
||||
def cast_spell(self, spell_name: str, targets: list) -> dict: ...
|
||||
|
||||
@abstractmethod
|
||||
def channel_mana(self, amount: int) -> dict: ...
|
||||
|
||||
@abstractmethod
|
||||
def get_magic_stats(self) -> dict: ...
|
||||
7
07/ex2/__init__.py
Normal file
7
07/ex2/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from .Combatable import Combatable
|
||||
from .Magical import Magical
|
||||
from .EliteCard import EliteCard
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "dgaillet"
|
||||
__all__ = ["Combatable", "Magical", "EliteCard"]
|
||||
21
07/ex2/main.py
Normal file
21
07/ex2/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
def main():
|
||||
from .EliteCard import EliteCard
|
||||
|
||||
print("=== DataDeck Ability System ===\n")
|
||||
card = EliteCard("Arcane Warrior", 25, "Legendary", 5, 20, 3, 4, 4)
|
||||
print("EliteCard capabilities:")
|
||||
print("- Card: ['play', 'get_card_info', 'is_playable']")
|
||||
print("- Combatable: ['attack', 'defend', 'get_combat_stats']")
|
||||
print("- Magical: ['cast_spell', 'channel_mana', 'get_magic_stats']")
|
||||
print("\nPlaying Arcane Warrior (Elite Card):\n")
|
||||
print("Combat phase:")
|
||||
print(f"Attack result: {card.attack('Enemy')}")
|
||||
print(f"Defense result: {card.defend(5)}")
|
||||
print("\nMagic phase:")
|
||||
print(f"Spell cast: {card.cast_spell('Fireball', ['Enemy1', 'Enemy2'])}")
|
||||
print(f"Mana channel: {card.channel_mana(3)}")
|
||||
print("Multiple interface implementation successful!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user