Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
it's long solution in Creative category for The Warlords by gatto_volante
import copy
##############WARRIORS#####################
class Warrior:
def __init__(self):
self.health=50
self.attack=5
self.defense=0
self.is_alive=True
self.heal_power=0
self.vampirism=0
def equip_weapon(self,weapon):
self.health+=weapon.health
self.attack+=weapon.attack
if self.defense!=0:
self.defense+=weapon.defense
if self.heal_power!=0:
self.heal_power+=weapon.heal_power
if self.vampirism!=0:
self.vampirism+=weapon.vampirism
pass
class Knight(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack=7
class Defender(Warrior):
def __init__(self):
Warrior.__init__(self)
self.health=60
self.attack=3
self.defense=2
class Vampire(Warrior):
def __init__(self):
Warrior.__init__(self)
self.health=40
self.attack=4
self.vampirism=50
class Lancer(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack=6
class Healer(Warrior):
def __init__(self):
Warrior.__init__(self)
self.health=60
self.attack=0
self.heal_power=2
def heal(self,unit):
b=type(unit)()
unit.health=min(unit.health+self.heal_power,b.health)
return unit
class Warlord(Warrior):
def __init__(self):
Warrior.__init__(self)
self.health=100
self.attack=4
self.defense=2
##############WEAPONS#####################
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=vampirism
self.heal_power=heal_power
class Sword(Weapon):
def __init__(self):
Weapon.__init__(self)
self.health=5
self.attack=2
class Shield(Weapon):
def __init__(self):
Weapon.__init__(self)
self.health=20
self.attack=-1
self.defense=2
class GreatAxe(Weapon):
def __init__(self):
Weapon.__init__(self)
self.health=-15
self.attack=5
self.defense=-2
self.vampirism=10
class Katana(Weapon):
def __init__(self):
Weapon.__init__(self)
self.health=-20
self.attack=6
self.defense=-5
self.vampirism=50
class MagicWand(Weapon):
def __init__(self):
Weapon.__init__(self)
self.health=30
self.attack=3
self.heal_power=3
##############ARMY#####################
class Army():
def __init__(self):
self.units=[]
self.a=1
def add_units(self,a, x):
warr=a()
for b in range(0,x):
a=True
if warr==Warlord():
if Warlord() in [type(x) for x in self.units]:
a=False
if a:
self.units.append(copy.copy(warr))
print(self.units)
def move_units(self):
u=[]
a=False
tu=[type(x) for x in self.units]
if Warlord() in tu:
a=True
if a:
k=False
if Lancer() in tu:
k=True
h=[]
w=0
for x in self.units():
if type(x)==Lancer():
u.append(x)
elif type(x)==Healer():
h.append(x)
elif w==0 and type(x)==Warlord():
w==x
elif k==False and x.attack>0 :
u.append(x)
if u!=[]:
self.units=u
for x in h:
self.units.insert(1, x)
else:
self.units=h
self.units.append(w)
##############BATTLE#####################
class Battle():
def fight(self,army1, army2):
a=True
while a:
u1=army1.units
u2=army2.units
lun1=len(u1)
lun2=len(u2)
b1=u1[1] if len(u1)>=2 else 0
b2=u2[1] if len(u2)>=2 else 0
combat, health, h1,h2=fight2(u1[0],u2[0],b1,b2)
if h1<=0:
u1.pop(1)
elif h1!=1000:
u1[1].health=h1
if h2<=0:
u2.pop(1)
elif h2!=1000:
u2[1].health=h2
#for x in u1:
#print(x.health)
#print(combat)
if combat==True:
u2.pop(0)
u1[0].health=health
if combat==False:
u1.pop(0)
u2[0].health=health
if len(u1)0]
u2=[x for x in u2 if x.health>0]
if len(u1)==0:
return False
if len(u2)==0:
return True
def fight2(unit_1, unit_2,b1=0,b2=0):
a=True
h1=1000
h2=1000
while a:
print(unit_1.health)
print(unit_2.health+1000)
at=unit_1.attack-unit_2.defense
unit_2.health-=at if at>0 else 0
unit_1.health+= at*unit_1.vampirism/100
if type(unit_1)==Lancer and b2!=0:
at=(unit_1.attack/2)-b2.defense
b2.health-=at if at>0 else 0
h2=b2.health
if type(b1)==Healer:
unit_1=b1.heal(unit_1)
if unit_2.health<=0:
unit_2.is_alive=False
return True, unit_1.health, h1, h2
at=unit_2.attack-unit_1.defense
unit_1.health-=at if at>0 else 0
unit_2.health+= at*unit_2.vampirism/100
if type(unit_2)==Lancer and b1!=0:
at=(unit_2.attack/2)-b1.defense
b1.health-=at if at>0 else 0
h1=b1.health
if type(b2)==Healer:
unit_2=b2.heal(unit_2)
if unit_1.health<=0:
unit_1.is_alive=False
return False, unit_2.health, h1, h2
a=False
def fight(unit_1, unit_2):
return fight2(unit_1, unit_2)[0]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
ronald = Warlord()
heimdall = Knight()
fight(heimdall, ronald) == False
my_army = Army()
my_army.add_units(Warlord, 1)
my_army.add_units(Warrior, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 2)
enemy_army = Army()
enemy_army.add_units(Warlord, 3)
enemy_army.add_units(Vampire, 1)
enemy_army.add_units(Healer, 2)
enemy_army.add_units(Knight, 2)
my_army.move_units()
enemy_army.move_units()
type(my_army.units[0]) == Lancer
type(my_army.units[1]) == Healer
type(my_army.units[-1]) == Warlord
type(enemy_army.units[0]) == Vampire
type(enemy_army.units[-1]) == Warlord
type(enemy_army.units[-2]) == Knight
#6, not 8, because only 1 Warlord per army could be
len(enemy_army.units) == 6
battle = Battle()
battle.fight(my_army, enemy_army) == True
print("Coding complete? Let's try tests!")
Feb. 19, 2019
Comments: