def test_get_adjacent_spaces() -> None: data = { Location(0, 0): Space(is_populated=True), Location(1, 1): Space(is_populated=True), } board = Board.create((10, 10), data=data) expected_adjacent_spaces = [ Space(), # 1,0 Space(), # 0,1 Space(is_populated=True), # 1,1 ] assert board.get_adjacent_spaces(Location(0, 0)) == expected_adjacent_spaces data = { Location(5, 5): Space(is_populated=True), Location(4, 4): Space(is_populated=True), Location(6, 6): Space(is_populated=True), } board = Board.create((10, 10), data=data) expected_adjacent_spaces = [ Space(is_populated=True), # 5,5 Space(), Space(), Space(), Space(), Space(), Space(), Space(), ] assert board.get_adjacent_spaces(Location(6, 6)) == expected_adjacent_spaces
def test_create() -> None: data = { Location(0, 0): Space(is_populated=True), Location(1, 2): Space(is_populated=True), } board = Board.create((3, 3), data=data) expected_data = { Location(0, 0): Space(is_populated=True), Location(0, 1): Space(), Location(0, 2): Space(), Location(1, 0): Space(), Location(1, 1): Space(), Location(1, 2): Space(is_populated=True), Location(2, 0): Space(), Location(2, 1): Space(), Location(2, 2): Space(), } assert board.data == expected_data board = Board.create((2, 2), data=data) expected_data = { Location(0, 0): Space(is_populated=True), Location(0, 1): Space(), Location(1, 0): Space(), Location(1, 1): Space(), } assert board.data == expected_data
def test_get_num_adjacent_populated_spaces( m_get_adjacent_spaces: Mock) -> None: m_get_adjacent_spaces.return_value = [ Space(is_populated=True), Space(), Space(), Space(is_populated=True), ] board = Board.create((45, 45)) assert board.get_num_adjacent_populated_spaces(Location(17, 42)) == 2
class GameOfLife(Thread): def __init__(self, board_size, window): self.board = Board(board_size) self.window = window self.alive = True super(GameOfLife, self).__init__() def run(self): sleep(1) while self.alive: for x in range(self.board.size): for y in range(self.board.size): if self.board.is_alive(x, y): self.window.draw_alive(x, y) else: self.window.draw_dead(x, y) self.board.next_step() sleep(1)
def load_board(filename): img = Image.open(filename) img.load() columns, lines = img.size board_state = numpy.zeros(shape=(lines, columns), dtype=int) for i in range(lines): for j in range(columns): board_state[i][j] = img.getpixel((j, i)) == (0, 0, 0) return Board(initial_state=board_state)
def setup_board_with_cell(cell, neighbour_count): neighbours = list(cell.neighbours()) random.shuffle(neighbours) board = Board([cell] + neighbours[:neighbour_count]) return board.next_generation()
def test_a_cell_with_three_neighbours_spawns(x, y): cell = Cell(x, y) neighbours = list(cell.neighbours()) board = Board(neighbours[:3]) board = board.next_generation() assert cell in board.cells
def test_evolve() -> None: glider = { Location(2, 1): Space(True), Location(3, 2): Space(True), Location(1, 3): Space(True), Location(2, 3): Space(True), Location(3, 3): Space(True), } board = Board.create((5, 5), data=glider) # board.render() assert board.data == { Location(x=0, y=0): Space(), Location(x=0, y=1): Space(), Location(x=0, y=2): Space(), Location(x=0, y=3): Space(), Location(x=0, y=4): Space(), Location(x=1, y=0): Space(), Location(x=1, y=1): Space(), Location(x=1, y=2): Space(), Location(x=1, y=3): Space(is_populated=True), Location(x=1, y=4): Space(), Location(x=2, y=0): Space(), Location(x=2, y=1): Space(is_populated=True), Location(x=2, y=2): Space(), Location(x=2, y=3): Space(is_populated=True), Location(x=2, y=4): Space(), Location(x=3, y=0): Space(), Location(x=3, y=1): Space(), Location(x=3, y=2): Space(is_populated=True), Location(x=3, y=3): Space(is_populated=True), Location(x=3, y=4): Space(), Location(x=4, y=0): Space(), Location(x=4, y=1): Space(), Location(x=4, y=2): Space(), Location(x=4, y=3): Space(), Location(x=4, y=4): Space(), } data = board.evolve() board = Board.create((5, 5), data=data) # board.render() assert board.data == { Location(x=0, y=0): Space(), Location(x=0, y=1): Space(), Location(x=0, y=2): Space(), Location(x=0, y=3): Space(), Location(x=0, y=4): Space(), Location(x=1, y=0): Space(), Location(x=1, y=1): Space(), Location(x=1, y=2): Space(is_populated=True), Location(x=1, y=3): Space(), Location(x=1, y=4): Space(), Location(x=2, y=0): Space(), Location(x=2, y=1): Space(), Location(x=2, y=2): Space(), Location(x=2, y=3): Space(is_populated=True), Location(x=2, y=4): Space(is_populated=True), Location(x=3, y=0): Space(), Location(x=3, y=1): Space(), Location(x=3, y=2): Space(is_populated=True), Location(x=3, y=3): Space(is_populated=True), Location(x=3, y=4): Space(), Location(x=4, y=0): Space(), Location(x=4, y=1): Space(), Location(x=4, y=2): Space(), Location(x=4, y=3): Space(), Location(x=4, y=4): Space(), } data = board.evolve() board = Board.create((5, 5), data=data) # board.render() assert board.data == { Location(x=0, y=0): Space(), Location(x=0, y=1): Space(), Location(x=0, y=2): Space(), Location(x=0, y=3): Space(), Location(x=0, y=4): Space(), Location(x=1, y=0): Space(), Location(x=1, y=1): Space(), Location(x=1, y=2): Space(), Location(x=1, y=3): Space(is_populated=True), Location(x=1, y=4): Space(), Location(x=2, y=0): Space(), Location(x=2, y=1): Space(), Location(x=2, y=2): Space(), Location(x=2, y=3): Space(), Location(x=2, y=4): Space(is_populated=True), Location(x=3, y=0): Space(), Location(x=3, y=1): Space(), Location(x=3, y=2): Space(is_populated=True), Location(x=3, y=3): Space(is_populated=True), Location(x=3, y=4): Space(is_populated=True), Location(x=4, y=0): Space(), Location(x=4, y=1): Space(), Location(x=4, y=2): Space(), Location(x=4, y=3): Space(), Location(x=4, y=4): Space(), }
def setUp(self): self.board = Board(3)
class gol_test(unittest.TestCase): def setUp(self): self.board = Board(3) def _scenario(self, coordinates, alive_neighbors, alive_at_the_beginning, alive_at_the_end): if alive_at_the_beginning: self.board.set_alive(*coordinates) else: self.board.set_dead(*coordinates) for neighbor in alive_neighbors: self.board.set_alive(*neighbor) self.board.next_step() if alive_at_the_end: self.assertTrue(self.board.is_alive(*coordinates), str(self.board)) else: self.assertFalse(self.board.is_alive(*coordinates), str(self.board)) def revive_scenario(self, coordinates, alive_neighbors): self._scenario(coordinates, alive_neighbors, alive_at_the_beginning=False, alive_at_the_end=True) def dying_scenario(self, coordinates, alive_neighbors): self._scenario(coordinates, alive_neighbors, alive_at_the_beginning=True, alive_at_the_end=False) def stays_dead_scenario(self, coordinates, alive_neighbors): self._scenario(coordinates, alive_neighbors, alive_at_the_beginning=False, alive_at_the_end=False) def stays_alive_scenario(self, coordinates, alive_neighbors): self._scenario(coordinates, alive_neighbors, alive_at_the_beginning=True, alive_at_the_end=True) def test_left_upper_corner_will_revive_when_all_surrounding_cells_are_alive( self): cell_coordinates = 0, 0 alive_neighbors = [(0, 1), (1, 1), (1, 0)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_left_upper_corner_will_survive_when_only_two_cells_are_alive( self): cell_coordinates = 0, 0 alive_neighbors = [(0, 1), (1, 1)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_left_upper_corner_will_survive_when_all_surrounding_cells_are_alive( self): cell_coordinates = 0, 0 alive_neighbors = [(0, 1), (1, 1), (1, 0)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_left_upper_corner_will_die_when_only_one_neighbor_is_alive(self): cell_coordinates = 0, 0 alive_neighbors = [(0, 1)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_left_upper_corner_will_not_revive_if_only_one_neighbor_is_alive( self): cell_coordinates = 0, 0 alive_neighbors = [(0, 1)] self.stays_dead_scenario(cell_coordinates, alive_neighbors) def test_right_upper_corner_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 0, 2 alive_neighbors = [(0, 1), (1, 1), (1, 2)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_right_upper_corner_will_die_if_one_neighbor_is_alive(self): cell_coordinates = 0, 2 alive_neighbors = [(0, 1)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_right_upper_corner_will_stay_alive_if_two_neighbors_are_alive( self): cell_coordinates = 0, 2 alive_neighbors = [(0, 1), (1, 2)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_left_lower_corner_will_stay_alive_if_two_neighbors_are_alive( self): cell_coordinates = 2, 0 alive_neighbors = [(1, 0), (1, 1)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_left_lower_corner_will_die_if_one_neighbor_is_alive(self): cell_coordinates = 2, 0 alive_neighbors = [(2, 1)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_left_lower_corner_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 2, 0 alive_neighbors = [(2, 1), (1, 1), (1, 0)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_right_lower_corner_will_stay_alive_if_two_neighbors_are_alive( self): cell_coordinates = 2, 2 alive_neighbors = [(2, 1), (1, 1)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_right_lower_corner_will_die_if_one_neighbor_is_alive(self): cell_coordinates = 2, 2 alive_neighbors = [(2, 1)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_right_lower_corner_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 2, 2 alive_neighbors = [(2, 1), (1, 1), (1, 2)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_upper_edge_will_die_if_all_neighbors_are_dead(self): cell_coordinates = 0, 1 alive_neighbors = [] self.dying_scenario(cell_coordinates, alive_neighbors) def test_upper_edge_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 0, 1 alive_neighbors = [(0, 0), (1, 1), (0, 1)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_lower_edge_will_die_if_one_neighbor_is_alive(self): cell_coordinates = 2, 1 alive_neighbors = [(2, 0)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_lower_edge_will_survive_if_two_neighbors_are_alive(self): cell_coordinates = 2, 1 alive_neighbors = [(2, 0), (1, 1)] self.stays_alive_scenario(cell_coordinates, alive_neighbors) def test_lower_edge_will_die_if_all_neighbors_are_alive(self): cell_coordinates = 2, 1 alive_neighbors = [(2, 0), (1, 0), (1, 1), (1, 2), (2, 2)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_lower_edge_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 2, 1 alive_neighbors = [(1, 1), (1, 2), (2, 2)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_cell_will_die_if_no_neighbors_are_alive(self): cell_coordinates = 1, 1 alive_neighbors = [] self.dying_scenario(cell_coordinates, alive_neighbors) def test_cell_will_die_if_all_neighbors_are_alive(self): cell_coordinates = 1, 1 alive_neighbors = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_cell_will_die_if_one_neighbor_is_alive(self): cell_coordinates = 1, 1 alive_neighbors = [(0, 2)] self.dying_scenario(cell_coordinates, alive_neighbors) def test_cell_will_revive_if_three_neighbors_are_alive(self): cell_coordinates = 1, 1 alive_neighbors = [(0, 0), (0, 1), (0, 2)] self.revive_scenario(cell_coordinates, alive_neighbors) def test_cell_will_survive_if_two_neighbors_are_alive(self): cell_coordinates = 1, 1 alive_neighbors = [(2, 0), (2, 2)] self.stays_alive_scenario(cell_coordinates, alive_neighbors)
def __init__(self, board_size, window): self.board = Board(board_size) self.window = window self.alive = True super(GameOfLife, self).__init__()