def start_game(): file_info = argv[1] info = Parser.parse_start_info(file_info) matrix_size = info[0] live_cells = info[1] game = Game_Of_Life(matrix_size, live_cells) game.run_game()
def animation(n, nsweeps, initial_condition): g = Game_Of_Life(n, initial_condition) fig = plt.figure() im=plt.imshow(g.state, animated=True) for i in range (nsweeps): g.sweep() #perform flip plt.cla() im=plt.imshow(g.state, animated=True, vmin = 0, vmax = 1) plt.draw() plt.pause(0.0001)
def test_change_state(self): g = Game_Of_Life(n=5) g.setAlive(1, 1) g.setAlive(2, 2) g.setAlive(0, 2) g.setAlive(2, 0) g.nextState() self.assertEqual(g.isAlive(1, 1), True) self.assertEqual(g.isAlive(0, 0), False)
def com(n, nsweeps): x = [] y = [] t = [] vx = [] vy = [] vt = [] velocity = [] boundaries = [] g = Game_Of_Life(n, 'g') for i in range (nsweeps): x1, y1 = g.centre_mass() if x1 > 0 and y1 > 0: x.append(x1) y.append(y1) t.append(i) if i > 320 and i < 420: vx.append(x1) vy.append(y1) vt.append(i) g.sweep() np.savetxt('GOL_com_coords.dat', np.column_stack([x, y, t])) xslope, xintercept = np.polyfit(vt, vx, 1) yslope, yintercept = np.polyfit(vt, vy, 1) vel = math.sqrt(xslope**2 + yslope**2) velocity.append(vel) np.savetxt('GOL_com_velocity.dat', velocity) plt.scatter(t, x, s=3, label = 'x radius') plt.scatter(t, y, s=3, label = 'y radius') plt.legend() plt.ylabel('X and Y radii') plt.xlabel('Time (Sweeps)') plt.title('X and Y Radii vs Time') plt.savefig('GOL_com_vs_time.png') plt.show()
def test_calculate_neighbours_count(self): g = Game_Of_Life(n=5) g.setAlive(1, 1) for i in [0, 1, 2]: for j in [0, 1, 2]: if i == 1 and j == 1: pass else: self.assertEqual(g.get_neighbours_count(i, j), 1) self.assertEqual(g.get_neighbours_count(1, 1), 0) self.assertEqual(g.get_neighbours_count(2, 3), 0)
def equil_histo(n, nsweeps): g = Game_Of_Life(n, 'r') equil_t = [] for i in range(100): while True: for j in range(nsweeps): init_sites = g.active_states() g.sweep() current_sites = g.active_states() if init_sites == current_sites: equil_t.append(i) break plt.hist(equil_t) plt.show()
def equil_histo(n, nsweeps): equil_t = [] for i in range(500): print (i) g = Game_Of_Life(n, 'r') counter = 0 for j in range(5000): init_sites = g.active_states() g.sweep() current_sites = g.active_states() if init_sites == current_sites: counter += 1 if counter == 5: equil_t.append(j-4) break else: counter = 0 np.savetxt('GOL_equilibration_t_histo.dat', np.column_stack([equil_t]))
def test_is_moving_equilibrium(self): # correctly returns moving equilibrium true test_animals_next_board_false = [[0,0], [0,1], [0,2], [1,1]] test_1 = Game_Of_Life(4,4, test_animals_next_board_false) test_1.next_board() test_1.next_board() test_1.next_board() self.assertEqual(test_1.is_moving_equilibrium(), False) # correctly returns moving equilibrium true test_animals_next_board_true = [[0,1], [1,1], [2,1]] test_2 = Game_Of_Life(8, 8, test_animals_next_board_true) test_2.next_board() test_2.next_board() test_2.next_board() self.assertEqual(test_2.is_moving_equilibrium(), True)
def test_next_animal_state(self): # living and less than two neighbors -> dead test_animals_less_than_two = [[0,0], [0,1]] test_1 = Game_Of_Life(4,4, test_animals_less_than_two) self.assertEqual(test_1.next_animal_state(0,1), dead) # living and two live neighbors -> living test_animals_equals_two = [[0,0], [0,1], [0,2]] test_2 = Game_Of_Life(4,4, test_animals_equals_two) self.assertEqual(test_2.next_animal_state(0,1), living) # living and three live neighbors -> living test_animals_equals_three = [[0,0], [0,1], [0,2], [1,1]] test_2 = Game_Of_Life(4,4, test_animals_equals_three) self.assertEqual(test_2.next_animal_state(0,1), living) # living and more than three live neighbors -> dead test_animals_equals_four = [[0,0], [0,1], [0,2], [1,1], [1,0]] test_2 = Game_Of_Life(4,4, test_animals_equals_four) self.assertEqual(test_2.next_animal_state(0,1), dead) # dead and more than three live neighbors -> living test_animals_resurrection = [[0,0], [0,2], [1,1]] test_2 = Game_Of_Life(4,4, test_animals_resurrection) self.assertEqual(test_2.next_animal_state(0,1), living)
from game_of_life import Game_Of_Life Game_Of_Life().run()
def setUp(self): self.live_cells = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 2], [1, 3], [2, 0], [2, 1], [3, 1]] self.game = Game_Of_Life(4, self.live_cells)
class Test_Game(unittest.TestCase): def setUp(self): self.live_cells = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 2], [1, 3], [2, 0], [2, 1], [3, 1]] self.game = Game_Of_Life(4, self.live_cells) def test_create_empty_matrix(self): dead = self.game.dead_symbol matrix = [[dead, dead, dead, dead], [dead, dead, dead, dead], [dead, dead, dead, dead], [dead, dead, dead, dead]] self.assertEqual(self.game._create_empty_matrix(), matrix) def test_is_in_matrix(self): self.assertTrue(self.game._is_in_matrix([0, 0])) self.assertTrue(self.game._is_in_matrix([3, 3])) self.assertFalse(self.game._is_in_matrix([-1, 2])) def test_is_alive(self): dead = self.game.dead_symbol live = self.game.live_symbol matrix = [[live, dead, dead, dead], [dead, live, dead, dead], [dead, live, dead, dead], [live, dead, dead, dead]] self.assertTrue(self.game._is_alive([0, 0], matrix)) self.assertFalse(self.game._is_alive([0, 1], matrix)) def test_step_for_live(self): dead = self.game.dead_symbol live = self.game.live_symbol matrix = [[live, dead, dead, dead], [dead, live, dead, dead], [dead, live, dead, dead], [live, dead, dead, dead]] self.game._step_for_live([0, 0], matrix) self.assertFalse(self.game._is_alive([0, 0], matrix)) def test_step_for_dead(self): dead = self.game.dead_symbol live = self.game.live_symbol matrix = [[live, dead, live, dead], [dead, live, dead, dead], [dead, live, dead, dead], [live, dead, dead, dead]] self.game._step_for_dead([0, 1], matrix) self.assertTrue(self.game._is_alive([0, 1], matrix)) def test_is_game_over(self): self.assertTrue(self.game._is_game_over(self.live_cells)) self.assertFalse(self.game._is_game_over([[1, 1], [2, 2]]))
def test_init_size(self): g = Game_Of_Life(n=10) self.assertEqual(len(g.board), 10) self.assertEqual(len(g.board[0]), 10)
def test_assignment(self): g = Game_Of_Life(n=5) g.setAlive(1, 1) self.assertEqual(g.isAlive(1, 1), True)