我已经实现了康威的生命游戏:
def neighbors(point):
x, y = point
for i, j in itertools.product(range(-1, 2), repeat=2):
if any((i, j)):
yield (x + i, y + j)
def advance(board):
newstate = set()
recalc = board | set(itertools.chain(*map(neighbors, board)))
for point in recalc:
count = sum((neigh in board)
for neigh in neighbors(point))
if count == 3 or (count == 2 and point in board):
newstate.add(point)
return newstate
Run Code Online (Sandbox Code Playgroud)
我想可视化结果,所以我尝试修改Matplotlib 动画示例中给定的示例:
glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)])
fig, …Run Code Online (Sandbox Code Playgroud)