-
-
Save dedan/1426734 to your computer and use it in GitHub Desktop.
session2
This file contains 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
class Life(object): | |
"""docstring for Life""" | |
def __init__(self, init=None): | |
super(Life, self).__init__() | |
self.cells = init | |
def count_neighbors(self): | |
"""docstring for count_neighbors""" | |
neighbor_counter = {} | |
neighbors = [(-1, -1), (0, -1), (1, -1), | |
(-1, 0), (1, 0), | |
(-1, 1), (0, 1), (1, 1)] | |
for x, y in self.cells: | |
for m, n in neighbors: | |
tmp_coordinate = (x+m, y+n) | |
if not tmp_coordinate in neighbor_counter: | |
neighbor_counter[tmp_coordinate] = 0 | |
neighbor_counter[tmp_coordinate] += 1 | |
return neighbor_counter | |
def generation_step(self): | |
"""docstring for next_generation""" | |
new_cells = set() | |
neighbor_counter = self.count_neighbors() | |
for neighbor_coordinate, neighbor_number in neighbor_counter.items(): | |
# first conway rule (become alive) | |
if not neighbor_coordinate in self.cells and (neighbor_number == 3): | |
new_cells.add(neighbor_coordinate) | |
# second conway rule (staying alive) | |
if neighbor_coordinate in self.cells and neighbor_number in [2, 3]: | |
new_cells.add(neighbor_coordinate) | |
# second conway rule (staying alive) | |
if neighbor_coordinate in self.cells and neighbor_number in [2, 3]: | |
new_cells.add(neighbor_coordinate) | |
self.cells = new_cells |
This file contains 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 unittest | |
from conway import Life | |
class TestConway(unittest.TestCase): | |
def setUp(self): | |
self.life = Life(init=set([(1, 0), (1, 1), (1, 2)])) | |
def test_amount_of_living_should_be_constant(self): | |
"""docstring for amount_of_living_should_be_constant""" | |
self.life.generation_step() | |
self.assertEqual(len(self.life.cells), 3) | |
def test_update_correct(self): | |
"""docstring for test_update_correct""" | |
self.life.generation_step() | |
self.assert_((1, 0) not in self.life.cells) | |
def test_count_neighbors(self): | |
"""docstring for test_count_neighbors""" | |
neighbour_count = self.life.count_neighbors() | |
self.assertEqual(neighbour_count[(0, 0)], 2) | |
self.assertEqual(neighbour_count[(0, 1)], 3) | |
def test_cell_becomes_alive(self): | |
"""docstring for test_cells_becomes_alive""" | |
self.life.generation_step() | |
self.assert_((0, 1) in self.life.cells) | |
def test_cell_stays_alive(self): | |
"""docstring for test_cell_stays_alive""" | |
self.life.generation_step() | |
self.assert_((1, 1) in self.life.cells) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment