def main(args): try: stdscr = None iterations, width, height, state = _read_input() if args.curses: symbol = '*' if args.symbol: symbol = args.symbol stdscr, color = _setup_curses() game = GameOfLife(iterations, width, height, state, float(args.sleep), stdscr, color, symbol) else: game = GameOfLife(iterations, width, height, state, float(args.sleep)) game.simulate() if stdscr: curses.endwin() except KeyboardInterrupt: try: curses.endwin() except curses.error: pass
def test_next_value(self): # tests where the current field is alive g = GameOfLife(3) g.grid[(1, 1)] = 1 self.assertEqual(g.next_value(1, 1), 0) # underpopulation g.grid[(0, 0)] = 1 self.assertEqual(g.next_value(1, 1), 0) # underpopulation g.grid[(0, 1)] = 1 self.assertEqual(g.next_value(1, 1), 1) # survives g.grid[(0, 2)] = 1 self.assertEqual(g.next_value(1, 1), 1) # survives g.grid[(1, 0)] = 1 self.assertEqual(g.next_value(1, 1), 0) # overpopulation # tests where the current field is not alive g = GameOfLife(3) self.assertEqual(g.next_value(1, 1), 0) # no new life g.grid[(1, 1)] = 0 self.assertEqual(g.next_value(1, 1), 0) # no new life g.grid[(0, 0)] = 1 self.assertEqual(g.next_value(1, 1), 0) # no new life g.grid[(0, 1)] = 1 self.assertEqual(g.next_value(1, 1), 0) # no new life g.grid[(0, 2)] = 1 self.assertEqual(g.next_value(1, 1), 1) # new life! g.grid[(1, 0)] = 1 self.assertEqual(g.next_value(1, 1), 0) # no new life g.grid[(1, 2)] = 1 self.assertEqual(g.next_value(1, 1), 0) # no new life
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_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 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)
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 run_game(size): gol = GameOfLife(size) display = Display() while True: display.show_board(gol.get_board()) time.sleep(1)
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 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_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 random_try(size): width = size length = width gol = GameOfLife() gol.init(width, length) print(gol) front = Front(gol, width, length)
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 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 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 __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())
class GameOfLifeTests(unittest.TestCase): tester = GameOfLife() def test_spawn_glider(self): self.tester.spawn_glider() self.assertTrue(True) def test_next_generation(self): self.tester.next_generation() self.assertTrue(True) def test_Canary(self): self.assertTrue(True)
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 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())
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 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 __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)
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_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 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 do_POST(self): global life global r_shift global g_shift global b_shift global period self._set_headers() content_length = int(self.headers['Content-length']) in_text = self.rfile.read(content_length) if in_text.startswith('gameoflife'): in_args = in_text.split() r_shift = int(in_args[1]) g_shift = int(in_args[2]) b_shift = int(in_args[3]) period = int(in_args[4]) life = GameOfLife(r_shift, g_shift, b_shift) if in_text.startswith('random'): pass self.wfile.write(in_text)
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__(self): try: self.tnr = Tenorion() # tenorion initialize self.init_tenorion() self.tnr.play() 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))