xam1816
Блин, вот тебе весь код на данный момент:
# Taken from mission The Weapons
import copy
import math
class Warrior:
maxhealth = 50
name = 'warrior'
def __init__(self, health = 50, attack = 5):
self.health = health
self.attack = attack
self.equipped_list = []
@property
def is_alive(self):
return self.health > 0
@property
def god_heal (self):
self.health = self.maxhealth
def hit(self, other):
other.health -= other.damage(self.attack)
def damage(self, attack):
if hasattr(self, 'defense'):
return max(attack - self.defense, 0)
else:
return max(attack, 0)
def equip_weapon(self, weapon):
if not isinstance(weapon, Weapon):
weapon = weapon()
list1 = [getattr(weapon,a) for a in dir(weapon) if not a.startswith('__')]
list2 = [a for a in dir(weapon) if not a.startswith('__')]
list3 = dict(zip(list2, list1))
for element in list3:
if hasattr(self, element):
setattr(self, element, max(getattr(self, element) + list3[element], 0))
self.equipped_list.append(weapon)
class Knight(Warrior):
maxhealth = 60
name = 'knight'
def __init__(self):
super().__init__(attack = 7)
class Defender(Warrior):
name = 'defender'
maxhealth = 60
def __init__(self):
super().__init__(health = 60, attack = 3)
self.defense = 2
class Vampire(Warrior):
maxhealth = 40
name = 'vampire'
def __init__(self):
super().__init__(health = 40, attack = 4)
self.vampirism = 50
def hit(self,other):
other.health -= other.damage(self.attack)
self.health = min(self.health + math.floor(self.vampirism
/ 100 * other.damage(self.attack)), self.maxhealth)
class Lancer(Warrior):
name = 'lancer'
def __init__(self):
super().__init__(attack = 6)
self.attack_2 = self.attack * 0.5
def hit_2(self,other):
other.health -= other.damage(self.attack_2)
class Healer(Warrior):
maxhealth = 60
name = 'healer'
def __init__(self):
super().__init__(health = 60, attack = 0)
self.heal_power = 2
def heal(self,other):
other.health = min(other.health + self.heal_power, other.maxhealth)
def fight(unit1,unit2):
first, sec = unit1, unit2
while unit1.is_alive and unit2.is_alive:
first.hit(sec)
first,sec = sec,first
return unit1.is_alive
class Army:
def __init__(self):
self.units = []
@property
def is_alive(self):
return self.units != []
@property
def first(self):
return self.units[0]
@property
def sec(self):
return self.units[1]
def del_unit(self,i):
self.units.pop(i)
def add_units(self,unit,count):
self.units += [unit() for i in range(count)]
class Battle:
@staticmethod
def fight(army1, army2):
first, sec = army1, army2
while first.is_alive and sec.is_alive:
first.first.hit(sec.first)
if len(first.units) > 1:
if isinstance(first.sec, Healer):
first.sec.heal(first.first)
if isinstance(first.first, Lancer) and len(sec.units) > 1:
first.first.hit_2(sec.sec)
if sec.sec.is_alive == False:
sec.del_unit(1)
if sec.first.is_alive == False:
sec.del_unit(0)
first, sec = army2, army1
first, sec = sec, first
return army1.is_alive
@staticmethod
def straight_fight(army1,army2):
list1, list2 = army1.units, army2.units
while list1 != [] and list2 != []:
excess = slice(min(len(list1),len(list2)),
max(len(list1),len(list2)))
list1_excess = copy.copy(list1[excess])
list2_excess = copy.copy(list2[excess])
fight_list = list(zip(list1,list2))
list1 = [i[0] for i in fight_list if fight(i[0],i[1]) == True]
list2 = [i[1] for i in fight_list if fight(i[0],i[1]) == False]
list1.extend(list1_excess)
list2.extend(list2_excess)
a = [(z.name,z.health) for z in list1]
print('army1 ',a)
b = [(z.name,z.health) for z in list2]
print('army2 - enemy ',b)
return list2 == []
class Weapon():
def __init__(self, health = 0, attack = 0, defense = 0, vampirism = 0,
heal_power = 0):
self.maxhealth = health
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__(health = 5, attack = 2)
class Shield(Weapon):
def __init__(self):
super().__init__(health = 20, attack = -1, defense = 2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(health = -15, attack = 5, defense = -2, vampirism = 10)
class Katana(Weapon):
def __init__(self):
super().__init__(health = -20, attack = 6, defense = -5, vampirism = 50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(health = 30, attack = 3, heal_power = 3)
и да, я попытался написать хитровыебанный метод,
и вроде даже получилось . А напрямую постоянно дописывать и расширять…когда там 10к+ строчек будет, посмотрим как ты порасширяешь
А старожилам: ответ на вопрос все-таки нашел сам, всего лишь надо было натолкнуть меня на атрибут __dict__ любого объекта Python, чтобы я не извращался вот так:
list1 = [getattr(weapon,a) for a in dir(weapon) if not a.startswith('__')]
list2 = [a for a in dir(weapon) if not a.startswith('__')]
list3 = dict(zip(list2, list1))
И тогда эта
работающая ересь превращается в работающее что-то покрасивее:
def equip_weapon(self,weapon):
if not isinstance(weapon, Weapon):
weapon = weapon()
for element in weapon.__dict__:
if hasattr(self, element):
setattr(self, element, max(getattr(self, element) + weapon.__dict__[element], 0))
self.equipped_list.append(weapon)
Кому интересно про __dict__, вот ссылка на хабр:
https://habr.com/ru/post/137415/Вот тебе ссылка на задание:
https://py.checkio.org/mission/the-warlords/solve/Ресурс рекомендован python community для обучения