class Player: def __init__(self, iterations=100): self.__iterations = iterations self.__board = Board() self.__initialised = False def initialise_board(self, filename): self.__board.read_from_file(filename) self.__initialised = True def set_board(self, board: Board): self.__board = board def get_board(self): return self.__board def play(self, verbose=True): if self.__initialised: for _ in range(0, self.__iterations): if verbose: print(self.__board) self.__board.update_board() else: print(f'The board must be initialised first')
def test_draw_board(self): board = Board(2, 2, live_cells=[(0, 1)]) live_cell_character = 'X' dead_cell_character = 'O' results = drawer.Drawer( live_cell_character=live_cell_character, dead_cell_character=dead_cell_character).draw(board) self.assertEqual(results, 'O O\nX O')
def test_overpopulation(self): init_config = [(0, 1), (1, 0), (1, 1), (1, 2), (2, 1)] board = Board(3) board.set_alive_cells(init_config) board.update() states = board.list_of_values self.assertListEqual(states, [ [1, 1, 1], [1, 0, 1], [1, 1, 1] ])
def test_simple_update2(self): init_config = [(0, 0), (0, 1), (0, 2)] board = Board(3) board.set_alive_cells(init_config) board.update() states = board.list_of_values self.assertListEqual(states, [ [0, 1, 0], [0, 1, 0], [0, 0, 0] ])
def test_simple_update(self): alive_cells = [(0, 0), (1, 1), (0, 1)] board = Board(3) board.set_alive_cells(alive_cells) board.update() states = board.list_of_values self.assertListEqual(states, [ [1, 1, 0], [1, 1, 0], [0, 0, 0] ])
def main(): """Console script for game_of_life.""" parser = argparse.ArgumentParser() parser.add_argument('rows', type=int, help="Number of rows in the board") parser.add_argument('cols', type=int, help="Number of cols in the board") args = parser.parse_args() rows = args.rows cols = args.cols player = Player(int(1e3)) board = Board(rows, cols) player.set_board(board) root = Tk() game_gui = GUI(root, player, board) root.mainloop()
def load(self): """Gets the raw string board using `self.input_function` and creates a `Board`.""" lines = self._get_lines_from_input() if not lines: raise errors.InvalidBoardError x_length, y_length = self._get_board_dimension_from_lines(lines) live_cells = [] for y, line in enumerate(lines): values = self._get_values(line) if len(values) != x_length: raise errors.InvalidBoardError for x, value in enumerate(values): if self._is_live_cell(value): live_cells.append((x, y)) return Board(x_length, y_length, live_cells=live_cells)
def test_create_empty_board_and_check_location(self): board = Board(1, 1) self.assertEqual(board[0, 0], False)
def test_equal(self): board_1 = Board(1, 1) board_2 = Board(1, 1, live_cells=[(0, 0)]) board_3 = Board(1, 1) self.assertNotEqual(board_1, board_2) self.assertEqual(board_1, board_3)
def _create_board_and_step(self, *args, **kwargs): board = Board(*args, **kwargs) board.step() return board
def test_cant_create_live_cells_out_of_bounds(self): for invalid_cell in [(1, 0), (0, 1), (-1, 0), (0, -1)]: with self.assertRaises(errors.InvalidBoardError): Board(1, 1, live_cells=[invalid_cell])
def test_board_with_valid_cell(self): board = Board(1, 2, live_cells=[(0, 1)]) self.assertEqual(board[0, 1], True)
def test_load_from_input(self): board = self._create_board_with_mock_input_function("0 0 0 0\n0 1 0 0") self.assertEqual( board, Board(4, 2, live_cells=[(1, 1)]) )
def create_test_board(size): board = Board(size) board[0, 0].state = ALIVE board[0, 1].state = ALIVE board[2, 1].state = ALIVE return board
def _create_board_and_step(self, *args, **kwargs): board = Board(*args, **kwargs) board.step() return board
def __init__(self, iterations=100): self.__iterations = iterations self.__board = Board() self.__initialised = False
def setUpClass(cls): cls.emptyBoard = Board() cls.board = Board(10, 10)