def on(self, grid: Grid) -> None:
        unvisited = []
        for cell in grid.eachCell():
            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 = cell.randomNeighbour()  # type: ignore
                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)
                self.step()

            # Passage carving once has found a valid path
            for index in range(len(path) - 1):
                path[index] += (path[index + 1])
                unvisited.remove(path[index])
                self.step()
 def _prepareLogGrid(self, grid: Grid) -> None:
     ''' Prepare the grid for logging the algorithm '''
     if not self.log: return
     # 'visit' : List of steps on which the cell was visited
     # 'links' : Directions of the links made by the cell (NOT to the cell)
     data = {'visit': [], 'links': []}  # type: Dict
     key = self.name  # type: str
     for cell in grid.eachCell():
         cell.data[key] = data
Ejemplo n.º 3
0
    def on(self, grid: Grid) -> Grid:
        grid_type = type(grid)

        # row i becomes col n-i when rotating 90 degrees clockwise
        rotated_grid = grid_type(rows=grid.cols, cols=grid.rows)

        for old_cell in grid.eachCell():
            row, column = self._rotated_coordinates(old_cell, rotated_grid)
            new_cell = rotated_grid[row, column]
            if new_cell is None:
                raise IndexError('Cell not found at row {} column {}'.format(
                    row, column))
            self._rotate_cell_neighbors(new_cell, old_cell, rotated_grid)

        return rotated_grid
Ejemplo n.º 4
0
    def on(self, grid: Grid) -> None:
        self._prepareLogGrid(grid)

        for cell in grid.eachCell():
            self._logVisit(cell)

            neighbours = []
            if cell.north: neighbours.append(cell.north)
            if cell.east:  neighbours.append(cell.east)

            if len(neighbours) > 0:
                neighbour = choice(neighbours)
                cell += neighbour
                self._logLink(cell, neighbour)

            self.step()
    def _render(grid: Grid, cell_size: int=4, coloring: bool=False) -> Image:
        ''' Rendering core '''
        wall_color = (0, 0, 0)
        image = Image.new('RGBA', (cell_size * grid.cols + 1, cell_size * grid.rows + 1), (255, 255, 255))
        draw = ImageDraw.Draw(image)

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

                if draw_pass == 0:
                    color = grid.color(cell) if coloring else (255, 255, 255)  # type: ignore
                    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 & cell.east:  draw.line((x2, y1, x2, y2), fill=wall_color, width=1)
                    if not cell & cell.south: draw.line((x1, y2, x2, y2), fill=wall_color, width=1)
        return image