-
Notifications
You must be signed in to change notification settings - Fork 0
/
starfish.py
89 lines (73 loc) · 2.99 KB
/
starfish.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import random
from organism import Organism
class Starfish(Organism):
def __init__(self, ecosystem, location = None, isNewborn = False):
Organism.__init__(self, ecosystem, location)
self.lifespanTicks = 35 * 365 * 24 * 60 # years * days * hours * mins
self.maturityTicks = 5 * 365 * 24 * 60
self.hunger = 50
self.starvationLevel = 100
self.survivalProbability = .95 # they can regenerate, after all
self.movementImpact = .1 # slowly crawl along the ocean floor
self.__initializeAgeAndMaturity(isNewborn)
self.__initializeSex()
def performStandardAction(self):
if self.ticksAlive >= self.lifespanTicks:
self.die('old age!') # die of old age
self.__updateMaturityLevel()
if self.isMature == False and self.hunger <= 2:
# some immature starfish reproduce asexually when food is plentiful
self.reproduce()
else:
self.__lookForPreyAndMates()
self.__handleAndUpdateHunger()
self.__chooseDirectionAndMove()
self.ticksAlive += 1
def reproduce(self):
baby = Starfish(self.ecosystem, self.location, True)
self.ecosystem.reportBirth(baby)
def printStatus(self):
return
def __initializeAgeAndMaturity(self, isNewborn):
if isNewborn:
self.ticksAlive = 0
else:
self.ticksAlive = random.randint(0, self.lifespanTicks - 1)
self.isMature = self.ticksAlive >= self.maturityTicks
def __initializeSex(self):
if self.ticksAlive >= self.maturityTicks:
if random.randint(0, 1) == 0:
self.sex = "M"
else:
self.sex = "F"
else:
self.sex = "X"
def __chooseDirectionAndMove(self):
if self.ticksAlive % 3 == 0:
# every few ticks, choose a random direction (if we choose a random
# direction every tick, we pretty much end up going nowhere)
self.randomDirection()
self.move()
def __handleAndUpdateHunger(self):
if self.hunger > self.starvationLevel:
self.die('starvation!')
self.hunger += 1
def __lookForPreyAndMates(self):
neighborOrgs = self.ecosystem.getNeighbors(self)
for org in neighborOrgs:
if self.hunger > 0 and self.ecosystem.isEdible(self, org):
food = org.beEaten()
self.hunger -= (food / 15000)
break
elif type(org) == type(self): # found a fellow starfish!
if self.isMature and org.isMature:
if self.sex == "F" and org.sex == "M":
self.reproduce()
break
def __updateMaturityLevel(self):
if self.isMature == False and self.ticksAlive >= self.maturityTicks:
self.isMature = True
if random.randint(0, 1) == 0:
self.sex = "M"
else:
self.sex = "F"