def __init__(self): self.survive_min = 5 # Cycle self.surival_record = 0 self.designer = CellDesigner() self.gene_bank = GeneBank() self.game = GameOfLife() self.sense = SenseHat()
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 main(): # Initialisation de pygame pygame.init() # Creation d'un window builder builder = WindowBuilder(1000) builder.makeWindow() builder.createGrid() # Creation d'un modifier modifier = WindowModifier(builder.fenetre, builder.rect_width, builder.rect_height, builder.margin) # Controleur de la boucle continuer = True gameOn = False while continuer: for event in pygame.event.get(): if event.type == QUIT: continuer = False if event.type == MOUSEBUTTONDOWN and event.button == 1: modifier.modifyColors(event.pos[0], event.pos[1]) if event.type == KEYDOWN and event.key == K_SPACE: game = GameOfLife(modifier) gameOn = not gameOn if gameOn: game.nextGen() modifier.colors = game.colors time.sleep(0.3) modifier.redrawGrid()
def main(): game = GameOfLife() sense = SenseHat() # cells = [ (2, 4), (3, 5), (4, 3), (4, 4), (4, 5) ] cells = [(2, 4), (2, 5), (1, 5), (1, 6), (3, 5)] game.set_cells(cells) while True: try: canvas = [] for i in game.world: if not i: canvas.append(WHITE) else: canvas.append(RED) sense.set_pixels(canvas) game.run() if not game.everyone_alive(): sense.clear() print("everyone died") break time.sleep(0.1) except: sense.clear() break
def test_step_rule_4(self): initial_state = [[0, 1, 0], [0, 1, 1], [0, 0, 0]] next_state = np.array([[0, 1, 1], [0, 1, 1], [0, 0, 0]]) game = GameOfLife(initial_state=initial_state) game.step() game_state = game.get_state() self.assertTrue(np.array_equal(game_state, next_state))
def test_run(self): initial_state = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] final_state = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) game = GameOfLife(initial_state=initial_state) game.run(generations=2) game_state = game.get_state() self.assertTrue(np.array_equal(game_state, final_state))
def test_tick_blinker(): state_1 = [(1, 0), (1, 1), (1, 2)] state_2 = [(0, 1), (1, 1), (2, 1)] gof = GameOfLife(seed=state_1) gof.tick() assert all(cell in gof.state for cell in state_2) gof.tick() assert all(cell in gof.state for cell in state_1)
def test_gen_world(rules): gl = GameOfLife(rules) shape = (50, 50) w = gl.gen_world(shape) assert w.shape == shape shape = (20, 50) w = gl.gen_world(shape) assert w.shape == shape
def test_cell_with_less_than_two_neighbours_dies(self): expect = [[False, False, False], [False, False, False], [False, False, False]] board = [[False, False, False], [True, True, False], [False, False, False]] gameoflife = GameOfLife(board) gameoflife.nextGen() self.assertEqual(expect, gameoflife.board)
def beacon(): game = GameOfLife(6) game.add_living_cell(1, 1) game.add_living_cell(2, 1) game.add_living_cell(1, 2) game.add_living_cell(4, 3) game.add_living_cell(3, 4) game.add_living_cell(4, 4) return game
def toad(): game = GameOfLife(6) game.add_living_cell(2, 2) game.add_living_cell(3, 2) game.add_living_cell(4, 2) game.add_living_cell(1, 3) game.add_living_cell(2, 3) game.add_living_cell(3, 3) return game
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_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_cell_with_two_neighbours_lives(self): board = [[True, False, False], [False, True, False], [False, False, True]] expect = [[False, False, False], [False, True, False], [False, False, False]] gameoflife = GameOfLife(board) gameoflife.nextGen() self.assertEqual(expect, gameoflife.board)
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 initialise(self): self.title('Game Of Life') self.grid() # leave at defaults for a 2 dimensional game of life with # John Conway standard rules (3/23) self.gol = GameOfLife() self.create_widgets() self.bindings() self.reset() self.reset_colour()
def setUp(self): self.game = GameOfLife(10) self.game.add_living_cell(1, 1) self.game.add_living_cell(2, 1) self.game.add_living_cell(3, 1) self.game.add_living_cell(1, 2) self.game.add_living_cell(3, 2) self.game.add_living_cell(1, 3) self.game.add_living_cell(2, 3) self.game.add_living_cell(3, 3) self.game.add_living_cell(4, 4)
def test_tick_glider(): state_0 = [(0, 0), (2, 0), (1, 1), (2, 1), (1, 2)] state_1 = [(2, 0), (0, 1), (2, 1), (1, 2), (2, 2)] state_2 = [(1, 0), (2, 1), (3, 1), (1, 2), (2, 2)] state_3 = [(2, 0), (3, 1), (1, 2), (2, 2), (3, 2)] state_4 = [(1, 1), (3, 1), (2, 2), (3, 2), (2, 3)] gof = GameOfLife(seed=state_0) gof.tick() assert all(cell in gof.state for cell in state_1) gof.tick() assert all(cell in gof.state for cell in state_2) gof.tick() assert all(cell in gof.state for cell in state_3) gof.tick() assert all(cell in gof.state for cell in state_4)
def __init__(self): self.window = Tk() self.window.title = "Game of Life" self.window.geometry("1100x950") self.window.config(background='brown') # Init variables. self.variables() # Init frames. self.frame = Frame(self.window, bg='red') self.frame_sim = Frame(self.frame, bg='green', border=1) self.frame_time = Frame(self.frame_sim, bg="yellow", border=1) # Create all of widgets. self.create_widgets() # Pack. self.frame.pack(expand=YES) self.frame_sim.grid(pady=40, row=0, column=1, sticky=N) self.frame_time.pack(expand=YES) # Core. self.core = GameOfLife(113, 113)
def main(): game = GameOfLife() # cells = [ (2, 4), (3, 5), (4, 3), (4, 4), (4, 5) ] cells = [ (2, 4), (2, 5), (1,5 ), (1, 6), (3, 5)] game.set_cells(cells) while True: try: game.print_world() game.run() if not game.everyone_alive(): print("everyone died") break time.sleep(0.1) except: break
def test_live(): alive_cell = (1, 4) seed = [(6, 7)] gof = GameOfLife(seed=seed) gof.live(alive_cell) assert alive_cell in gof.state
def test_die(): alive_cell = (1, 4) seed = [alive_cell, (6, 7)] gof = GameOfLife(seed=seed) gof.die(alive_cell) assert alive_cell not in gof.state
def test_is_alive(): alive_cell = (1, 4) seed = [alive_cell, (6, 7)] gof = GameOfLife(seed=seed) assert gof.is_alive(alive_cell)
def test_that_neighbours_count_is_1(self): self.game = GameOfLife(10) self.assertEqual(self.game.neighbours_count(1, 2), 0) self.game.add_living_cell(0, 0) self.assertEqual(self.game.neighbours_count(1, 0), 1)
def setUp(self): self.game = GameOfLife(5)
def setUp(self): self.game = GameOfLife(5) self.game.add_living_cell(1, 2) self.game.add_living_cell(2, 2) self.game.add_living_cell(1, 2)
def test_that_it_raises_value_error_when_size_is_not_a_number(self): with self.assertRaises(ValueError): game_without_size = GameOfLife(0)
def test_init(): seed = [(1, 4), (6, 7)] gof = GameOfLife(seed=seed) assert seed == gof.state
def test_get_neighbors_count(neighbors, count): alive_cell = (3, 3) seed = [alive_cell] + neighbors gof = GameOfLife(seed=seed) assert gof.get_neighbors_count(alive_cell) == count
def test_that_neighbours_count_is_0(self): self.game = GameOfLife(10) self.assertEqual(self.game.neighbours_count(1, 2), 0) self.assertEqual(self.game.neighbours_count(2, 2), 0) self.assertEqual(self.game.neighbours_count(3, 2), 0) self.assertEqual(self.game.neighbours_count(4, 2), 0)