-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_AnimalHouse.py
More file actions
70 lines (57 loc) · 1.55 KB
/
Copy pathdemo_AnimalHouse.py
File metadata and controls
70 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
# The Random Zookeep
# Code walk-thru @ https://www.youtube.com/watch?v=Nmg9sYGILb0
#
import enum
class Food(enum.Enum):
BIRD_FOOD = 1
CAT_FOOD = 2
import abc
class AbsAnimal(abc.ABC):
def __init__(self, name='wild'):
self.name = name
self.health = 0
@abc.abstractmethod
def do_eat(self, food):
pass
class Bird(AbsAnimal):
def __init__(self):
super().__init__("Bird")
def do_eat(self, food):
if isinstance(food, Food):
if food is Food.BIRD_FOOD:
self.health += 5
return True
return False
class Cat(AbsAnimal):
def __init__(self):
super().__init__("Cat")
def do_eat(self, food):
if isinstance(food, Food):
if food is Food.CAT_FOOD:
self.health += 5
return True
return False
import random
zoo = [Bird() if random.randrange(2)
else Cat() for _ in range(10)]
print(*zoo, sep='\n')
if not issubclass(Cat, AbsAnimal):
raise Exception("Crazy Cat")
if not issubclass(Bird, AbsAnimal):
raise Exception("Crazy Bird")
if issubclass(Bird, Cat):
raise Exception("Unloved Bird")
if issubclass(Cat, Bird):
raise Exception("Unloved Cat")
done = False
while not done:
for pet in zoo:
if random.randrange(1,3) == 1:
pet.do_eat(Food.BIRD_FOOD)
else:
pet.do_eat(Food.CAT_FOOD)
if pet.health > 20:
print(pet.name, "wins!")
done = True
break