def on(grid: Grid) -> Grid:
        unvisited = []
        for cell in grid.each_cell():
            unvisited.append(cell)

        first_cell = choice(unvisited)
        unvisited.remove(first_cell)

        while len(unvisited) > 0:
            # start a walk
            cell = choice(unvisited)
            path = [cell]

            while cell in unvisited:
                cell = choice(cell.neighbors)
                try:
                    position = path.index(cell)
                    # already walked, perform loop-erase. e.g. A -> B -> C -> D -> B   becomes A -> B
                    path = path[:position + 1]
                except ValueError:
                    path.append(cell)

            # Passage carving once has found a valid path
            for index in range(len(path) - 1):
                path[index].link(path[index + 1])
                unvisited.remove(path[index])

        return grid
예제 #2
0
    def _render(grid: Grid, cell_size: int = 4, coloring: bool = False) -> Image:
        wall_color = (0, 0, 0)
        image_width = cell_size * grid.columns
        image_height = cell_size * grid.rows

        image = Image.new("RGBA", (image_width + 1, image_height + 1), (255, 255, 255))
        draw = ImageDraw.Draw(image)

        for draw_pass in range(2):
            for cell in grid.each_cell():
                x1 = cell.column * cell_size
                y1 = cell.row * cell_size
                x2 = (cell.column + 1) * cell_size
                y2 = (cell.row + 1) * cell_size

                if draw_pass == STEP_BACKGROUND and coloring:
                    if coloring:
                        color = cast(ColoredGrid, grid).background_color_for(cell)
                    else:
                        color = (255, 255, 255)
                    draw.rectangle((x1, y1, x2, y2), fill=color)
                else:
                    if not cell.north:
                        draw.line((x1, y1, x2, y1), fill=wall_color, width=1)
                    if not cell.west:
                        draw.line((x1, y1, x1, y2), fill=wall_color, width=1)
                    if not cell.linked_to(cell.east):
                        draw.line((x2, y1, x2, y2), fill=wall_color, width=1)
                    if not cell.linked_to(cell.south):
                        draw.line((x1, y2, x2, y2), fill=wall_color, width=1)

        return image
 def on(grid: Grid) -> Grid:
     for cell in grid.each_cell():
         neighbors = []
         if cell.north:
             neighbors.append(cell.north)
         if cell.east:
             neighbors.append(cell.east)
         if len(neighbors) > 0:
             cell.link(choice(neighbors))
     return grid
예제 #4
0
 def on(self, grid: Grid) -> None:
     for cell in grid.each_cell():
         neighbors = []
         if cell.north:
             neighbors.append(cell.north)
         if cell.east:
             neighbors.append(cell.east)
         if len(neighbors) > 0:
             neighbor = choice(neighbors)
             cell += neighbor
예제 #5
0
    def on(self, grid: Grid) -> None:
        current_cell: Optional[Cell] = grid.random_cell()

        while current_cell is not None:
            unvisited_neighbors = [neighbor for neighbor in current_cell.neighbors if len(neighbor.links) == 0]
            if len(unvisited_neighbors) > 0:
                # as int as there are unvisited paths, walk them
                neighbor = choice(unvisited_neighbors)
                current_cell += neighbor
                current_cell = neighbor
            else:
                # enter hunt mode, find first unvisited cell near any visited cell
                current_cell = None

                for cell in grid.each_cell():
                    visited_neighbors = [neighbor for neighbor in cell.neighbors if len(neighbor.links) > 0]
                    if len(cell.links) == 0 and len(visited_neighbors) > 0:
                        current_cell = cast(Cell, cell)  # outside of Mypy it's a mere assignment
                        neighbor = choice(visited_neighbors)
                        current_cell += neighbor
                        break
예제 #6
0
    def on(grid: Grid) -> Grid:
        current_cell = grid.random_cell()  # type: Optional[Cell]

        while current_cell is not None:
            unvisited_neighbors = \
                [neighbor for neighbor in current_cell.neighbors if len(neighbor.links) == 0]

            if len(unvisited_neighbors) > 0:
                # as long as there are unvisited paths, walk them
                neighbor = choice(unvisited_neighbors)
                current_cell.link(neighbor)
                current_cell = neighbor
            else:
                # enter hunt mode, find first unvisited cell near any visited cell
                current_cell = None
                for cell in grid.each_cell():
                    visited_neighbors = [neighbor for neighbor in cell.neighbors if len(neighbor.links) > 0]
                    if len(cell.links) == 0 and len(visited_neighbors) > 0:
                        current_cell = cast(Cell, cell)
                        neighbor = choice(visited_neighbors)
                        current_cell.link(neighbor)
                        break

        return grid