Skip to content

Commit

Permalink
Fix life-codon to move out the neighbors struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed Jan 12, 2023
1 parent 6364de6 commit c49022d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions python/conway_game_of_life/life-codon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ class Automata:
survives: tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool]
data: list[bool]

neighbors = (
Point(-1, -1),
Point(0, -1),
Point(1, -1),
Point(-1, 0),
Point(1, 0),
Point(-1, 1),
Point(0, 1),
Point(1, 1),
)


def __init__(self, width, height, born, survives):
self.width = width
self.height = height
Expand All @@ -39,18 +51,7 @@ def set(self, p: Point):
self.data[self.index(p)] = True

def count_neighbors(self, p: Point):
neighbors = (
Point(-1, -1),
Point(0, -1),
Point(1, -1),
Point(-1, 0),
Point(1, 0),
Point(-1, 1),
Point(0, 1),
Point(1, 1),
)

return sum(1 for loc in neighbors if self.get(loc + p))
return sum(1 for loc in self.neighbors if self.get(loc + p))

def next(self):
result = Automata(self.width, self.height, self.born, self.survives)
Expand Down Expand Up @@ -85,7 +86,7 @@ def add_glider(self, p: Point):

obj.add_glider(Point(0, 18))

for i in range(0, 1000):
for i in range(0, 10000):
obj = obj.next()

for y in range(0, obj.height):
Expand Down

0 comments on commit c49022d

Please sign in to comment.