Created
February 12, 2013 04:35
-
-
Save tombatron/4760274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy | |
class Cell(object): | |
x = None | |
y = None | |
is_alive = None | |
_neighbors = None | |
def __init__(self, x, y, world, initially_alive=False): | |
self.x = x | |
self.y = y | |
self._world = world | |
self._world.cells[x, y] = self | |
self.is_alive = initially_alive | |
def _find_neighbors(self): | |
x = self.x | |
y = self.y | |
x_plus_one = x + 1 if x + 1 < self._world.max_x else 0 | |
x_minus_one = x - 1 if x - 1 >= 0 else self._world.max_x - 1 | |
y_plus_one = y + 1 if y + 1 < self._world.max_y else 0 | |
y_minus_one = y - 1 if y - 1 >= 0 else self._world.max_y - 1 | |
yield self._world.cells[x, y_plus_one] | |
yield self._world.cells[x, y_minus_one] | |
yield self._world.cells[x_plus_one, y] | |
yield self._world.cells[x_minus_one, y] | |
yield self._world.cells[x_plus_one, y_minus_one] | |
yield self._world.cells[x_minus_one, y_plus_one] | |
yield self._world.cells[x_plus_one, y_plus_one] | |
yield self._world.cells[x_minus_one, y_minus_one] | |
def get_neighbors(self): | |
if not self._neighbors: | |
self._neighbors = list(self._find_neighbors()) | |
return self._neighbors | |
_next = None | |
def next(self): | |
if self._next is None: | |
self._next = 0 # Catch all... | |
live_neighbors = sum(1 for n in self.get_neighbors() if n.is_alive) | |
if self.is_alive: | |
if 2 > live_neighbors < 3: | |
self._next = 0 | |
if 1 < live_neighbors < 4: | |
self._next = 1 | |
else: | |
if live_neighbors == 3: | |
self._next = 1 | |
return self._next | |
def step(self): | |
self.is_alive = self.next | |
self._next = None | |
self.changed = None | |
class World(object): | |
max_x = None | |
max_y = None | |
cells = None | |
def __init__(self, size, auto_gen_cells=False): | |
self.max_x = size[0] | |
self.max_y = size[1] | |
self.cells = numpy.empty((size[0], size[1],), dtype=Cell) | |
if auto_gen_cells: | |
it = numpy.nditer(self.cells, flags=['multi_index', 'refs_ok']) | |
while not it.finished: | |
(x, y) = it.multi_index | |
Cell(x, y, self, initially_alive=(random.randint(0,10000) % 3 == 0)) | |
it.iternext() | |
def next(self): | |
it = numpy.nditer(self.cells, flags=['multi_index', 'refs_ok']) | |
while not it.finished: | |
it[0].step() | |
it.iternext() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment