-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazes.py
81 lines (69 loc) · 2.51 KB
/
mazes.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import typer
from generators.binary_tree import BinaryTree
from generators.sidewinder import Sidewinder
from generators.generator import Generator
from core.grid import Grid
from core.colored_grid import ColoredGrid
from core.distance_grid import DistanceGrid
app = typer.Typer()
# class Algorithm(Enum):
def get_generator(algorithm: str) -> Generator:
"""
Converts string to an actual generator.
TODO: Should maybe move this to generators as a factory.
"""
match algorithm:
case ["BinaryTree"]:
return BinaryTree
case ["Sidewinder"]:
return Sidewinder
case _:
return BinaryTree
def get_grid(grid: str, width: int, height: int) -> Grid:
match grid:
case ["colored", _]:
typer.echo("Using colored grid")
return ColoredGrid(height, width)
case ["distance", _]:
return DistanceGrid(height, width)
case _:
return Grid(width, height)
@app.command()
def generate(
generator: str = "BinaryTree",
grid: str = "ColoredGrid",
width: int = 50,
height: int = 50,
out: str = "results/output.png",
) -> None:
"""Entrypoint for the mazes cli interface
Allows you to generate various mazes with a set of various options.
By default generates a 50x50 maze, generated by the binary tree algorithm.
TODO: Add support to select a renderer.
TODO: Add support to select a color scheme.
"""
generator_class = get_generator(generator)
grid_class = get_grid(grid.lower(), width=width, height=height)
# grid = Grid(width, height)
# TODO:
# This obviously beats the point and will always generate a colored grid.
# I could do a if type = ColoredGrid and then call grid_class.distances
# but that's still not great.
#
# What I probably should do is either:
# - add a calculate_distances(from: (x,y)) method to grid, that is only implemented
# in classes that have a distances() method
# - add a origin in to_png() and calculate the distances in coloredgrid where
# coloredgrid overrides to_png(). This feels weird though,
# since it has nothing to do with png generation I'll also make a
# Renderer for png.
#
# Postponing it for now.
grid_class = ColoredGrid(height, width)
generator_class.on(grid_class)
start = grid_class[0][0]
grid_class.distances(start.distances())
image = grid_class.to_png()
image.save(out)
if __name__ == "__main__":
typer.run(generate)