module 7 ex1 + 0

This commit is contained in:
2026-02-17 15:27:05 +01:00
parent 222b212162
commit ddb56264ae
9 changed files with 117 additions and 0 deletions

22
07/ex0/Card.py Normal file
View File

@@ -0,0 +1,22 @@
from abc import ABC, abstractmethod
from typing import Union
class Card(ABC):
def __init__(self, name: str, cost: int, rarity: str) -> None:
self.name = name
self.cost = cost
self.rarity = rarity
@abstractmethod
def play(self, game_state: dict) -> dict:
pass
def get_card_info(self) -> dict:
res = dict(self.__dict__)
return res
def is_playable(self, available_mana: int) -> bool:
if available_mana > 5:
return True
return False

32
07/ex0/CreatureCard.py Normal file
View File

@@ -0,0 +1,32 @@
from Card import Card
from typing import Dict, Union
class CreatureCard(Card):
def __init__(
self, name: str, cost: int, rarity: str, attack: int, health: int
) -> None:
super().__init__(name, cost, rarity)
self.attack = attack
self.health = health
def play(self, game_state: dict) -> dict:
try:
res: dict[str, Union[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_target(self, target: str) -> dict:
res: Dict[str, Union[int, str, bool]] = {}
res["attacker"] = self.name
res["target"] = target
res["damage_dealt"] = self.attack
res["combat_resolved"] = True
return res

6
07/ex0/__init__.py Normal file
View File

@@ -0,0 +1,6 @@
from .Card import Card
from .CreatureCard import CreatureCard
__version__ = "1.0.0"
__author__ = "moi"
__all__ = ["Card", "CreatureCard"]

20
07/ex0/main.py Normal file
View File

@@ -0,0 +1,20 @@
def main():
from CreatureCard import CreatureCard
game_state = {"player": "michel", "mana": 6}
print("=== DataDeck Card Foundation ===\n")
print("Testing Abstract Base Class Design:\n")
creature_card = CreatureCard("Fire Dragon", 5, "Legendary", 7, 5)
print(f"CreatureCard info:\n{creature_card.get_card_info()}")
print("\nPlaying Fire Dragon with 6 mana available:")
print(f"Playable: {creature_card.is_playable(game_state['mana'])}")
print(f"Play result: {creature_card.play(game_state)}")
print("\nFire Dragon attacks Goblin Warrior:")
print(f"Attack result: {creature_card.attack_target('Goblin Warrior')}")
print("\nTesting insufficient mana (3 available):")
print(f"Playable: {creature_card.is_playable(3)}")
print("\nAbstract pattern successfully demonstrated!")
if __name__ == "__main__":
main()

16
07/ex1/ArtifactCard.py Normal file
View File

@@ -0,0 +1,16 @@
from ex0.Card import Card
class ArtifactCard(Card):
def __init__(
self, name: str, cost: int, rarity: str, durability: int, effect: str
) -> None:
super().__init__(name, cost, rarity)
self.durability = durability
self.effect = effect
def play(self, game_state: dict) -> dict:
return super().play(game_state)
def activate_ability(self) -> dict:
pass

6
07/ex1/Deck.py Normal file
View File

@@ -0,0 +1,6 @@
from ex0.Card import Card
class Deck():
def __init__(self) -> None:
self:

15
07/ex1/SpellCard.py Normal file
View File

@@ -0,0 +1,15 @@
from ex0.Card import Card
class SpellCard(Card):
def __init__(
self, name: str, cost: int, rarity: str, effect_type: str
) -> None:
super().__init__(name, cost, rarity)
self.effect_type = effect_type
def play(self, game_state: dict) -> dict:
return super().play(game_state)
def resolve_effect(self, targets: list) -> dict:
pass

0
07/ex1/__init__.py Normal file
View File

0
07/ex1/main.py Normal file
View File