Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

README.md

🎴 Module 07 - DataDeck

πŸ“– Overview

This project is part of the 42 curriculum and focuses on advanced object-oriented programming concepts in Python.

The program simulates the creation and interaction of different creature types, allowing them to perform actions such as attacking, healing, transforming, and adapting their behavior during battles.

The goal is to build a flexible and extensible architecture, promoting clean design, separation of concerns, and dynamic behavior management.

πŸ“š Table of Contents


Exercise 0 - Creature Factory

πŸ“ Directory Structure

ex0/
β”œβ”€β”€ Creature.py          # Abstract Creature base class
β”œβ”€β”€ CreatureFactory.py   # Abstract factory interface
β”œβ”€β”€ FlameFactory.py      # Factory for Flame creatures
β”œβ”€β”€ AquaFactory.py       # Factory for Aqua creatures
β”œβ”€β”€ Flameling.py         # Base Flame creature
β”œβ”€β”€ Pyrodon.py           # Evolved Flame creature
β”œβ”€β”€ Aquabub.py           # Base Aqua creature
β”œβ”€β”€ Torragon.py          # Evolved Aqua creature
└── __init__.py          # Exposes factory classes

battle.py                # Test script for creature creation & battles

πŸ“Œ Description

This exercise introduces the Abstract Factory design pattern to create families of creatures.

Creature (Abstract Class)

  • Defines common attributes (name, type)
  • Provides a concrete describe() method
  • Declares an abstract attack() method

Concrete Creatures

  • Flameling / Pyrodon (Flame family)
  • Aquabub / Torragon (Aqua family)
  • Each implements its own attack behavior

CreatureFactory (Abstract Class)

  • Defines methods to create:
    • base creature
    • evolved creature

Concrete Factories

  • FlameFactory β†’ creates Flameling and Pyrodon
  • AquaFactory β†’ creates Aquabub and Torragon

battle.py

  • Tests factory creation
  • Demonstrates polymorphism (describe() + attack())
  • Simulates a simple battle between creatures

Notes

  • The ex0 package does not expose concrete Creature classes directly
  • Only factory classes are exposed to ensure proper encapsulation
  • This design allows easy extension with new creature families without modifying existing code

Exercise 1 - Capabilities

πŸ“ Directory Structure

ex1/
β”œβ”€β”€ HealCapability.py            # Heal capability interface
β”œβ”€β”€ TransformCapability.py       # Transform capability interface
β”œβ”€β”€ Sproutling.py                # Base healing creature
β”œβ”€β”€ Bloomelle.py                 # Evolved healing creature
β”œβ”€β”€ Shiftling.py                 # Base transform creature
β”œβ”€β”€ Morphagon.py                 # Evolved transform creature
β”œβ”€β”€ HealingCreatureFactory.py    # Factory for healing creatures
β”œβ”€β”€ TransformCreatureFactory.py  # Factory for transform creatures
└── __init__.py                  # Exposes factory classes

capacitor.py                     # Test script demonstrating capabilities

πŸ“Œ Description

This exercise extends the creature system by introducing capabilities using abstract classes.

Instead of modifying the base Creature, new behaviors are added through separate capability classes, promoting flexibility and reuse.

Capabilities

HealCapability

  • Defines the heal() abstract method
  • Implemented by healing creatures
  • Returns a string describing the healing action

TransformCapability

  • Defines:
    • transform()
    • revert()
  • Uses an internal state to modify behavior dynamically
  • Impacts how attack() behaves

Healing Creatures

  • Sproutling (base)

    • Basic attack
    • Heals itself for a small amount
  • Bloomelle (evolved)

    • Stronger attack
    • Heals itself and others for a large amount

Transform Creatures

  • Shiftling (base)

    • Normal attack by default
    • Can transform to enhance attack
    • Reverts to original state
  • Morphagon (evolved)

    • More powerful transformed attack
    • Uses the same transform/revert cycle with stronger effects

Factories

HealingCreatureFactory

  • Creates:
    • Sproutling (base)
    • Bloomelle (evolved)

TransformCreatureFactory

  • Creates:
    • Shiftling (base)
    • Morphagon (evolved)

capacitor.py

  • Tests healing creatures:

    • describe β†’ attack β†’ heal
  • Tests transform creatures:

    • describe β†’ attack β†’ transform β†’ attack β†’ revert
  • Demonstrates dynamic behavior based on capabilities


Notes

  • Capabilities are implemented as independent abstract classes
  • They do not inherit from Creature, enabling composition
  • Behavior is extended without modifying the base class
  • Output must strictly match the subject requirements

Exercise 2 - Abstract Strategy

πŸ“ Directory Structure

ex2/
β”œβ”€β”€ BattleStrategy.py       # Abstract BattleStrategy interface
β”œβ”€β”€ NormalStrategy.py       # Strategy for basic attack behavior
β”œβ”€β”€ DefensiveStrategy.py    # Strategy for attack + heal behavior
β”œβ”€β”€ AggressiveStrategy.py   # Strategy for transform + attack + revert behavior
└── __init__.py             # Exposes strategy classes

tournament.py               # Test script simulating battles with strategies

πŸ“Œ Description

This exercise introduces the Strategy design pattern to control how creatures behave during battles.

Instead of hardcoding behavior inside the creatures, actions are delegated to strategy objects, allowing dynamic behavior selection at runtime.


BattleStrategy (Abstract Class)

  • Defines two methods:

    • is_valid(creature) -> bool
    • act(creature) -> str or None
  • is_valid ensures the strategy is compatible with the creature

  • act defines how the creature behaves in battle


Concrete Strategies

NormalStrategy

  • Valid for all creatures
  • Behavior:
    • Performs a standard attack()

AggressiveStrategy

  • Valid only for creatures with transform capability
  • Behavior:
    • transform() β†’ attack() β†’ revert()

DefensiveStrategy

  • Valid only for creatures with healing capability
  • Behavior:
    • attack() β†’ heal()

Exception Handling

  • If a strategy is used with an incompatible creature:
    • is_valid() returns False
    • Calling act() raises a custom exception

tournament.py

  • Creates multiple creatures using factories (from ex0 and ex1)
  • Assigns a strategy to each creature
  • Simulates a tournament:
    • Each creature fights all others
    • Strategy determines behavior during combat
  • Handles invalid strategy-creature combinations safely

Notes

  • The Strategy pattern allows behavior to change without modifying creature classes

  • Eliminates conditional logic (if transform, if heal, etc.)

  • Promotes flexibility and scalability

  • Works together with:

    • Factory pattern (object creation)
    • Capability pattern (behavior composition)
  • Output and error handling must strictly match the subject requirements