Last active
December 15, 2015 08:39
-
-
Save attilammagyar/5232758 to your computer and use it in GitHub Desktop.
A simple Conway's Game of Life implementation in Python.
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
class GameOfLife(object): | |
def evolve(self, cells): | |
next_generation = set() | |
for cell in cells: | |
if self.__should_survive(cell, cells): | |
next_generation.add(cell) | |
for neighbor in self.__get_neighbors(cell): | |
if self.__should_become_alive(neighbor, cells): | |
next_generation.add(neighbor) | |
return next_generation | |
def __should_survive(self, cell, cells): | |
number_of_neighbors = self.__count_living_neighbors(cell, cells) | |
return number_of_neighbors == 2 or number_of_neighbors == 3 | |
def __should_become_alive(self, cell, cells): | |
return (cell not in cells | |
and self.__count_living_neighbors(cell, cells) == 3) | |
def __count_living_neighbors(self, cell, cells): | |
number_of_neighbors = 0 | |
for neighbor in self.__get_neighbors(cell): | |
if neighbor in cells: | |
number_of_neighbors += 1 | |
return number_of_neighbors | |
def __get_neighbors(self, cell): | |
neighbors = [] | |
(x, y) = cell | |
for (dx, dy) in self.__get_neighbor_offsets(): | |
neighbors.append((x + dx, y + dy)) | |
return neighbors | |
def __get_neighbor_offsets(self): | |
return [ (-1, -1), (0, -1), (1, -1), | |
(-1, 0), (1, 0), | |
(-1, 1), (0, 1), (1, 1) ] |
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 GameOfLife import GameOfLife | |
class GameOfLifeTest(unittest.TestCase): | |
def setUp(self): | |
self.game = GameOfLife() | |
def test_any_live_cell_with_less_than_two_neighbors_dies(self): | |
self.evolve([ (0, 0) ]) | |
self.assert_dead((0, 0)) | |
self.evolve([ (0, 0), (1, 0) ]) | |
self.assert_dead((1, 0)) | |
def test_any_live_cell_with_two_or_three_neighbors_survives(self): | |
self.evolve([ (1, 1), (2, 1), | |
(0, 2), (1, 2) ]) | |
self.assert_alive((0, 2)) | |
self.assert_alive((1, 2)) | |
def test_any_live_cell_with_more_than_three_neighbors_dies(self): | |
self.evolve([ (0, 1), (1, 1), (2, 1), | |
(0, 2), (1, 2) ]) | |
self.assert_dead((1, 1)) | |
self.assert_dead((1, 2)) | |
def test_any_dead_cell_with_three_neighbors_becomes_alive(self): | |
self.evolve([ (1, 1), | |
(0, 2), | |
(2, 3) ]) | |
self.assert_alive((1, 2)) | |
def evolve(self, cells): | |
self.next_generation = self.game.evolve(cells) | |
def assert_alive(self, cell): | |
self.assertTrue(cell in self.next_generation) | |
def assert_dead(self, cell): | |
self.assertFalse(cell in self.next_generation) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment