Skip to content

Instantly share code, notes, and snippets.

@kynan
Created October 15, 2012 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kynan/3895422 to your computer and use it in GitHub Desktop.
Save kynan/3895422 to your computer and use it in GitHub Desktop.
Game of Life with rule 24 in Python
from copy import copy
class GameOfLife:
RULES = {
(1,1,1): 0,
(1,1,0): 0,
(1,0,1): 0,
(1,0,0): 1,
(0,1,1): 1,
(0,1,0): 0,
(0,0,1): 0,
(0,0,0): 0
}
def __init__(self, state):
self.state = state
self.n = len(state)
def __str__(self):
return str(self.state)
def __eq__(self, other):
return self.state == other.state
def step(self):
state = copy(self.state)
for i in range(0, self.n - 2):
sublist = self.state[i: i+3]
state[i+1] = self.RULES[tuple(sublist)]
return GameOfLife(state)
from gol import GameOfLife
def test_single_cell():
g = GameOfLife([0])
assert g.step() == g
def test_state_len():
g = GameOfLife([0])
assert g.n == 1
def test_three_cells():
g = GameOfLife([1, 1, 1])
assert g.step() == GameOfLife([1, 0, 1])
def test_five_cells():
g = GameOfLife([1,1,0,0])
assert g.step() == GameOfLife([1, 0, 1, 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment