def main(): # Screen stuff resolution = 20 S_WIDTH, S_HEIGHT = 600, 600 # Board state stuff width, height = round(S_WIDTH / resolution), round(S_HEIGHT / resolution) weight = 0.7 screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT)) board_state = life.random_state(width, height, weight) while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() # pygame.draw.rect(screen, pygame.Color( # 255, 255, 255), (0, 0, S_WIDTH, S_HEIGHT)) life.render(board_state, screen, resolution) pygame.display.flip() board_state = life.next_board_state(board_state) time.sleep(.1)
def test_next_underpopulation(self): initial_state = [ [1, 0, 0], [0, 1, 0], [0, 0, 0], ] expected_state = dead_state(3, 3) actual_state = next_board_state(initial_state) self.assertEqual(expected_state, actual_state)
def test_next_reproduction(self): initial_state = [ [1, 1, 1], [0, 0, 0], [0, 0, 0], ] expected_state = [ [0, 1, 0], [0, 1, 0], [0, 0, 0], ] actual_state = next_board_state(initial_state) self.assertEqual(expected_state, actual_state)
def test_next_alive(self): initial_state = [ [1, 0, 0], [1, 1, 0], [0, 0, 0], ] expected_state = [ [1, 1, 0], [1, 1, 0], [0, 0, 0], ] actual_state = next_board_state(initial_state) self.assertEqual(expected_state, actual_state)
import life if __name__ == '__main__': # TEST 1: dead cells should stay dead with no live neighbors initial_state_1 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] expected_state_1 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] actual_state_1 = life.next_board_state(initial_state_1) assert actual_state_1 == expected_state_1, 'Test 1 failed' # TEST 2: dead cells should become alive with 3 live neighbors initial_state_2 = [[0, 0, 1], [0, 1, 1], [0, 0, 0]] expected_state_2 = [[0, 1, 1], [0, 1, 1], [0, 0, 0]] actual_state_2 = life.next_board_state(initial_state_2) assert actual_state_2 == expected_state_2, 'Test 2 failed' # TEST 3: cells should die or stay dead with <= 1 neighbor initial_state_3 = [[1, 0, 0], [0, 0, 1], [0, 0, 1]] expected_state_3 = [[0, 0, 0], [0, 1, 0], [0, 0, 0]] actual_state_3 = life.next_board_state(initial_state_3) assert actual_state_3 == expected_state_3, 'Test 3 failed' # TEST 4: cells should die with > 3 live neighbors initial_state_4 = [[0, 1, 1], [0, 1, 1], [0, 1, 0]] expected_state_4 = [[0, 1, 1], [1, 0, 0], [0, 1, 1]] actual_state_4 = life.next_board_state(initial_state_4) assert actual_state_4 == expected_state_4, 'Test 4 failed' # TEST 5: cells should stay alive with 2 or 3 live neighbors initial_state_5 = [[0, 1, 1], [0, 0, 1], [0, 1, 0]] expected_state_5 = [[0, 1, 1], [0, 0, 1], [0, 0, 0]]
def test_next_empty(self): expected = dead_state(3, 4) actual = next_board_state(expected) self.assertEqual(expected, actual)