|
| 1 | +import argparse |
1 | 2 | from time import gmtime, strftime |
| 3 | +from typing import cast |
2 | 4 |
|
3 | | -import args |
4 | | -from typing import cast, Union # noqa: F401 |
5 | | - |
6 | | -from base.grid import Grid |
7 | | -from base.distance_grid import DistanceGrid |
8 | 5 | from base.colored_grid import ColoredGrid |
| 6 | +from base.rotator import Rotator |
9 | 7 |
|
10 | 8 | import pathfinders.dijkstra as Dijkstra |
11 | 9 | import pathfinders.longest_path as LongestPath |
12 | 10 |
|
13 | | -from base.rotator import Rotator |
14 | | - |
15 | | -from demos.demo_utils import ALGORITHMS, get_exporter, get_rotations, get_algorithm, get_pathfinding |
| 11 | +from demos.demo_utils_v2 import ALGORITHM_NAMES, str2bool, available_algorithm, available_exporter |
16 | 12 |
|
17 | 13 |
|
18 | 14 | DEFAULT_EXPORTER = "PNGExporter" |
19 | 15 | AVAILABLE_EXPORTERS = ["PNGExporter"] |
20 | | - |
21 | | - |
22 | | -def get_coloring() -> bool: |
23 | | - return bool("--coloring" in args.flags) |
| 16 | +AVAILABLE_ALGORITHMS = ALGORITHM_NAMES |
24 | 17 |
|
25 | 18 |
|
26 | 19 | if __name__ == "__main__": |
27 | | - if len(args.all) < 3: |
28 | | - print("Usage:\nPYTHONPATH=. python3 demos/image_demo.py <rows> <columns> <algorithm> ", end="") |
29 | | - print("[--exporter=<exporter>] [--rotations=<rotations>] [--pathfinding] [--coloring]") |
30 | | - print("Valid algorithms: {}".format("|".join([algorithm.__name__ for algorithm in ALGORITHMS]))) |
31 | | - print("Valid exporters: {}".format("|".join(AVAILABLE_EXPORTERS))) |
32 | | - print("Rotations is an integer value measuring number of 90 degree clockwise rotations to perform") |
33 | | - print("Pathfinding flag shows distances between cells") |
34 | | - exit(1) |
35 | | - exporter, exporter_name = get_exporter(AVAILABLE_EXPORTERS, DEFAULT_EXPORTER) |
36 | | - rotations = get_rotations() |
37 | | - pathfinding = get_pathfinding() |
38 | | - rows = int(args.all[0]) |
39 | | - columns = int(args.all[1]) |
40 | | - algorithm = get_algorithm() |
41 | | - coloring = get_coloring() |
42 | | - print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format(algorithm.__name__, rows, columns, exporter_name)) |
| 20 | + parser = argparse.ArgumentParser(description="Render a maze") |
| 21 | + parser.add_argument("rows", type=int, help="number or rows") |
| 22 | + parser.add_argument("columns", type=int, help="number or columns") |
| 23 | + parser.add_argument("algorithm", type=str, help="algorithm to use") |
| 24 | + parser.add_argument("-e", "--exporter", type=str, default=DEFAULT_EXPORTER, help="maze exporter to use") |
| 25 | + parser.add_argument("-f", "--filename", type=str, default=None, help="file name to use") |
| 26 | + parser.add_argument("-r", "--rotations", type=int, default=0, |
| 27 | + help="number of 90 degree clockwise rotations to perform") |
| 28 | + parser.add_argument("-p", "--pathfinding", type=str2bool, default=False, help="whether solve the maze") |
| 29 | + parser.add_argument("-c", "--coloring", type=str2bool, help="whether to color the maze solution") |
| 30 | + args = parser.parse_args() |
| 31 | + |
| 32 | + rows = args.rows |
| 33 | + columns = args.columns |
| 34 | + algorithm = available_algorithm(args.algorithm, AVAILABLE_ALGORITHMS) |
| 35 | + exporter = available_exporter(args.exporter, AVAILABLE_EXPORTERS) |
| 36 | + filename = args.filename if args.filename else strftime("%Y%m%d%H%M%S", gmtime()) |
| 37 | + rotations = args.rotations |
| 38 | + pathfinding = args.pathfinding |
| 39 | + coloring = args.coloring |
| 40 | + print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format(args.algorithm, rows, columns, args.exporter)) |
43 | 41 | print("90deg Rotations: {}\nPathfinding: {}\nColoring: {}".format(rotations, pathfinding, coloring)) |
44 | 42 |
|
45 | | - # here coloring takes precedence, because ColoredGrid inherits from DistanceGrid |
46 | | - if coloring: |
47 | | - grid = ColoredGrid(rows, columns) # type: Union[Grid, DistanceGrid, ColoredGrid] |
48 | | - elif pathfinding: |
49 | | - grid = DistanceGrid(rows, columns) |
50 | | - else: |
51 | | - grid = Grid(rows, columns) |
| 43 | + # Always use Colored Grid. Just don"t color the output if colored == False |
| 44 | + grid = ColoredGrid(rows, columns) |
52 | 45 |
|
53 | | - grid = algorithm.on(grid) |
| 46 | + algorithm.on(grid) |
54 | 47 |
|
55 | 48 | for num in range(rotations): |
56 | | - grid = Rotator.on(grid) |
| 49 | + grid = cast(ColoredGrid, Rotator().on(grid)) |
| 50 | + |
| 51 | + # exporter.render(grid, coloring=coloring, filename=filename) |
57 | 52 |
|
58 | | - # here pathfinding first, so if also colored we'll see the route colored, else if colored will see all maze painted |
| 53 | + # here pathfinding first, so if also colored we"ll see the route colored, else if colored will see all maze painted |
59 | 54 | if pathfinding: |
60 | | - start_row, start_column, end_row, end_column = LongestPath.calculate(cast(DistanceGrid, grid)) |
61 | | - print("Solving maze from row {} column {} to row {} column {}".format( |
62 | | - start_row, start_column, end_row, end_column)) |
63 | | - grid = Dijkstra.calculate_distances(cast(DistanceGrid, grid), start_row, start_column, end_row, end_column) |
| 55 | + start, end = LongestPath.calculate(grid) |
| 56 | + print("Solving maze from row {} column {} to row {} column {}".format(*start, *end)) |
| 57 | + Dijkstra.calculate_distances(grid, start, end) |
64 | 58 | elif coloring: |
65 | | - start_row = round(grid.rows / 2) |
66 | | - start_column = round(grid.columns / 2) |
67 | | - print("Drawing colored maze with start row {} column {}".format(start_row, start_column)) |
68 | | - start_cell = grid.cell_at(start_row, start_column) |
69 | | - if start_cell is None: |
70 | | - raise IndexError("Invalid start cell row {} column {}".format(start_row, start_column)) |
71 | | - grid.distances = start_cell.distances # type: ignore |
72 | | - |
73 | | - filename = strftime("%Y%m%d%H%M%S", gmtime()) |
74 | | - |
75 | | - exporter.render(grid, coloring=coloring, filename=filename) |
| 59 | + starting_position = (round(grid.rows / 2), round(grid.columns / 2)) |
| 60 | + print("Drawing colored maze with start row {} column {}".format(*starting_position)) |
| 61 | + if grid[starting_position] is None: |
| 62 | + raise IndexError("Invalid start cell row {} column {}".format(*starting_position)) |
| 63 | + grid.distances = grid[starting_position].distances # type: ignore |
| 64 | + |
| 65 | + if coloring or pathfinding: |
| 66 | + exporter.render(grid, coloring=coloring, filename=filename) |
76 | 67 |
|
77 | 68 | print("Maze has {} dead-ends".format(len(grid.deadends))) |
0 commit comments