def _render(grid: ColoredGrid, cell_size: int = 4, coloring: bool = False) -> Image: ''' Rendering core ''' cs = cell_size arr = np.zeros((cs * grid.rows + 2, cs * grid.cols + 2, 3)) # Outermost walls arr[:, 0, 0:3] = 0 # noqa: E201, E203 arr[:, -1, 0:3] = 0 # noqa: E201, E203 arr[0, :, 0:3] = 0 # noqa: E201, E203 arr[-1, :, 0:3] = 0 # noqa: E203 # Draw each cell for ri, row in enumerate(grid.eachRow()): for ci, cell in enumerate(row): # Figure out the color if len(cell.links) > 0: color = grid.color(cell) if coloring else (255, 255, 255) if color is None: color = (0, 0, 0) else: color = (0, 0, 0) # no links therefore color cell # The entire cell rp, cp = ri * cs + 1, ci * cs + 1 arr[rp:rp + cs, cp:cp + cs, 0] = color[0] arr[rp:rp + cs, cp:cp + cs, 1] = color[1] arr[rp:rp + cs, cp:cp + cs, 2] = color[2] # Corners of the cell arr[rp, cp, 0:3] = 0 # noqa: E203, E226 arr[rp + cs - 1, cp, 0:3] = 0 # noqa: E203, E226 arr[rp, cp + cs - 1, 0:3] = 0 # noqa: E203, E226 arr[rp + cs - 1, cp + cs - 1, 0:3] = 0 # noqa: E203, E226 # Walls if not cell & cell.north: arr[rp, cp + 1:cp + cs - 1, 0:3] = 0 # noqa: E203, E226 if not cell & cell.south: arr[rp + cs - 1, cp + 1:cp + cs - 1, 0:3] = 0 # noqa: E203, E226 if not cell & cell.west: arr[rp + 1:rp + cs - 1, cp, 0:3] = 0 # noqa: E203, E226 if not cell & cell.east: arr[rp + 1:rp + cs - 1, cp + cs - 1, 0:3] = 0 # noqa: E203, E226 return Image.fromarray(np.uint8(arr))
def is_valid(grid: ColoredGrid) -> bool: """ Wolf3D exit wall cell is a switch that only gets rendered east and west """ _, _, end_row, end_column = LongestPath.calculate(grid) cell = grid.cell_at(end_row, end_column) if cell is None: raise ValueError("Ending row not found at row {} column {}".format( end_row, end_column)) linked_neighbor = cell.links[0] # assume exactly one path to the exit return (cell.east is not None and cell.east == linked_neighbor) or \ (cell.west is not None and cell.west == linked_neighbor)
def _expand(grid: ColoredGrid, rows: int, columns: int) -> ColoredGrid: new_grid = ColoredGrid(rows, columns) for cell in grid.each_cell(): new_grid.set_cell_at(cell.row, cell.column, cell) return new_grid
parser.add_argument("-c", "--coloring", type=str2bool, help="whether to color the maze solution") args = parser.parse_args() rows = args.rows columns = args.columns algorithm = get_algorithm(args.algorithm, AVAILABLE_ALGORITHMS) exporter = get_exporter(args.exporter, AVAILABLE_EXPORTERS) filename = args.filename if args.filename else strftime("%Y%m%d%H%M%S", gmtime()) rotations = args.rotations pathfinding = args.pathfinding coloring = args.coloring print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format(args.algorithm, rows, columns, args.exporter)) print("90deg Rotations: {}\nPathfinding: {}\nColoring: {}".format(rotations, pathfinding, coloring)) # Always use Colored Grid. Just don"t color the output if colored == False grid = ColoredGrid(rows, columns) algorithm.on(grid) for num in range(rotations): grid = cast(ColoredGrid, Rotator().on(grid)) # here pathfinding first, so if also colored we"ll see the route colored, else if colored will see all maze painted if pathfinding: start, end = LongestPath.calculate(grid) print("Solving maze from row {} column {} to row {} column {}".format(*start, *end)) Dijkstra.calculate_distances(grid, start, end) elif coloring: starting_position = (round(grid.rows / 2), round(grid.columns / 2)) print("Drawing colored maze with start row {} column {}".format(*starting_position)) if grid[starting_position] is None:
print( "Rotations is an integer value measuring number of 90 degree clockwise rotations to perform" ) exit(1) exporter, exporter_name = get_exporter(AVAILABLE_EXPORTERS, DEFAULT_EXPORTER) pathfinding = True rows = int(args.all[0]) columns = int(args.all[1]) algorithm = get_algorithm() print("Algorithm: {}\nRows: {}\ncolumns: {}\nExporter: {}".format( algorithm.__name__, rows, columns, exporter_name)) valid_map = False while not valid_map: grid = ColoredGrid(rows, columns) grid = algorithm.on(grid) valid_map = exporter.is_valid(grid) if not valid_map: print( "Generated maze has no east/west linked ending cell. Recreating..." ) filename = strftime("%d%H%M%S", gmtime()) exporter.render(grid, filename=filename) print("Generated maze map has {} enemies".format(exporter.enemies_count)) grid = store_solution(grid) PNGExporter().render(grid, coloring=True, filename=filename)
def _expand(grid: ColoredGrid, rows: int, cols: int) -> ColoredGrid: new_grid = ColoredGrid(rows, cols) for cell in grid.eachCell(): new_grid[cell.row, cell.col] = cell return new_grid