Skip to content

Commit 1bec326

Browse files
committed
feat: Add loading from file
1 parent 8111bcc commit 1bec326

5 files changed

Lines changed: 121 additions & 5 deletions

File tree

game/main.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,31 @@
55
app = typer.Typer()
66

77

8-
@app.command()
9-
def main(map_grid: str = None):
8+
@app.callback(invoke_without_command=True)
9+
def main(
10+
ctx: typer.Context,
11+
map_grid: str = typer.Option(
12+
None,
13+
"--map-grid",
14+
"-m",
15+
help="Load a map from a string representation",
16+
),
17+
):
18+
if ctx.invoked_subcommand is not None:
19+
return
20+
1021
game_map = Map()
1122
if map_grid is not None:
1223
game_map.load_from_str(map_grid)
1324
game_map.run()
1425

1526

27+
@app.command()
28+
def from_file(file_path: str):
29+
game_map = Map()
30+
game_map.load_from_file(file_path)
31+
game_map.run()
32+
33+
1634
if __name__ == "__main__":
1735
app()

game/tests/cli.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from game.utils.map import Map
2+
3+
4+
def test_from_arg():
5+
game_map = Map()
6+
game_map.load_from_str("..... .### # ....# .")
7+
8+
assert game_map.map == [
9+
[False for _ in range(5)],
10+
[False] + [True for _ in range(3)] + [False],
11+
[True] + [False for _ in range(4)],
12+
[False for _ in range(4)] + [True],
13+
[False for _ in range(5)],
14+
]
15+
16+
17+
def test_from_file():
18+
game_map = Map()
19+
game_map.load_from_file("game/tests/test_map.txt")
20+
21+
assert game_map.map == [
22+
[False for _ in range(5)],
23+
[False] + [True for _ in range(3)] + [False],
24+
[True] + [False for _ in range(4)],
25+
[False for _ in range(4)] + [True],
26+
[False for _ in range(5)],
27+
]
28+
29+
30+
def test_from_arg_and_from_file_are_identical():
31+
game_map_from_arg = Map()
32+
game_map_from_arg.load_from_str("..... .### # ....# .")
33+
34+
game_map_from_file = Map()
35+
game_map_from_file.load_from_file("game/tests/test_map.txt")
36+
37+
assert game_map_from_arg.map == game_map_from_file.map

game/tests/test_map.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.....
2+
.###
3+
#
4+
....#
5+
.

game/utils/map.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,27 @@ def __str__(self) -> str:
9494
def load_from_str(self, map_grid: str):
9595
"""
9696
Load the map from a string.
97-
Use space for line breaks and # for alive cells.
97+
Use space for end of row, . for dead cells and # for alive cells.
9898
"""
9999
rows = map_grid.split(" ")
100+
self.load_from_rows(rows)
101+
102+
def load_from_file(self, file_path: str):
103+
"""
104+
Load the map from a file.
105+
Use line breaks for end of row, . for dead cells and # for alive cells.
106+
"""
107+
with open(file_path) as file:
108+
map_grid = file.readlines()
109+
self.load_from_rows(map_grid)
110+
111+
def load_from_rows(self, rows: list[str]):
100112
self.number_of_rows = len(rows)
101-
self.number_of_columns = len(rows[0])
113+
self.number_of_columns = len(rows[0].strip())
102114
self.map = [[False for _ in range(self.number_of_columns)] for _ in range(self.number_of_rows)]
103115
for y, line in enumerate(rows):
104-
for x, char in enumerate(line):
116+
for x, char in enumerate(line.strip()):
117+
print(x, y, char, line)
105118
if char == "#":
106119
self.map[y][x] = True
107120
elif char != ".":

input.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
...................
2+
.
3+
.
4+
.
5+
...###.#..
6+
..##...##.
7+
..#.##..###
8+
.#.##
9+
.
10+
.
11+
.
12+
.
13+
.###....###
14+
...##....##
15+
.
16+
.####
17+
.####
18+
.###.###.###.###
19+
.##.....###
20+
.
21+
....#
22+
...#.#
23+
....#
24+
.
25+
.
26+
.....###
27+
....##.##
28+
.....###
29+
.
30+
.
31+
.....#
32+
..##.#.#.##
33+
...#..#..#
34+
..##.#.#.##
35+
.....#
36+
.
37+
.###
38+
.
39+
........#######
40+
.
41+
.
42+
.
43+
.

0 commit comments

Comments
 (0)