def test_count_neighbors(rules, world, world_single): gl = GameOfLife(rules, world_single) neighbors = gl.get_neighbors((1, 1)) count = gl.count_neighbors(neighbors) assert count == 3 gl.world = world neighbors = gl.get_neighbors((0, 0)) count = gl.count_neighbors(neighbors) assert count == 2
def test_get_neighbors_corner(self): game = GameOfLife(xsize=3, ysize=3) neighbors = game.get_neighbors(0, 0) self.assertEqual(len(neighbors), 3) self.assertIn((0, 1), neighbors) self.assertIn((1, 0), neighbors) self.assertIn((1, 1), neighbors)
def birth(gl: GameOfLife, value: int, index: Tuple[int, int]): neighbors = gl.get_neighbors(index) count = gl.count_neighbors(neighbors) if count == 3: return 1 return value
def test_get_neighbors_with_focal(self): game = GameOfLife(xsize=3, ysize=3) game.set_include_focal() neighbors = game.get_neighbors(1, 0) self.assertEqual(len(neighbors), 6) self.assertIn((0, 0), neighbors) self.assertIn((0, 1), neighbors) self.assertIn((1, 0), neighbors) self.assertIn((1, 1), neighbors) self.assertIn((2, 0), neighbors) self.assertIn((2, 1), neighbors)
def test_get_neighbors(self): game = GameOfLife(xsize=3, ysize=3) neighbors = game.get_neighbors(1, 1) self.assertEqual(len(neighbors), 8) self.assertIn((0, 0), neighbors) self.assertIn((0, 1), neighbors) self.assertIn((0, 2), neighbors) self.assertIn((1, 0), neighbors) self.assertIn((1, 2), neighbors) self.assertIn((2, 0), neighbors) self.assertIn((2, 1), neighbors) self.assertIn((2, 2), neighbors)
def underpopulation(gl: GameOfLife, value: int, index: Tuple[int, int]): neighbors = gl.get_neighbors(index) count = gl.count_neighbors(neighbors) if count <= 2: return 0 return value
def overpopulation(gl: GameOfLife, value: int, index: Tuple[int, int]): neighbors = gl.get_neighbors(index) count = gl.count_neighbors(neighbors) if count > 3: return 0 if value <= 0 else value - 1 return value
def test_get_neighbors(rules, world, world_single): gl = GameOfLife(rules, world_single) neighbors = gl.get_neighbors((1, 1)) assert world_single.all() == np.array(neighbors).all() assert world_single.shape == (3, 3)