-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.py
68 lines (55 loc) · 1.71 KB
/
life.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
from enum import Enum
class LifeformState(Enum):
dead = 0,
alive = 1,
dying = 2,
resurrecting = 3
class Lifeform():
def __init__(self, c1, c2, state=False):
'''
Create a lifeform
:param state: True stands for alive, False stands for dead.
'''
self._age = 0
self._state = LifeformState.alive if state else LifeformState.dead
self._neighbors = []
self._c1 = c1
self._c2 = c2
def alive(self):
'''
Is this cell alive?
'''
return True if self._state in \
[LifeformState.alive, LifeformState.dying] else False
def specifyNeighbors(self, neighbors):
'''
Tell the lifeform whom its neighbor is.
:param neighbor: another lifeform
'''
self._neighbors = neighbors
def aliveNeighbors(self):
'''
Count how many naighbors are alive
:return: numeric
'''
return sum(neighbor.alive() for neighbor in self._neighbors)
def play(self):
'''
This is where the Game of Life rules get implemented
'''
nc = self.aliveNeighbors()
if self.alive():
self._age += 1
if (nc < self._c1 or nc > self._c2) and self.alive():
self._state = LifeformState.dying
if nc == self._c2 and not self.alive():
self._state = LifeformState.resurrecting
def updateState(self):
if self._state == LifeformState.dying:
self.kill()
if self._state == LifeformState.resurrecting:
self.resurrect()
def kill(self):
self._state = LifeformState.dead
def resurrect(self):
self._state = LifeformState.alive