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.
-
π Exercise 0 - Creature Factory
Creation of creature families using the Abstract Factory pattern -
βοΈ Exercise 1 - Capabilities
Extending creatures with reusable behaviors through abstract capabilities -
π§ Exercise 2 - Abstract Strategy
Dynamic battle behavior using the Strategy pattern
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 & battlesThis exercise introduces the Abstract Factory design pattern to create families of creatures.
- Defines common attributes (
name,type) - Provides a concrete
describe()method - Declares an abstract
attack()method
Flameling/Pyrodon(Flame family)Aquabub/Torragon(Aqua family)- Each implements its own attack behavior
- Defines methods to create:
- base creature
- evolved creature
FlameFactoryβ createsFlamelingandPyrodonAquaFactoryβ createsAquabubandTorragon
- Tests factory creation
- Demonstrates polymorphism (
describe()+attack()) - Simulates a simple battle between creatures
- The
ex0package 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
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 capabilitiesThis 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.
- Defines the
heal()abstract method - Implemented by healing creatures
- Returns a string describing the healing action
- Defines:
transform()revert()
- Uses an internal state to modify behavior dynamically
- Impacts how
attack()behaves
-
Sproutling(base)- Basic attack
- Heals itself for a small amount
-
Bloomelle(evolved)- Stronger attack
- Heals itself and others for a large amount
-
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
- Creates:
Sproutling(base)Bloomelle(evolved)
- Creates:
Shiftling(base)Morphagon(evolved)
-
Tests healing creatures:
- describe β attack β heal
-
Tests transform creatures:
- describe β attack β transform β attack β revert
-
Demonstrates dynamic behavior based on capabilities
- 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
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 strategiesThis 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.
-
Defines two methods:
is_valid(creature) -> boolact(creature) -> str or None
-
is_validensures the strategy is compatible with the creature -
actdefines how the creature behaves in battle
- Valid for all creatures
- Behavior:
- Performs a standard
attack()
- Performs a standard
- Valid only for creatures with transform capability
- Behavior:
transform()βattack()βrevert()
- Valid only for creatures with healing capability
- Behavior:
attack()βheal()
- If a strategy is used with an incompatible creature:
is_valid()returnsFalse- Calling
act()raises a custom exception
- 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
-
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