class TestCgolMethods(unittest.TestCase): 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_created_grid(self): np.testing.assert_array_equal(self.g.get_grid(), np.zeros((self.grid_width, self.grid_height))) def test_pattern_placement(self): self.g.add_object(self.conf.Beacon, 0, 0) self.g.add_object(self.conf.Block, 10, 10) test_grid = np.zeros((self.grid_width, self.grid_height)) test_grid[0:4, 0:4] = np.array([[1, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1]]) test_grid[10:12, 10:12] = np.array([[1, 1], [1, 1]]) np.testing.assert_array_equal(self.g.get_grid(), test_grid) def test_still_life(self): self.g.add_object(self.conf.Block, 0, 0) self.g.add_object(self.conf.Beehive, 6, 0) self.g.add_object(self.conf.Tub, 6, 12) self.g.update_grid() test_grid = np.zeros((self.grid_width, self.grid_height)) test_grid[0:2, 0:2] = np.array([[1, 1], [1, 1]]) test_grid[6:9, 0:4] = np.array([[0, 1, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0]]) test_grid[6:9,12:15] = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) np.testing.assert_array_equal(self.g.get_grid(), test_grid) def test_oscillators(self): self.g.add_object(self.conf.Blinker, 0, 0) self.g.add_object(self.conf.Toad, 6, 0) self.g.add_object(self.conf.Beacon, 6, 12) self.g.update_grid() test_grid = np.zeros((self.grid_width, self.grid_height)) test_grid[0:3, 0:3] = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]]) test_grid[6:10, 0:4] = np.array([[0, 0, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 0, 0]]) test_grid[6:10,12:16] = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]]) np.testing.assert_array_equal(self.g.get_grid(), test_grid) def test_spaceships(self): self.g.add_object(self.conf.Glider, 0, 0) self.g.add_object(self.conf.LWSpaceship, 12, 5) self.g.update_grid() test_grid = np.zeros((self.grid_width, self.grid_height)) test_grid[0:3, 1:4] = np.array([[1, 0, 0], [0, 1, 1], [1, 1, 0]]) test_grid[12:16, 5:10] = np.array([[0, 1, 1, 1, 1], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [1, 0, 0, 1, 0]]) np.testing.assert_array_equal(self.g.get_grid(), test_grid)
input("Please enter the maximum number of iterations (int): ")) max_iterations = np.clip(max_iterations, 10, 1000) for _ in range(20): print("\nEnter the key to the configuration you want to add :") print( "\n0 Start Game\t1 Block \n2 Beehive\t3 Tub\n4 Blinker\t5 Toad\n6 Beacon\t7 Glider\n8 Light Space Ship\t9 Random Grid" ) selection = int(input("\nYour Choice: ")) if selection == 9: g.random_grid() else: i, j = map( int, input( "Please enter the location for the pattern as two space-seperated integers: " ).split()) g.add_object(conf.d[selection], i, j) response = input('\n Do you want to add more configurations? Y/N') if response != 'Y' or response != 'y': break print( "Press Ctrl+C at any point to exit. The program shall automatically exit after the maximum iterations." ) for counter in range(max_iterations): g.run_once() time.sleep(t / 1000) g.move_cursor_down() print("\n **************** THANKS FOR PLAYING! ********************")