from random import randint
class Card:
def __init__(self, group: str = "NoGroup", name: str = "Нет имени", hp: int = 0, damage: int = 0,
group_attr: int = 0):
self._group = group.lower()
self._name = name
self._hp = hp
self._damage = damage
self._group_attr = group_attr
self._set_group_attr()
def _set_group_attr(self):
if self._group == "intelegence":
self._damage += self._group_attr
elif self._group == "strenght":
self._hp += self._group_attr
def get_info(self):
if self._group == "intelegence":
return f"Группа {self._group.capitalize()} | Имя: {self._name} | Здоровье: {self._hp} | Физический урон: {self._damage - self._group_attr} | Магический урон: {self._group_attr}"
if self._group == "agility":
return f"Группа {self._group.capitalize()} | Имя: {self._name} | Здоровье: {self._hp} | Физический урон: {self._damage} | Блокируемый урон: {self._group_attr * 0.5}"
if self._group == "strenght":
return f"Группа {self._group.capitalize()} | Имя: {self._name} | Здоровье: {self._hp} | Физический урон: {self._damage} | Дополнительное ХП: {self._group_attr}"
self._group = "NoGroup"
return f"Группа {self._group} | Имя: {self._name} | Здоровье: {self._hp} | Физический урон: {self._damage}"
def get_damage(self, damage):
if self._group == "agility":
damage -= int(self._group_attr * 0.5)
self._hp -= damage
if self._hp <= 0:
self._hp = 0
return damage
def attack(self, enemy):
res_damage = enemy.get_damage(self._damage + randint(-20, 20))
return res_damage
@property
def group(self):
return self._group
@property
def name(self):
return self._name
@property
def hp(self):
return self._hp
@property
def damage(self):
return self._damage
@property
def group_attr(self):
return self._group_attr
class Deck:
def __init__(self, name: str = "Колода", date: str = "01.01.2022"):
self.__cards = []
self._name = name
self._date = date
def append(self, card: Card):
self.__cards.append(card)
return f"Карта {card.name} добавлена в колоду '{self.name}'"
def get_info(self):
info = []
for card in self.__cards:
info.append(card.get_info())
return f"\nВо мне {self.__len__()} карт, а именно: \n" + "\n".join(info) + "\n"
def sort_by(self, key: str = "group"):
keys = "group name hp damage"
key = key.lower()
if key not in keys:
print("Критерий не понятен")
return 0
if key == "group":
self.__cards.sort(key=lambda card: card.group, reverse=False)
if key == "name":
self.__cards.sort(key=lambda card: card.name)
elif key == "hp":
self.__cards.sort(key=lambda card: card.hp, reverse=True)
elif key == "damage":
self.__cards.sort(key=lambda card: card.damage, reverse=True)
def random_sort(self):
self.sort_by(['group', 'name', 'hp', 'damage'][randint(0, 3)])
@property
def name(self):
return self._name
def __len__(self):
return len(self.__cards)
def __iter__(self):
self.current = 0
return self
def __next__(self):
if self.current < len(self.__cards):
self.current += 1
return self.__cards[self.current - 1]
else:
raise StopIteration
def __getitem__(self, index: int):
if 0 <= index <= len(self.__cards):
return self.__cards[index]
else:
raise IndexError
def __delitem__(self, index: int):
if 0 <= index <= len(self.__cards):
del self.__cards[index]
else:
raise IndexError
class Fighter:
def __init__(self, deck_0, deck_1):
self._result = "Сражение еще не было проведено!"
self._looser = None
self._winner = None
self._attacker = deck_0
self._defender = deck_1
def _randomer(self):
self._attacker.random_sort()
self._defender.random_sort()
if randint(0, 1):
self._attacker, self._defender = self._defender, self._attacker
print(f"Атаковать первой будет колода '{self._attacker.name}'")
else:
print(f"Атаковать первой будет колода '{self._attacker.name}'")
def _fight(self):
attacker = self._attacker
defender = self._defender
while len(attacker) != 0 and len(defender) != 0:
attacker_unit = attacker[0]
defender_unit = defender[0]
res_damage = attacker_unit.attack(defender_unit)
print(
f"{attacker.name}--->'{attacker_unit.name}' атаковал '{defender_unit.name}' тычкой в {res_damage} урона. У него осталось {defender_unit.hp}ХП--->{defender.name}\n")
if defender_unit.hp > 0:
res_damage = defender_unit.attack(attacker_unit)
print(
f"{defender.name}<-------Но '{defender_unit.name}' ответил '{attacker_unit.name}' тычкой в {res_damage}. У него осталось {attacker_unit.hp}ХП<-------{attacker.name}\n")
if defender_unit.hp == 0 or attacker_unit.hp == 0:
victim = None
if defender_unit.hp == 0:
victim = defender
elif attacker_unit.hp == 0:
victim = attacker
else:
print("Ошибка")
return 0
print(f"Откис {victim[0].name} из колоды '{victim.name}'")
del victim[0]
if len(victim) == 0:
break
print("Новый боец идет ринг!")
print(victim.get_info())
return 1
def _define_winner(self):
if len(self._attacker) == 0 and len(self._defender) != 0:
print(f"Колода '{self._defender.name}' победила!")
self._looser = self._attacker
self._winner = self._defender
elif len(self._attacker) != 0 and len(self._defender) == 0:
print(f"Колода '{self._attacker.name}' победила!")
self._looser = self._defender
self._winner = self._attacker
else:
print("Так быть не должно!")
return 0
return 1
def _set_result(self):
self._result = f"Представляем колоду победителя:\n {self._winner.get_info()} \n" + f"Колода проигравшего:\n Пуста! ;("
@property
def result(self):
return self._result
def fight_conductor(self):
self._randomer()
self._fight()
self._define_winner()
self._set_result()
print("------------------------------------")
print("Закончили упражнение!")
print("------------------------------------")
my_deck = Deck("Моя колода", "25.05.2022")
my_cards = [
Card("Strenght", "Рудге", 800, 60, 200),
Card("Agility", "Крип", 500, 100, 30),
Card("Strenght", "Земеля", 700, 60, 80),
Card("Intelegence", "Инвокер", 500, 100, 80),
Card("Неизвестно", "PB", 600, 100, 80)
]
for card in my_cards:
my_deck.append(card)
# print(my_deck.give_info())
enemy_deck = Deck("Колода засранца", "26.05.2022")
enemy_cards = [
Card("Strenght", "Рудге", 800, 60, 200),
Card("Intelegence", "Инвокер", 500, 100, 80),
Card("Неизвестно", "PB", 600, 100, 80)
]
for card in enemy_cards:
enemy_deck.append(card)
# print(enemy_deck.give_info())
# comparator = Comparator()
# comparator.compare_deck(my_deck, enemy_deck)
fighter = Fighter(my_deck, enemy_deck)
fighter.fight_conductor()
print(fighter.result)
from random import randint
class Card:
def __init__(self, group: str = "NoGroup", name: str = "Нет имени", hp: int = 0, damage: int = 0,
group_attr: int = 0):
self.group = group.lower()
self.name = name
self.hp = hp
self.damage = damage
self.group_attr = group_attr
if self.group == "intelegence":
self.damage += self.group_attr
elif self.group == "strenght":
self.hp += self.group_attr
def give_info(self):
if self.group == "intelegence":
return f"Группа {self.group.capitalize()} | Имя: {self.name} | Здоровье: {self.hp} | Физический урон: {self.damage - self.group_attr} | Магический урон: {self.group_attr}"
if self.group == "agility":
return f"Группа {self.group.capitalize()} | Имя: {self.name} | Здоровье: {self.hp} | Физический урон: {self.damage} | Блокируемый урон: {self.group_attr * 0.5}"
if self.group == "strenght":
return f"Группа {self.group.capitalize()} | Имя: {self.name} | Здоровье: {self.hp} | Физический урон: {self.damage} | Дополнительное ХП: {self.group_attr}"
self.group = "NoGroup"
return f"Группа {self.group} | Имя: {self.name} | Здоровье: {self.hp} | Физический урон: {self.damage}"
def get_damage(self, damage):
if self.group == "agility":
damage -= self.group_attr * 0.5
self.hp -= damage
if self.hp <= 0:
self.hp = 0
return damage
def attack(self, enemy):
res_damage = enemy.get_damage(self.damage + randint(-20, 20))
return res_damage
class Deck:
def __init__(self, name: str = "Колода", date: str = "01.01.2022"):
self.__cards = []
self.name = name
self.date = date
def append(self, card: Card):
self.__cards.append(card)
return f"Карта {card.name} добавлена в колоду '{self.name}'"
def give_info(self):
info = []
for card in self.__cards:
info.append(card.give_info())
return f"\nВо мне {self.__len__()} карт, а именно: \n" + "\n".join(info) + "\n"
def total_attr(self):
result = {"TotalHP": 0, "TotalDamage": 0, "UnicNames": set()}
for card in self.__cards:
result["TotalHP"] += card.hp
result["TotalDamage"] += card.damage
result["UnicNames"].add(card.name)
return result
# Сортировка по группам
def sort(self):
self.__cards.sort(key=lambda card: card.group, reverse=False)
def sort_by(self, key: str = "name"):
keys = "name hp damage"
key = key.lower()
if key not in keys:
print("Критерий не понятен")
return
if key == "name":
self.__cards.sort(key=lambda card: card.name)
elif key == "hp":
self.__cards.sort(key=lambda card: card.hp, reverse=True)
elif key == "damage":
self.__cards.sort(key=lambda card: card.damage, reverse=True)
def __len__(self):
return len(self.__cards)
def __iter__(self):
self.current = 0
return self
def __next__(self):
if self.current < len(self.__cards):
self.current += 1
return self.__cards[self.current - 1]
else:
raise StopIteration
def __getitem__(self, index: int):
if 0 <= index <= len(self.__cards):
return self.__cards[index]
else:
raise IndexError
def __delitem__(self, index: int):
if 0 <= index <= len(self.__cards):
del self.__cards[index]
else:
raise IndexError
class Comparator:
def __init__(self):
self.result = "Сравнениея еще не было"
def compare_deck(self, deck_1: Deck, deck_2: Deck):
# Параметры
total_1 = deck_1.total_attr()
total_2 = deck_2.total_attr()
total_hp = [total_1['TotalHP'], total_2['TotalHP']]
total_damage = [total_1['TotalDamage'], total_2['TotalDamage']]
intersection = total_1['UnicNames'].intersection(total_2['UnicNames'])
intersection = " ".join(intersection)
compare = f'''
В колоде '{deck_1.name}' {len(deck_1)} карт А в колоде '{deck_2.name}' {len(deck_2)} карт
Суммарное ХП: {total_hp[0]} Суммарное ХП: {total_hp[1]}
Суммарный Урон: {total_damage[0]} Суммарный Урон: {total_damage[1]}
Одинаковых карт:
{intersection}\n'''
if total_hp[0] > total_hp[1] and total_damage[0] > total_damage[1]:
compare += f"\nОчевидно, победила колода '{deck_1.name}'"
else:
compare += f"Хрен его знает кто круче."
print(compare)
class Fighter:
def __init__(self):
self.result = "Сражение еще не было проведено!"
self.looser = None
self.winner = None
def set_result(self):
self.result = f"Представляем колоду победителя:\n {self.winner.give_info()} \n" + f"Колода проигравшего:\n Пуста! ;("
def define_winner(self, deck_0: Deck, deck_1: Deck):
self.winner = None
if len(deck_0) == 0 and len(deck_1) != 0:
print(f"Колода '{deck_1.name}' победила!")
self.looser = deck_0
self.winner = deck_1
elif len(deck_0) != 0 and len(deck_1) == 0:
print(f"Колода '{deck_0.name}' победила!")
self.looser = deck_1
self.winner = deck_0
def fight(self, deck_0: Deck, deck_1: Deck):
tag = ['name', 'damage', 'hp']
deck_0.sort_by(tag[randint(0, 2)])
deck_1.sort_by(tag[randint(0, 2)])
attacker, defender = deck_0, deck_1
if randint(0, 1):
print(f"Атаковать первой будет колода '{deck_1.name}'")
attacker, defender = deck_1, deck_0
else:
print(f"Атаковать первой будет колода '{deck_0.name}'")
while len(attacker) != 0 and len(defender) != 0:
attacker_unit = attacker[0]
defender_unit = defender[0]
res_damage = attacker_unit.attack(defender_unit)
print(
f"{attacker.name}--->'{attacker_unit.name}' атаковал '{defender_unit.name}' тычкой в {res_damage} урона. У него осталось {defender_unit.hp}ХП--->{defender.name}\n")
if defender_unit.hp > 0:
res_damage = defender_unit.attack(attacker_unit)
print(
f"{defender.name}<-------Но '{defender_unit.name}' ответил '{attacker_unit.name}' тычкой в {res_damage}. У него осталось {attacker_unit.hp}ХП<-------{attacker.name}\n")
if defender_unit.hp == 0 or attacker_unit.hp == 0:
victim = None
if defender_unit.hp == 0:
victim = defender
elif attacker_unit.hp == 0:
victim = attacker
else:
print("Ошибка")
return
print(f"Откис {victim[0].name} из колоды '{victim.name}'")
del victim[0]
if len(victim) == 0:
break
print("Новый боец идет ринг!")
print(victim.give_info())
print("------------------------------------")
print("Закончили упражнение!")
print("------------------------------------")
self.define_winner(deck_0, deck_1)
self.set_result()
print(self.result)
my_deck = Deck("Моя колода", "25.05.2022")
my_cards = [
Card("Strenght", "Рудге", 800, 60, 200),
Card("Agility", "Крип", 500, 100, 30),
Card("Strenght", "Земеля", 700, 60, 80),
Card("Intelegence", "Инвокер", 500, 100, 80),
Card("Неизвестно", "PB", 600, 100, 80)
]
for card in my_cards:
my_deck.append(card)
# print(my_deck.give_info())
enemy_deck = Deck("Колода засранца", "26.05.2022")
enemy_cards = [
Card("Strenght", "Рудге", 800, 60, 200),
Card("Intelegence", "Инвокер", 500, 100, 80),
Card("Неизвестно", "PB", 600, 100, 80)
]
for card in enemy_cards:
enemy_deck.append(card)
# print(enemy_deck.give_info())
# comparator = Comparator()
# comparator.compare_deck(my_deck, enemy_deck)
fighter = Fighter()
fighter.fight(my_deck, enemy_deck)
class Card:
def __init__(self, group : str = "not_state", name : str = None, hp : int = 0, damage : int = 0):
self.group = group
self.name = name
self.hp = hp
self.damage = damage
def give_info(self):
return f"Группа {self.group} | Имя: {self.name} | Здоровье: {self.hp} | Урон: {self.damage}"
class Deck:
__cards = []
def __init__(self, name : str = "Колода", date : str = "01.01.2022"):
self.name = name
self.date = date
def add_card(self, card : Card):
self.__cards.append(card)
def get_amount(self):
return len(self.__cards)
def give_info(self):
info = []
for card in self.__cards:
info.append( card.give_info() )
return f"Во мне {self.get_amount()} карт, а именно: \n" + "\n".join(info)
my_deck = Deck("Моя колода", "25.05.2022")
my_deck.add_card( Card("Intelegence", "Течис", 600, 32) )
my_deck.add_card( Card("Strenght", "Рудге", 800, 60) )
print( my_deck.give_info() )