Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Warlords by alexandrov.net
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.max_health = self.health
@property
def is_alive(self):
return self.health > 0
def equip_weapon(self, weapon_name):
#all warriors has attack and health
#warrior attribute + weapon attribute or 0
self.attack= max(0, self.attack+ weapon_name.attack)
self.max_health = max(0, self.max_health + weapon_name.health)
self.health = max(0, self.health + weapon_name.health)
#pass if warrior has no attribute (defense,vampirism,heal_power)
try:
self.defense=max(0,self.defense+weapon_name.defense)
except:
pass
try:
self.vampirism=max(0,self.vampirism+weapon_name.vampirism)
except:
pass
try:
self.heal_power = max(0,self.heal_power+weapon_name.heal_power)
except:
pass
#flag False mean all other warriors except 'name'
def is_name(self, name,flag=True):
return type(self) == type(name()) if flag else not(type(self) == type(name()))
def __repr__(self):
return f'{str(type(self).__name__)} - {self.health}'
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 0.5 # marker vampirism
self.max_health = self.health
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defense = 2 # marker defense
self.max_health = self.health
class Healer(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 0
self.heal_power = 2 #heal
self.max_health = self.health
def heal(self, unit):
unit.health = min(self.max_health, unit.health + self.heal_power)
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 6
# marker lance & attack force to second
self.extra_attack = 3
self.max_health = self.health
class Warlord(Warrior):
def __init__(self):
super().__init__()
self.health = 100
self.attack = 4
self.defense = 2
self.max_health = self.health
def damage(unit_1, unit_2):
# attack - defense if unit has defense
try:
return max(unit_1.attack - unit_2.defense, 0)
except:
return unit_1.attack
#lancer
def extra_damage(attack_force, unit):
try:
return max(attack_force - unit.defense, 0)
except:
return attack_force
# 1v1 fight, only vampirism marker
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_2.health -= damage(unit_1, unit_2)
if hasattr(unit_1, 'vampirism'):
vampirism_power = int(damage(unit_1, unit_2) * unit_1.vampirism)
unit_1.health = min(unit_1.max_health, unit_1.health + vampirism_power)
if unit_2.is_alive:
unit_1.health -= damage(unit_2, unit_1)
if hasattr(unit_2, 'vampirism'):
vampirism_power=int(damage(unit_2, unit_1) * unit_2.vampirism)
unit_2.health = min(unit_2.max_health, unit_2.health + vampirism_power)
return unit_1.is_alive
class Army():
def __init__(self):
self.units = []
def add_units(self, unit, amount):
if type(unit()) == type(Warlord()):
#only 1 warlord in army
if not self.is_warlord:
self.units.append(unit())
else:
for i in range(amount):
self.units.append(unit())
def clear_corpse(self):
self.units = list(filter(lambda x: x.is_alive, self.units))
self.move_units()
#without move_units for straight
def clear_straight(self):
self.units = list(filter(lambda x: x.is_alive, self.units))
def move_units(self):
#only warlord move
if not self.is_warlord:
return
#warlord to last cell
for i in range(len(self.units)-1):
if self.units[i].is_name(Warlord):
self.units.append(self.units.pop(i))
break
lancers = list(filter(lambda x: x.is_name(Lancer), self.units))
no_lancers = list(filter(lambda x: x.is_name(Lancer, False), self.units))
self.units = lancers + no_lancers
healers = list(filter(lambda x:x.is_name(Healer), self.units))
no_healers = list(filter(lambda x:x.is_name(Healer, False), self.units))
#warriors in no_healers - > paste healers to second cell
if len(no_healers)>1:
self.units = no_healers[0::-1]+healers+no_healers[1:]
#only warlord!!!
else:
self.units = healers + no_healers
#army.is_alive
@property
def is_alive(self):
return self.units != []
@property
def front(self):
return self.units[0]
@property
def second(self):
try:
return self.units[1]
except:
return
@property
def is_warlord(self):
return any(type(unit)==type(Warlord()) for unit in self.units)
class Battle():
'''
front unit - army.units[0]
second unit army.units[1] if len(army.units)>1
1st army turn army_turn(armym, enemy)
unit2.health - damage unit1->unit2
if vampirism raise unit1.health
if lance extra damage to second unit
if second Healer - > healing(first)
'''
def army_turn(self, army, enemy):
enemy.front.health -= damage(army.front, enemy.front)
# try looks better than hasattr(unit,'marker')
try:
army.front.health += int(damage(army.front, enemy.front) * army.front.vampirism)
except:
pass # No vampirism
try:
enemy.second.health -= extra_damage(army.front.extra_attack, enemy.second)
except:
pass # No lance or no enemy.second()
try:
army.second.heal(army.front)
except:
pass # No Healer
'''
1st army turn army_turn(army, enemy)
if front.enemy alive -> revenge turn
clear_corpse
while army.is_alive
'''
def fight(self, army1, army2):
#to move units Warlord
while army1.is_alive and army2.is_alive:
self.army_turn(army1, army2)
if army2.front.is_alive:
# mb second died from extra damage
army2.clear_corpse()
self.army_turn(army2, army1)
# clear after turn
army1.clear_corpse()
army2.clear_corpse()
return army1.is_alive
#all vs all fight
def straight_fight(self, army1, army2):
while (army1.is_alive and army2.is_alive):
#short army len
x = min(len(army1.units), len(army2.units))
for i in range(x):
fight(army1.units[i], army2.units[i])
army1.clear_straight()
army2.clear_straight()
return army1.is_alive
class Weapon:
def __init__(self, health=0, attack=0, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = int(vampirism/100)
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__()
self.attack = 2
self.health = 5
class Shield(Weapon):
def __init__(self):
super().__init__()
self.attack = -1
self.health = 20
self.defense = 2
class GreatAxe(Weapon):
def __init__(self):
super().__init__()
self.attack = 5
self.health = -15
self.defense = -2
self.vampirism = 0.1
class Katana(Weapon):
def __init__(self):
super().__init__()
self.attack = 6
self.health = -20
self.defense = -5
self.vampirism = 0.5
class MagicWand(Weapon):
def __init__(self):
super().__init__()
self.attack = 3
self.health = 30
self.heal_power = 3
Oct. 22, 2018