diff --git a/game/main.py b/game/main.py index 4e76466..85a46c0 100644 --- a/game/main.py +++ b/game/main.py @@ -5,13 +5,31 @@ app = typer.Typer() -@app.command() -def main(map_grid: str = None): +@app.callback(invoke_without_command=True) +def main( + ctx: typer.Context, + map_grid: str = typer.Option( + None, + "--map-grid", + "-m", + help="Load a map from a string representation", + ), +): + if ctx.invoked_subcommand is not None: + return + game_map = Map() if map_grid is not None: game_map.load_from_str(map_grid) game_map.run() +@app.command() +def from_file(file_path: str): + game_map = Map() + game_map.load_from_file(file_path) + game_map.run() + + if __name__ == "__main__": app() diff --git a/game/tests/cli.py b/game/tests/cli.py new file mode 100644 index 0000000..f1ddeca --- /dev/null +++ b/game/tests/cli.py @@ -0,0 +1,37 @@ +from game.utils.map import Map + + +def test_from_arg(): + game_map = Map() + game_map.load_from_str("..... .### # ....# .") + + assert game_map.map == [ + [False for _ in range(5)], + [False] + [True for _ in range(3)] + [False], + [True] + [False for _ in range(4)], + [False for _ in range(4)] + [True], + [False for _ in range(5)], + ] + + +def test_from_file(): + game_map = Map() + game_map.load_from_file("game/tests/test_map.txt") + + assert game_map.map == [ + [False for _ in range(5)], + [False] + [True for _ in range(3)] + [False], + [True] + [False for _ in range(4)], + [False for _ in range(4)] + [True], + [False for _ in range(5)], + ] + + +def test_from_arg_and_from_file_are_identical(): + game_map_from_arg = Map() + game_map_from_arg.load_from_str("..... .### # ....# .") + + game_map_from_file = Map() + game_map_from_file.load_from_file("game/tests/test_map.txt") + + assert game_map_from_arg.map == game_map_from_file.map diff --git a/game/tests/test_map.txt b/game/tests/test_map.txt new file mode 100644 index 0000000..557cd49 --- /dev/null +++ b/game/tests/test_map.txt @@ -0,0 +1,5 @@ +..... +.### +# +....# +. \ No newline at end of file diff --git a/game/utils/map.py b/game/utils/map.py index b12a17f..5e93ba3 100644 --- a/game/utils/map.py +++ b/game/utils/map.py @@ -94,14 +94,27 @@ def __str__(self) -> str: def load_from_str(self, map_grid: str): """ Load the map from a string. - Use space for line breaks and # for alive cells. + Use space for end of row, . for dead cells and # for alive cells. """ rows = map_grid.split(" ") + self.load_from_rows(rows) + + def load_from_file(self, file_path: str): + """ + Load the map from a file. + Use line breaks for end of row, . for dead cells and # for alive cells. + """ + with open(file_path) as file: + map_grid = file.readlines() + self.load_from_rows(map_grid) + + def load_from_rows(self, rows: list[str]): self.number_of_rows = len(rows) - self.number_of_columns = len(rows[0]) + self.number_of_columns = len(rows[0].strip()) self.map = [[False for _ in range(self.number_of_columns)] for _ in range(self.number_of_rows)] for y, line in enumerate(rows): - for x, char in enumerate(line): + for x, char in enumerate(line.strip()): + print(x, y, char, line) if char == "#": self.map[y][x] = True elif char != ".":