Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Army membership attribute, and normal health solution in Clear category for The Lancers by PythonWithPI
from collections import deque
from typing import List, Type
class Warrior:
__slots__=("is_alive","health","army")
normal_health=50
def __init__(self,army=None):
self.health=type(self).normal_health
self.is_alive=True
self.army=army
#class variable
attack=5
def take_hit(self, attack) -> int:
damage=min(attack,self.health)
self.health-=damage
if self.health==0:
self.is_alive=False
return damage
def hit(self,other):
other.take_hit(self.attack)
def fight(self,other) -> bool:
while self.is_alive and other.is_alive:
self.hit(other)
if other.is_alive:
other.hit(self)
return self.is_alive
def __str__(self) -> str:
return "{}(health={},is_alive={},attack={},army={})".format(type(self).__name__,self.health,self.is_alive,self.attack,self.army)
__repr__=__str__
def __hash__(self) -> int: id(self) #People's identities are immutable supposedly
def __eq__(self,other) -> bool: return self is other
def __ne__(self,other) -> bool: return self is not other
def __bool__(self) -> bool: return self.is_alive
fight=Warrior.fight
class Knight(Warrior):
__slots__=() #no instance members
attack=7
class Defender(Warrior):
__slots__=() #no instance members
normal_health=60
attack=3
defense=2
def take_hit(self,attack):
if attack>self.defense:
return super().take_hit(attack-self.defense)
return 0
class Vampire(Warrior):
__slots__=()
normal_health=40
attack=4
vampirism=.5
def hit(self, other):
self.health+=self.vampirism*other.take_hit(self.attack)
class Lancer(Warrior):
__slots__=()
attack=6
def hit(self,other):
if other.army and len(other.army)>=2: # Check if other is in an army and has backup
next=other.army[1]
damage=other.take_hit(self.attack)
# possibly next.take_hit(self.attack/2)
next.take_hit(damage/2)
else: #Otherwise hit like a Warrior
super().hit(other)
class Army(deque):
__slots__=() #no instance members
def add_units(self, unit_class: Type[Warrior], quantity: int):
self.extend(map(unit_class,(self,)*quantity))
# for i in range(quantity): self.append(unit_class(self))
class Battle:
__slots__=()
def fight(self, army1: Army, army2: Army) -> bool:
while army1 and army2:
army1[0].fight(army2[0])
#remove the bodies
if not army1[0]:
del army1[0]
if not army2[0]:
del army2[0]
return bool(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()
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
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 4)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 2)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 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(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")
Feb. 12, 2019