#!/usr/bin/python # By Tom Wright # Use this code however you like... """ Numpy game of life.""" import numpy def iterate(board): # find number of neighbours that each square has neighbour_count = numpy.zeros(board.shape) neighbour_count[1:, 1:] += board[:-1, :-1] neighbour_count[1:, :-1] += board[:-1, 1:] neighbour_count[:-1, 1:] += board[1:, :-1] neighbour_count[:-1, :-1] += board[1:, 1:] neighbour_count[:-1, :] += board[1:, :] neighbour_count[1:, :] += board[:-1, :] neighbour_count[:, :-1] += board[:, 1:] neighbour_count[:, 1:] += board[:, :-1] # a live cell is killed if it has fewer # than 2 or more than 3 neighbours. part1 = ((board == 1) & (neighbour_count < 4) & (neighbour_count > 1)) # a new cell forms if a square has exactly three members part2 = ((board == 0) & (neighbour_count == 3)) # cast booleans to integers new_board = numpy.cast[numpy.int](part1 | part2) return new_board board = numpy.array([ [0, 1, 0, 0, 0], [0, 0, 1, 1, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 0, 0]]) for i in range(100): print board board = iterate(board)