Skip to content

Commit

Permalink
Better Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
prochigor committed Oct 3, 2023
1 parent eb1c68b commit f52e157
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
class Animal:
alive = []

def __init__(self, name: str, health: int = 100) -> None:
def __init__(
self, name: str,
health: int = 100,
hidden: bool = False
) -> None:
self.name = name
self.health = health
self.hidden = False
self.alive.append(self)
self.hidden = hidden
Animal.alive.append(self)

def __repr__(self) -> str:
return (
f"{{Name: {self.name}, Health: "
f"{self.health}, Hidden: {self.hidden}}}"
)

def remove_dead_animal(self) -> None:
if self.health <= 0:
Animal.alive.remove(self)


class Herbivore(Animal):

def hide(self) -> None:
if not self.hidden:
self.hidden = True
else:
self.hidden = False
self.hidden = not self.hidden


class Carnivore(Animal):
Expand All @@ -29,5 +34,4 @@ class Carnivore(Animal):
def bite(herbivore: Herbivore) -> None:
if isinstance(herbivore, Herbivore) and not herbivore.hidden:
herbivore.health -= 50
if herbivore.health <= 0:
Animal.alive.remove(herbivore)
herbivore.remove_dead_animal()

0 comments on commit f52e157

Please sign in to comment.