def life_sequence(world,sleep=1): os.system('clear') last_body_count = 0 counter = 0 for x in range(1,55555): os.system('clear') print "Generation #",x print print_world(world) # Optionally: Remove distant cells #world = prune(world,50) time.sleep(sleep) new_world = next_generation(world) if len(new_world) == 0 : print "All cells dead at generation #",x break world = new_world # Break once the population has been stabilized if len(world) == last_body_count: counter += 1 if counter > 100 : print "Population stable at generation:",x - counter break else: last_body_count = len(world) return
def test_next_gen() : grid = [[0,0,0], [1,1,1], [0,0,0]] nexgen = game_of_life.next_generation(grid) assert nexgen == [[0,1,0], [0,1,0], [0,1,0]]
def test_next_generation(): grid =[[1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1]] grid1 = [[0,0,1,0], [0,0,1,0], [0,0,1,0], [0,0,1,0]] new_life = game_of_life.next_generation(grid) new_life1 = game_of_life.next_generation(grid1) assert new_life == [[1,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,1]] assert new_life1 == [[0,0,0,0],[0,1,1,1],[0,1,1,1],[0,0,0,0]]
def test(self): self.assertEquals( next_generation( sorted([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (5, -1), (5, 5), (6, -1), (6, 5), (7, -1), (7, 5), (9, 1), (9, 2), (9, 3)])), sorted([(0, 1), (0, 2), (1, 0), (1, 3), (2, 0), (3, 1), (3, 3), (4, 2), (6, -2), (6, -1), (6, 0), (6, 4), (6, 5), (6, 6), (8, 2), (9, 2), (10, 2)]))
def life_sequence(world, sleep=1): print '<!DOCTYPE html><html><head><title>Game of Life animation generated with Python and HTML5/SVG</title>' print '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Dosis">' print '<link rel="image_src" href="https://www.doc.ic.ac.uk/project/examples/2012/163/g1216326/img/gameoflife.png"/>' print '<style>.generations{ display: none } .svgText { font-family: Dosis; font-size: 1em;}' print '</style></head><body>' print print '<!--' print 'Game of Life animation generated with Python and HTML5/SVG' print 'Seed (based on xxxP200H100V0A359):' print '[(0, 20), (-1, 20), (-2, 20), (-3, 20), (0, 21), (-4, 21), (0, 22), (-1, 23), (-4, 23), (-3, 26), (-2, 27), (-2, 28), (-2, 29), (-3, 29), (-4, 30), (-8, 31), (-9, 31), (-12, 31), (-13, 31), (-8, 32), (-9, 32), (-12, 32), (-13, 32), (0, 34), (-1, 34), (-2, 34), (-3, 34), (0, 35), (-4, 35), (0, 36), (-1, 37), (-4, 37), (0, 0), (1, 0), (2, 0), (3, 0), (0, 1), (4, 1), (0, 2), (1, 3), (4, 3), (3, 6), (2, 7), (2, 8), (2, 9), (3, 9), (4, 10), (8, 11), (9, 11), (12, 11), (13, 11), (8, 12), (9, 12), (12, 12), (13, 12), (0, 14), (1, 14), (2, 14), (3, 14), (0, 15), (4, 15), (0, 16), (1, 17), (4, 17)]' print print 'https://github.com/juan-domenech/game_of_life/html.py' print '-->' print for generation in range(1, 5000): print '<div id="generation_' + str( generation) + '" class="generations">' print '<svg width="1400" height="600">' print_world_HTML(world, generation) print '</div></svg>' print '<script type="text/javascript">' print 'setTimeout(function(){var elem=document.getElementById("generation_' + str( generation) + '");elem.parentNode.removeChild(elem);},' + str( 100 * generation) + ');' print 'setTimeout(function(){document.getElementById("generation_' + str( generation + 1) + '").style.display="block";},' + str( 100 * generation) + ');' print '</script>' print world = next_generation(world) if len(world) == 0: print "All cells dead at generation #", generation break print '</body></html>' return
def test_next_generation_two_not_neighbour_live_cells(): grid = [[True, False, False], [False, False, False], [False, False, True]] a = game_of_life.next_generation(m=grid) e = [[False, False, False], [False, False, False], [False, False, False]] assert a == e
def test_next_generation_one_live_cell(): grid = [[False, False, False], [False, True, False], [False, False, False]] a = game_of_life.next_generation(m=grid) e = [[False, False, False], [False, False, False], [False, False, False]] assert a == e
def test_next_generation_for_various_size_matrix(): grid = [[True, True], [False, True], [True, False]] a = game_of_life.next_generation(m=grid) e = [[True, True], [False, True], [False, False]] assert a == e
def test_next_generation_five_live_cells_straight_cross(): grid = [[False, True, False], [True, True, True], [False, True, False]] a = game_of_life.next_generation(m=grid) e = [[True, True, True], [True, False, True], [True, True, True]] assert a == e
def test_next_generation_four_live_cells_three_through_side_and_one_on_side_far( ): grid = [[True, False, False], [True, False, True], [True, False, False]] a = game_of_life.next_generation(m=grid) e = [[False, True, False], [True, False, False], [False, True, False]] assert a == e
def test_next_generation_four_live_cells_three_diagonally_and_one_on_corner(): grid = [[True, False, False], [False, True, False], [True, False, True]] a = game_of_life.next_generation(m=grid) e = [[False, False, False], [True, True, False], [False, True, False]] assert a == e
def life_sequence(world, sleep=1): palette = [ 'white', '#8b0000', '#ff0300', '#ff7700', '#ffeb00', '#9fff5e', '#2bffd3', '#00b5ff', '#0040ff', '#0000ca', 'spectrogram palette' ] print '<!DOCTYPE html><html><head><title>Game of Life animation generated with Python and HTML5/SVG</title>' print '<link rel="image_src" href="http://juan-domenech.github.io/sandbox/python/game-of-life/seed-rabbits-3000-generations.png"/>' print '<style>.generations{ display: none } ' print '.svgText { font-family: Courier New; font-size: 25px; fill: white;}' print '.svgTextBold { font-family: Courier New; font-size: 25px; font-weight: bold; fill: black;}' print '</style></head><body>' print print '<!--' print 'Game of Life animation generated with Python and HTML5/SVG' print 'Seed "Rabbits": rle="o3b3o$3o2bo$bo!"' print print 'https://github.com/juan-domenech/game_of_life/' print '-->' print for generation in range(1, generations): print '<div id="generation_' + str( generation) + '" class="generations">' print '<svg width="1260" height="630" style="background: black">' print_world_HTML_with_retracing(world, generation, palette) world.pop(0) world.append(next_generation(world[8])) # print '<!-- Palette -->' for item in range(0, 10): print '<rect x=' + str( (30 * item) + 10) + ' y="590" width="30" height="30" fill="' + palette[ item] + '"/>' # print '<!-- -->' print '<script type="text/javascript">' print 'setTimeout(function(){var elem=document.getElementById("generation_' + str( generation) + '");elem.parentNode.removeChild(elem);},' + str( rate * generation) + ');' print 'setTimeout(function(){document.getElementById("generation_' + str( generation + 1) + '").style.display="block";},' + str( rate * generation) + ');' print '</script>' print '</div></svg>' # Print last world and leave it static print '<div id="generation_' + str(generations) + '" class="generations">' print '<svg width="1260" height="630" style="background: black">' for item in range(0, 10): print '<rect x=' + str( (30 * item) + 10 ) + ' y="590" width="30" height="30" fill="' + palette[item] + '"/>' print_world_HTML_with_retracing(world, generations, palette) print '<script type="text/javascript">' print 'setTimeout(function(){document.getElementById("generation_' + str( generations) + '").style.display="block";},' + str( 200 * generations) + ');' print '</script>' print '</body></html>' return
def test(self): self.assertEquals(next_generation([]), [])
def test(self): self.assertEquals( next_generation( sorted([(0, 0), (0, 1), (1, 0), (2, 3), (3, 2), (3, 3)])), sorted([(0, 0), (0, 1), (1, 1), (1, 0), (2, 2), (2, 3), (3, 2), (3, 3)]))
def test_next_generation_three_live_cells_neighbours_all_at_different_sides(): grid = [[False, False, False], [True, False, True], [False, True, False]] a = game_of_life.next_generation(m=grid) e = [[False, False, False], [False, True, False], [False, True, False]] assert a == e
def test_next_generation_three_live_cells_neighbours_diagonally(): grid = [[True, False, False], [False, True, False], [False, False, True]] a = game_of_life.next_generation(m=grid) e = [[False, False, False], [False, True, False], [False, False, False]] assert a == e
def test_next_generation_four_live_cells_neighbours_each_on_side(): grid = [[False, True, False], [True, False, True], [False, True, False]] a = game_of_life.next_generation(m=grid) e = [[False, True, False], [True, False, True], [False, True, False]] assert a == e