-
Notifications
You must be signed in to change notification settings - Fork 2
/
conway.py
42 lines (34 loc) · 1.44 KB
/
conway.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
#!/usr/bin/env python
"""
Conway's game of life, final version.
Rules:
* Any cell with fewer than two neighbors dies (underpopulation)
* Any cell with more than three neighbors dies (overpopulation)
* Any empty spot with three neighbors becomes a live cell (reproduction)
Check out the wikipedia article for more information, including interesting
starting populations.
"""
def run(population, generations=100):
"""Runs Conway's game of life on an initial population."""
population = set(population)
for i in range(generations):
population = evolve(population)
return list(population)
def evolve(population):
"""Evolves the population by one generation."""
# Get a unique set of discrete cells that need to be checked
active_cells = population | set([neighbor for p in population
for neighbor in neighbors(p)])
# For each cell in the set, test if it lives or dies
new_population = set()
for cell in active_cells:
count = sum([neighbor in population for neighbor in neighbors(cell)])
if count == 3 or (count == 2 and cell in population):
new_population.add(cell)
# Return the new surviving population
return new_population
def neighbors(cell):
"""Returns the neighbors of a given cell."""
x, y = cell
return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1), (x + 1, y + 1),
(x + 1, y - 1), (x - 1, y + 1), (x - 1, y - 1)]