mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-13 20:56:54 +01:00
module 7 ex1 + 0
This commit is contained in:
22
07/ex0/Card.py
Normal file
22
07/ex0/Card.py
Normal 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
32
07/ex0/CreatureCard.py
Normal 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
6
07/ex0/__init__.py
Normal 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
20
07/ex0/main.py
Normal 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
16
07/ex1/ArtifactCard.py
Normal 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
6
07/ex1/Deck.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from ex0.Card import Card
|
||||
|
||||
|
||||
class Deck():
|
||||
def __init__(self) -> None:
|
||||
self:
|
||||
15
07/ex1/SpellCard.py
Normal file
15
07/ex1/SpellCard.py
Normal 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
0
07/ex1/__init__.py
Normal file
0
07/ex1/main.py
Normal file
0
07/ex1/main.py
Normal file
Reference in New Issue
Block a user