def run_game(size): gol = GameOfLife(size) display = Display() while True: display.show_board(gol.get_board()) time.sleep(1)
def test_set_dead_not_present(self): gol = GameOfLife() gol.set_dead(dict(x=2, y=4)) self.assertFalse('2:4' in gol.table) self.assertEqual(0, len(gol.table))
def play_game(self): while True: pygame.time.wait(1000) self.clock.tick(10) self.draw_cells() pygame.display.update() for event in pygame.event.get(): print(event) escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE if event.type == pygame.QUIT: pygame.quit() elif event.type == escape: pass elif event.type == pygame.KEYDOWN: if event.key == pygame.K_r: self.restart() elif event.type == pygame.MOUSEBUTTONDOWN: mousebuttondown = True while mousebuttondown: pos = pygame.mouse.get_pos() self.new_cell(Vec2D(pos[0]/configs.PIXEL_SIZE, pos[1]/configs.PIXEL_SIZE)) self.game = GameOfLife(self.generation) self.draw_cells() pygame.display.update() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: mousebuttondown = False self.generation = self.game.next_generation() self.game = GameOfLife(self.generation)
def test_glider(self): gol = GameOfLife() gol.set_alive(dict(x=1, y=1), dict(x=2, y=1), dict(x=3, y=1), dict(x=1, y=2), dict(x=2, y=3)) gol.next_step() self.assertEqual(set(['3:2', '1:1', '1:2', '2:1', '2:0']), gol.table)
def test_get_neighbors(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=1), dict(x=3, y=1), dict(x=2, y=2), dict(x=3, y=2), dict(x=3, y=3)) neighbors_count = gol.get_neighbors_count(dict(x=1, y=1)) self.assertEqual(2, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=2, y=1)) self.assertEqual(3, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=3, y=1)) self.assertEqual(3, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=3, y=1)) self.assertEqual(3, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=2, y=2)) self.assertEqual(4, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=3, y=2)) self.assertEqual(4, neighbors_count) neighbors_count = gol.get_neighbors_count(dict(x=3, y=3)) self.assertEqual(2, neighbors_count)
def test_next_cell_status_IV(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=2), dict(x=2, y=4), dict(x=3, y=3), dict(x=3, y=4)) status = gol.next_cell_status(dict(x=2, y=3)) self.assertFalse(status)
def test_get_neighbors_count_zero(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=2), dict(x=2, y=4), dict(x=3, y=3), dict(x=3, y=4), dict(x=5, y=-3)) count = gol.get_neighbors_count(dict(x=-1, y=-1)) self.assertEqual(0, count)
def test_tick_with_one_death(self): seed = set([(0,0)]) game = GameOfLife(seed) game.tick() self.assertEqual(game.alive_cells, set())
def setUp(self): self.g = GameOfLife() self.conf = configurations() with open('config.json') as config_fd: config = json.load(config_fd) self.grid_width = np.clip(config['width'], 8, 30) self.grid_height = np.clip(config['height'], 8, 30)
def test_tick_with_one_birth(self): seed = set([(1, 0), (2, 2), (0, 1)]) game = GameOfLife(seed) game.tick() self.assertEqual(game.alive_cells, set([(1, 1)]))
def test_game_of_life_empty_tick(self): # GIVEN seed = set() game_of_life = GameOfLife(seed) # WHEN next_generation = game_of_life.tick() # THEN self.assertEqual(next_generation, seed)
def testIsDead(self): matrix = GameOfLife.matrixUniverse(5, 5) matrix[1, 1] = 0 matrix[1, 2] = 1 dead = GameOfLife.isDead(1, 1) self.assertEqual(dead, True) notDead = GameOfLife.isDead(1, 2) self.assertFalse(notDead, False)
def __init__(self): super().__init__(width, height) self.gameOfLife = GameOfLife(self.get_size()[0], self.get_size()[1], cell_size, fill_percent) pyglet.clock.schedule_interval(self.update, 1.0/24.0) self.i = 0
def random_try(size): width = size length = width gol = GameOfLife() gol.init(width, length) print(gol) front = Front(gol, width, length)
def test_initialize_board_to_correct_size(self): test_object = GameOfLife(2) board = test_object.get_board() self.assertEqual(len(board), 2) self.assertEqual(len(board[0]), 2) self.assertEqual(len(board[1]), 2)
def test_tick_with_all_survivors(self): seed = set([(1, 0), (2, 0), (1, 1), (2, 1)]) game = GameOfLife(seed) game.tick() self.assertEqual(game.alive_cells, seed)
def test_should_return_correct_set_of_alive_neighbours_of_cell(self): # GIVEN seed = set([(-1, -1), (-1, 0)]) game_of_life = GameOfLife(seed) cell = (0, 0) expected_neighbours = set([(-1, -1), (-1, 0)]) # WHEN alive_neighbours = game_of_life.get_alive_neighbours(cell) # THEN self.assertEqual(expected_neighbours, alive_neighbours)
def test_dead_neighbours(self): seed = set([(0, 0), (1, 1)]) game = GameOfLife(seed) neighbours = game.dead_neighbours((0, 0)) cells_expected = set([(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), ]) self.assertEqual(neighbours, cells_expected)
def test_blinker(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=3), dict(x=2, y=4), dict(x=2, y=2)) gol.next_step() self.assertTrue('1:3' in gol.table) self.assertTrue('2:3' in gol.table) self.assertTrue('3:3' in gol.table) self.assertEqual(3, len(gol.table))
def test_get_important_cells_I(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=3)) cells = sorted(tuple(gol.get_important_cells())) self.assertEqual( sorted((dict(x=1, y=3), dict(x=1, y=2), dict(x=2, y=2), dict(x=3, y=2), dict(x=3, y=3), dict(x=3, y=4), dict(x=2, y=4), dict(x=1, y=4))), cells )
def test_set_dead(self): gol = GameOfLife() gol.set_alive(dict(x=2, y=4)) gol.set_alive(dict(x=1, y=5)) gol.set_dead(dict(x=2, y=4)) self.assertFalse('2:4' in gol.table) self.assertTrue('1:5' in gol.table) self.assertEqual(1, len(gol.table))
def test_static_block(self): gol = GameOfLife() gol.set_alive(dict(x=1, y=1), dict(x=2, y=1), dict(x=2, y=2), dict(x=1, y=2)) gol.next_step() self.assertTrue('1:1' in gol.table) self.assertTrue('2:2' in gol.table) self.assertTrue('1:2' in gol.table) self.assertTrue('2:1' in gol.table) self.assertEqual(4, len(gol.table))
def __init__(self): super().__init__(600, 600) self.gameOfLife = GameOfLife(self.get_size()[0], self.get_size()[1], cell_size, 0.4) pyglet.clock.schedule_interval(self.update, 1.0 / float(framerate)) self.midiDispatcher = midi_dispatcher.MidiDispatcher() @self.midiDispatcher.midiInputHandler.event def on_pitch(pitch): #self.gameOfLife.drawCircle(random.uniform(-1, 1)*250 + 300, random.uniform(-1, 1)*250 +300) self.gameOfLife.run_rules() #actualizo el juego con el teclado
def test4(self): field = """ ...... ...... ......""" expected = '......\n......\n......' ga = GameOfLife(3, 6, field) ga.next_generation() self.assertEqual(expected, ga.to_string())
def test_print(self): gol = GameOfLife() gol.set_alive(dict(x=1, y=1), dict(x=2, y=1), dict(x=3, y=1), dict(x=1, y=2), dict(x=2, y=3)) s = str(gol) expected = [ ' ', ' xxx', ' x ', ' x ' ] self.assertEqual('\n'.join(expected), s)
def test_tick_block_should_survive(self): """ ** ** => ** ** """ # GIVEN seed = set([(0, 0), (0, 1), (1, 0), (1, 1)]) expected = seed game_of_life = GameOfLife(seed) # WHEN & THEN self.assertEqual(game_of_life.tick(), expected)
def test_can_update(self): game = GameOfLife(width=self.width, height=self.height, cell_size=1) game.clist = self.clist with open('steps.txt') as f: steps = json.load(f) num_updates = 0 for step in sorted(steps.keys(), key=int): with self.subTest(step=step): for _ in range(int(step) - num_updates): game.clist = game.update_cell_list(game.clist) num_updates += 1 self.assertEqual(steps[step], game.clist)
def test3(self): field = """ ...XX. .XX... ..X... XX.... X..XX.""" expected = '.X..XX\n.XX...\nX.X...\nXXXX.X\nXXXXX.' ga = GameOfLife(5, 6, field) ga.next_generation() self.assertEqual(expected, ga.to_string())
class Window(pyglet.window.Window): def __init__(self): super().__init__(800, 800) #pyglet.gl.glClearColor(0.1, 0.3, 0.3, 1.0) self.gameOfLife = GameOfLife(self.get_size()[0], self.get_size()[1], 20, 0.3) pyglet.clock.schedule_interval(self.update, 1.0 / 24.0) def on_draw(self): self.clear() self.gameOfLife.draw() def update(self, dt): self.gameOfLife.run_rules()
def test_tick_with_one_cell_dies(self): """ .*. => ... """ # GIVEN seed = set([(0, 0)]) expected_1 = set() expected_2 = set() game_of_life = GameOfLife(seed) # WHEN & THEN self.assertEqual(game_of_life.tick(), expected_1) self.assertEqual(game_of_life.tick(), expected_2)
def __init__(self): try: self.cnt = 0 self.gol_obj = GameOfLife() lcd.clear() lcd.setColor(lcd.WHITE) buttonA.wasPressed(self.on_AwasPressed) while True: self.print_gol() except Exception as e: lcd.print(str(e))
def __init__(self, path=None, cols=10, rows=10, writer=False): # fourcc = cv.VideoWriter_fourcc(*'XVID') self.writer = writer self.path = path self.cols = cols self.rows = rows self.stream() self.video_h = int(self.cap.get(cv.CAP_PROP_FRAME_HEIGHT)) self.video_w = int(self.cap.get(cv.CAP_PROP_FRAME_WIDTH)) self.height = self.video_h // self.rows self.width = self.video_w // self.cols self.crop_h = self.height * self.rows self.crop_w = self.width * self.cols self.game = GameOfLife(H=rows, W=cols, init=None)
class Window(pyglet.window.Window): def __init__(self): super(Window, self).__init__(600, 600) self.game_of_life = GameOfLife(600, 600, 10, 0.2) pyglet.clock.schedule_interval(self.update, 1.0 / 24.0) def on_draw(self): self.clear() self.game_of_life.draw() self.game_of_life.run_rules() def update(self, dt): pass
class LiveNeighbourCounterTests(unittest.TestCase): gol = GameOfLife() def test_every_cell_of_dead_grid_has_zero_live_neighbours(self): grid = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] self.assertEquals(0, self.gol.count_alive_neighbours(grid, (0, 0))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (0, 1))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (0, 2))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (1, 0))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (1, 1))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (1, 2))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (2, 0))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (2, 1))) self.assertEquals(0, self.gol.count_alive_neighbours(grid, (2, 2))) def test_every_cell_of_fully_live_grid_has_eight_live_neighbours(self): grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] self.assertEquals(8, self.gol.count_alive_neighbours(grid, (0, 0))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (0, 1))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (0, 2))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (1, 0))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (1, 1))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (1, 2))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (2, 0))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (2, 1))) self.assertEquals(8, self.gol.count_alive_neighbours(grid, (2, 2))) def test_single_alive_cell_grid_has_eight_live_neighbours(self): grid = [[1]] self.assertEquals(8, self.gol.count_alive_neighbours(grid, (0, 0)))
def main(): """Since 1 braille chars can represent 2*4 points we have to scale the board accordingly""" width, height = drawille.getTerminalSize() width, height = int((width*2.0)-2), int((height*4.0)-4) game = GameOfLife(width=width, height=height) def fill_board_randomly(game): def set_cell_randomly(): if randrange(10) > randrange(6, 10): return 1 return 0 for y in range(game.height): for x in range(game.width): game.board[y][x] = set_cell_randomly() fill_board_randomly(game) def frame_coordinates(game): while True: game.evolve_board() s = [] for y in range(game.height): for x in range(game.width): if game.board[y][x] == 1: s.append((x, y)) yield s s = drawille.Canvas() drawille.animate(s, frame_coordinates, 1./5, game)
def test_should_return_correct_set_of_birth_candidates(self): """ .... .**. .... """ # GIVEN seed = set([(0, 0), (0, 1)]) game_of_life = GameOfLife(seed) expected_birth_candidates = set([(-1, 1), (-1, 0), (-1, -1), (1, 0), (0, -1), (1, 1), (1, -1), (1, 2), (0, 2), (-1, 2)]) # WHEN birth_candidates = game_of_life.get_birth_candidates() # THEN self.assertEqual(expected_birth_candidates, birth_candidates)
def test_game_of_life_object_initialization(self): # GIVEN seed = set([]) # WHEN game_of_life = GameOfLife(seed) # THEN self.assertEqual(game_of_life.alive_cells, seed)
def headless_man(): model = """ --------- --OO----- --O---O-- ---OOOO-- --------- ---OOOO-- --O---O-- --OO----- --------- """ gol = GameOfLife() gol.set_grid(model, "O") print(gol) front = Front(gol, gol.rows, gol.cols)
def __init__(self): super().__init__(500, 500) self.gameOfLife = GameOfLife(500, 500, 10, 0.4) pyglet.clock.schedule_interval(self.update, 1.0/12.0) self.gameOfLife.load_grid(((1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0), (1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1), (0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0), (1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0), (1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 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1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), (1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0), (1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0)))
class App: def __init__(self): try: self.cnt = 0 self.gol_obj = GameOfLife() lcd.clear() lcd.setColor(lcd.WHITE) buttonA.wasPressed(self.on_AwasPressed) while True: self.print_gol() except Exception as e: lcd.print(str(e)) def on_AwasPressed(self): lcd.clear() self.gol_obj.reset() def print_gol(self): #次世代セルを計算 matrix = self.gol_obj.calc_state() #表示情報の取得( 1を黒, 0を白で表示) img = self.gol_obj.create_matrix_img(matrix) # 表示をアップデート self.draw_pyxel_matrix(img) self.cnt += 1 #lcd.print(str(self.cnt)) def draw_pyxel_matrix(self, img): for y, values in enumerate(img): for x, val in enumerate(values): if val == 255: #lcd.rect(x*15+45 ,y*15+5, 15 ,15, lcd.BLACK,lcd.BLACK) lcd.circle(x * 15 + 55, y * 15 + 15, 7, lcd.BLACK, lcd.BLACK) elif val == 0: #lcd.rect(x*15+45 ,y*15+5, 15 ,15, lcd.WHITE,lcd.WHITE) lcd.circle(x * 15 + 55, y * 15 + 15, 7, lcd.WHITE, lcd.WHITE)
def run(self): for i in range(0, self.iters): coverage = rnd.random() gol = GameOflife(50, 50, coverage) cycle_detected_flag = False grids_saved = {} iterations = 0 initial_grid_state = str(gol.get_as_tuple()) living_cells_count = gol.get_living_count() all_dead = False while not cycle_detected_flag and iterations < self.MAX_CYCLES: current_grid_state = hash(gol.get_as_tuple()) if current_grid_state not in grids_saved: grids_saved[current_grid_state] = iterations iterations += 1 else: cycle_detected_flag = True final_living_cells_count = gol.get_living_count() if final_living_cells_count == 0: all_dead = True entry = Dataset(percent_fill = coverage, intial_fill = living_cells_count, final_fill = final_living_cells_count, is_empty = all_dead, generations = iterations, generation_loop = grids_saved[current_grid_state], loop_length = iterations - grids_saved[current_grid_state], initial_generation = initial_grid_state) self.db_session.add(entry) self.db_session.commit() gol.run_rules() print(self.thread_name, i)
def test_import_from_file(self): gol = GameOfLife.import_from_file('./example') self.assertTrue('1:0' in gol.table) self.assertTrue('2:0' in gol.table) self.assertTrue('0:1' in gol.table) self.assertTrue('2:1' in gol.table) self.assertTrue('1:2' in gol.table) self.assertEqual(5, len(gol.table))
def run_unicorn_loop(): global life global period_count period_count = (period_count + 1) % period if period_count == 0: life.next_generation() life.show_board() if life.all_dead(): life = GameOfLife(r_shift, g_shift, b_shift)
def _init_simulation(self): # Restart a new Game of Life simulation. initial_population_size = random.randint(int(0.1 * g.number_of_nodes()), g.number_of_nodes()) print 'Initial population size', initial_population_size initial_population = random.sample(g.nodes(), initial_population_size) self._game = GameOfLife(g, initial_population) self._tick = -1 self._random_index = range(1, 384) random.shuffle(self._random_index)
def test_total_around(self): """ Currently doesn't give meaningful fails TO DO - meaningful fails! """ g = GameOfLife(6, 4) """ ---x-- --x--- -x---- x-x-x- """ g.grid[(3, 0)] = 1 g.grid[(2, 1)] = 1 g.grid[(1, 2)] = 1 g.grid[(0, 3)] = 1 g.grid[(2, 3)] = 1 g.grid[(4, 3)] = 1 expected = { (0, 0): 1, (1, 0): 3, (2, 0): 3, (3, 0): 4, (4, 0): 2, (5, 0): 2, (0, 1): 1, (1, 1): 2, (2, 1): 3, (3, 1): 2, (4, 1): 1, (5, 1): 0, (0, 2): 2, (1, 2): 4, (2, 2): 3, (3, 2): 3, (4, 2): 1, (5, 2): 2, (0, 3): 2, (1, 3): 3, (2, 3): 3, (3, 3): 3, (4, 3): 2, (5, 3): 2 } for key in g.grid: self.assertEqual(g.total_around(*key), expected[key])
def test_should_arise_from_dead_one_cell(self): """ .* ** => ** ** """ # GIVEN seed = set([(0, 0), (0, 1), (1, 1)]) game_of_life = GameOfLife(seed) expected_births = set([(1, 0)]) # WHEN births = game_of_life.get_births() # THEN self.assertEqual(expected_births, births)
def test_should_return_only_survivors(self): """ .* ** => ** ** """ # GIVEN seed = set([(0, 0), (0, 1), (1, 1)]) game_of_life = GameOfLife(seed) expected_survivors = set([(0, 0), (0, 1), (1, 1)]) # WHEN survivors = game_of_life.get_survivors() # THEN self.assertEqual(expected_survivors, survivors)
def init(c: PixmapCanvas): print("init") c.gol = GameOfLife(ROWS, COLS, solid_borders=SOLID_BORDERS, rules=RULES) c.timer.stop() c.timeout(False) c.cell_width = c.width() / c.gol.rows c.cell_height = c.height() / c.gol.cols print(c.cell_width, c.cell_height)
def test_should_return_correct_neighbours(self): # GIVEN cell = (0, 0) expected_len = 8 expected_neighbours = set([(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]) # WHEN neighbours = GameOfLife.get_neighbours(cell) # THEN self.assertEqual(expected_len, len(neighbours)) self.assertEqual(expected_neighbours, neighbours)
def test_should_return_correct_neighbours_any_cell(self): # GIVEN cell = (14, 3) expected_len = 8 expected_neighbours = set([(13, 2), (14, 2), (15, 2), (13, 3), (15, 3), (13, 4), (14, 4), (15, 4)]) # WHEN neighbours = GameOfLife.get_neighbours(cell) # THEN self.assertEqual(expected_len, len(neighbours)) self.assertEqual(expected_neighbours, neighbours)
def __init__(self, settings=Settings()): pygame.init() self.generation = {} self.game = GameOfLife(self.generation, settings) self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((configs.SCREENWIDTH, configs.SCREENHEIGHT)) self.background = pygame.Surface(self.screen.get_size()) self.background.fill(configs.BLACK) pygame.display.set_caption('Game of Life') while self.play_game(): pass
class GameOfLifeTest(unittest.TestCase): def setUp(self): self.game = GameOfLife({Vec2D(0, 0): Cell(True), Vec2D(0, 1): Cell(True), Vec2D(1, 0): Cell(True), Vec2D(3, 3): Cell(True)}) def test_alive_cell_when_game_is_set_up(self): self.assertEqual(self.game.is_alive(Vec2D(1, 0)), True) self.assertEqual(self.game.is_alive(Vec2D(1, 1)), False) def test_number_of_alive_neighbours(self): self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(0, 0)), 2) self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(1, 1)), 3) self.assertEqual(self.game.number_of_alive_neighbours(Vec2D(4, 0)), 0) def test_neighbours_of(self): self.assertEqual(sorted(self.game.neighbours_of(Vec2D(0, 0))), sorted([Vec2D(-1, -1), Vec2D(-1, 0), Vec2D(-1, 1), Vec2D(0, -1), Vec2D(0, 1), Vec2D(1, -1), Vec2D(1, 0), Vec2D(1, 1)])) def test_will_be_born(self): self.assertEqual(self.game.will_be_born(Vec2D(1, 1)), True) self.assertEqual(self.game.will_be_born(Vec2D(0, 1)), False) def test_will_servive(self): self.assertEqual(self.game.will_servive(Vec2D(0, 1)), True) self.assertEqual(self.game.will_servive(Vec2D(3, 3)), False) def test_stable_cells(self): stable_cells = self.game.stable_cells() self.assertEqual(sorted(stable_cells.keys()), sorted([Vec2D(0, 0), Vec2D(0, 1), Vec2D(1, 0)])) self.assertEqual(stable_cells[Vec2D(0, 0)].new_born, False) def test_new_born_cells(self): new_born_cells = self.game.new_born() self.assertEqual(sorted(new_born_cells.keys()), [Vec2D(1, 1)]) self.assertEqual(new_born_cells[Vec2D(1, 1)].new_born, True) def test_next_generation(self): next_generation = self.game.next_generation() self.assertEqual(sorted(next_generation.keys()), sorted([Vec2D(0, 0), Vec2D(0, 1), Vec2D(1, 0), Vec2D(1, 1)]))
class TestGameOfLife(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), (0, 1)]) self.assert_dead((0, 1)) 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), (2, 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), (2, 1), (0, 2)]) self.assert_alive((1, 2)) def evolve(self, cells): self.next_gen = self.game.evolve(cells) def assert_alive(self, cell): self.assertTrue(cell in self.next_gen) def assert_dead(self, cell): self.assertFalse(cell in self.next_gen)
class Window(pyglet.window.Window): def __init__(self): super().__init__(500, 500) self.gameOfLife = GameOfLife(500, 500, 10, 0.4) pyglet.clock.schedule_interval(self.update, 1.0/12.0) self.gameOfLife.load_grid(((1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0), (1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1), (0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0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, 0, 0, 1, 1, 0, 0), (0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), (1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), (1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0), (1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0))) def on_draw(self): self.clear() self.gameOfLife.draw() def update(self, dt): pass self.gameOfLife.run_rules()
def setUp(self): self.game = GameOfLife({Vec2D(0, 0): Cell(True), Vec2D(0, 1): Cell(True), Vec2D(1, 0): Cell(True), Vec2D(3, 3): Cell(True)})
class GUIGameOfLife: def __init__(self, settings=Settings()): pygame.init() self.generation = {} self.game = GameOfLife(self.generation, settings) self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((configs.SCREENWIDTH, configs.SCREENHEIGHT)) self.background = pygame.Surface(self.screen.get_size()) self.background.fill(configs.BLACK) pygame.display.set_caption('Game of Life') while self.play_game(): pass def play_game(self): while True: pygame.time.wait(1000) self.clock.tick(10) self.draw_cells() pygame.display.update() for event in pygame.event.get(): print(event) escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE if event.type == pygame.QUIT: pygame.quit() elif event.type == escape: pass elif event.type == pygame.KEYDOWN: if event.key == pygame.K_r: self.restart() elif event.type == pygame.MOUSEBUTTONDOWN: mousebuttondown = True while mousebuttondown: pos = pygame.mouse.get_pos() self.new_cell(Vec2D(pos[0]/configs.PIXEL_SIZE, pos[1]/configs.PIXEL_SIZE)) self.game = GameOfLife(self.generation) self.draw_cells() pygame.display.update() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: mousebuttondown = False self.generation = self.game.next_generation() self.game = GameOfLife(self.generation) def draw_cells(self): self.screen.fill(configs.BLACK) for coords, cell in self.game.alive_cells.items(): if cell.new_born: color = configs.GREEN else: color = configs.WHITE x, y = coords.x*configs.PIXEL_SIZE, coords.y*configs.PIXEL_SIZE pygame.draw.rect(self.screen, color, (x, y, configs.PIXEL_SIZE, configs.PIXEL_SIZE)) def new_cell(self, coords): cell = {coords: Cell(True)} self.generation.update(cell) def restart(self): self.generation = {} self.game = GameOfLife(self.generation) self.screen.fill(configs.BLACK) pygame.display.update()
def restart(self): self.generation = {} self.game = GameOfLife(self.generation) self.screen.fill(configs.BLACK) pygame.display.update()
def test_live_neighbours_one_neighbour(self): seed = set([(0, 0), (0, 1)]) game = GameOfLife(seed) self.assertEqual(game.live_neighbours((0, 0)), set([(0, 1)]))