Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Straight Fight by juestr
class Warrior:
def __init__(self, health=50, attack=5, deephit=0, defense=0, vampirism=0, healing=0):
self.health = self.max_health = health
self.attack = attack
self.deephit = deephit
self.defense = defense
self.vampirism = vampirism
self.healing = healing
@property
def is_alive(self):
return self.health > 0
def hit(self, other, ally=None, other2=None):
'''Hit other unit(s)'''
damage = max(self.attack - other.defense, 0)
other.health -= damage
self.health += damage * self.vampirism // 100
if other2:
other2.health -= damage * self.deephit // 100
if ally and ally.is_alive:
ally.heal(self)
def heal(self, ally):
ally.health = min(ally.max_health, ally.health + self.healing)
def __repr__(self):
return f'{self.__class__.__name__}({self.health})'
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3, defense=2)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4, vampirism=50)
class Lancer(Warrior):
def __init__(self):
super().__init__(health=50, attack=6, deephit=50)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0, healing=2)
def fight(unit_1, unit_2, unit_1b=None, unit_2b=None):
while unit_1.is_alive and unit_2.is_alive:
unit_1.hit(unit_2, unit_1b, unit_2b)
if unit_2.is_alive:
unit_2.hit(unit_1, unit_2b, unit_1b)
return unit_1.is_alive
class Army:
def __init__(self):
self.units = []
def add_units(self, factory, number):
self.units.extend(factory() for _ in range(number))
def __iter__(self):
return iter(self.units)
def __len__(self):
return len(self.units)
class Battle:
# a class for just this is silly
def fight(self, army1, army2):
def nextw(it):
return next(it, None)
print('battle starts', '\n army 1:', army1.units, '\n army 2:', army2.units)
a1iter, a2iter = iter(army1), iter(army2)
w1, w1b = nextw(a1iter), nextw(a1iter)
w2, w2b = nextw(a2iter), nextw(a2iter)
while True:
if w1 is None: return False
if w2 is None: return True
print('fight ', w1b, w1, '--', w2, w2b)
if fight(w1, w2, w1b, w2b):
print('a1 won', w1b, w1, '--', w2, w2b)
w2, w2b = w2b, nextw(a2iter)
else:
print('a2 won', w1b, w1, '--', w2, w2b)
w1, w1b = w1b, nextw(a1iter)
def straight_fight(self, army1, army2):
print('battle starts with straight fight')
while len(army1) and len(army2):
print(' army 1:', army1.units, '\n army 2:', army2.units)
for w1, w2 in zip(army1.units[:], army2.units[:]):
print('fight ', w1, '--', w2)
result = fight(w1, w2)
print(f'a{2-int(result)} won', w1, '--', w2)
(army1, army2)[result].units.remove((w1, w2)[result])
return bool(len(army1))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
#fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()
eric = Vampire()
adam = Vampire()
richard = Defender()
ogre = Warrior()
freelancer = Lancer()
vampire = Vampire()
priest = Healer()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
assert fight(bob, mike) == False
assert fight(lancelot, rog) == True
assert fight(eric, richard) == False
assert fight(ogre, adam) == True
assert fight(freelancer, vampire) == True
assert freelancer.is_alive == True
assert freelancer.health == 14
priest.heal(freelancer)
assert freelancer.health == 16
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 4)
enemy_army.add_units(Healer, 1)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
enemy_army.add_units(Healer, 1)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 1)
army_3.add_units(Healer, 1)
army_3.add_units(Defender, 2)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 1)
army_4.add_units(Healer, 1)
army_4.add_units(Lancer, 2)
army_5 = Army()
army_5.add_units(Warrior, 10)
army_6 = Army()
army_6.add_units(Warrior, 6)
army_6.add_units(Lancer, 5)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
assert battle.straight_fight(army_5, army_6) == False
print("Coding complete? Let's try tests!")
April 19, 2019