Created
October 15, 2012 20:57
-
-
Save kynan/3895422 to your computer and use it in GitHub Desktop.
Game of Life with rule 24 in Python
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
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) |
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
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