Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions game/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
37 changes: 37 additions & 0 deletions game/tests/cli.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions game/tests/test_map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.....
.###
#
....#
.
19 changes: 16 additions & 3 deletions game/utils/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ".":
Expand Down