Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Units by Calen
class Army:
@classmethod
def train_swordsman(cls, name):
return Swordsman(name, cls)
@classmethod
def train_lancer(cls, name):
return Lancer(name, cls)
@classmethod
def train_archer(cls, name):
return Archer(name, cls)
class Warrior:
def __init__(self, name, army):
self.name = name
self.armytype = army.armytype
self.unittype = army.units[self.spec]
def introduce(self):
return f"{self.unittype} {self.name}, {self.armytype} {self.spec}"
class Swordsman(Warrior):
def __init__(self, name, army):
self.spec = 'swordsman'
super().__init__(name, army)
class Lancer(Warrior):
def __init__(self, name, army):
self.spec = 'lancer'
super().__init__(name, army)
class Archer(Warrior):
def __init__(self, name, army):
self.spec = 'archer'
super().__init__(name, army)
class AsianArmy(Army):
armytype = 'Asian'
units = {"swordsman": 'Samurai', "lancer": 'Ronin', "archer": 'Shinobi'}
class EuropeanArmy(Army):
armytype = 'European'
units = {"swordsman": 'Knight', "lancer": 'Raubritter', "archer": 'Ranger'}
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
my_army = EuropeanArmy()
enemy_army = AsianArmy()
soldier_1 = my_army.train_swordsman("Jaks")
soldier_2 = my_army.train_lancer("Harold")
soldier_3 = my_army.train_archer("Robin")
soldier_4 = enemy_army.train_swordsman("Kishimoto")
soldier_5 = enemy_army.train_lancer("Ayabusa")
soldier_6 = enemy_army.train_archer("Kirigae")
assert soldier_1.introduce() == "Knight Jaks, European swordsman"
assert soldier_2.introduce() == "Raubritter Harold, European lancer"
assert soldier_3.introduce() == "Ranger Robin, European archer"
assert soldier_4.introduce() == "Samurai Kishimoto, Asian swordsman"
assert soldier_5.introduce() == "Ronin Ayabusa, Asian lancer"
assert soldier_6.introduce() == "Shinobi Kirigae, Asian archer"
print("Coding complete? Let's try tests!")
March 23, 2021