forked from cooklee/simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreature.py
More file actions
51 lines (36 loc) · 1.1 KB
/
creature.py
File metadata and controls
51 lines (36 loc) · 1.1 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
__author__ = 'cooklee'
def get_method_form_class(classname, start_with='action'):
names_of_functions = dir(classname)
interested_function = []
for name in names_of_functions:
if name.startswith(start_with):
interested_function.append(name)
return interested_function
class Mind(object):
def __init__(self, objects):
self.options = [getattr(objects, x) for x in get_method_form_class(objects)]
def decide(self):
import random
gen = random.randrange(len(self.options))
self.options[gen]()
class Creature(object):
def __init__(self, health=100.0):
self.health = health
self.live = 1
self.mind = Mind(self)
def check_status(self):
if self.health <= 0:
self.live = 0
def action_move(self):
print 'move'
self.health -= 0.5
self.check_status()
def action_eat(self, portion=0.1):
print 'eat'
self.health += portion
def action_nothing(self):
print 'nothing'
def think(self):
self.mind.decide()
c = Creature()
c.think()