def on(self, grid: Grid) -> None: if self.starting_cell is None: self.starting_cell = grid.random_cell() if not is_cell(self.starting_cell) or self.starting_cell not in grid: ValueError( "Starting point of the algorithm must be a valid cell in the grid" ) # We'll use the list as a stack to do very easily any backtracking walked_path = [] walked_path.append(self.starting_cell) while len(walked_path) > 0: current_cell = walked_path[-1] unvisited_neighbors = [ neighbor for neighbor in current_cell.neighbors if len(neighbor.links) == 0 ] if len(unvisited_neighbors) == 0: walked_path.pop() else: neighbor = choice(unvisited_neighbors) current_cell += neighbor walked_path.append(neighbor)
def test_random_cell() -> None: grid = Grid(2, 2) for _ in range(100): assert grid.random_cell() in [ Cell(0, 0), Cell(0, 1), Cell(1, 0), Cell(1, 1) ]
def on(self, grid: Grid) -> None: current_cell = grid.random_cell() unvisited_count = grid.size - 1 while unvisited_count > 0: neighbor = current_cell.random_neighbour() if neighbor is None: raise ValueError("Aldous-Broder algorithm needs all cells to have at least one neighbor") if len(neighbor.links) == 0: current_cell += neighbor unvisited_count -= 1 current_cell = neighbor
def on(grid: Grid) -> Grid: current_cell = grid.random_cell() unvisited_count = grid.size - 1 while unvisited_count > 0: neighbor = choice(current_cell.neighbors) if neighbor is None: raise ValueError( "Aldous-Broder algorithm needs all cells to have at least one neighbor" ) if len(neighbor.links) == 0: current_cell.link(neighbor) unvisited_count -= 1 current_cell = neighbor return grid
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
def on(grid: Grid, starting_cell: Optional[Cell] = None) -> Grid: if starting_cell is None: starting_cell = grid.random_cell() # We'll use the list as a stack to do very easily any backtracking walked_path = [] walked_path.append(starting_cell) while len(walked_path) > 0: current_cell = walked_path[-1] unvisited_neighbors = [ neighbor for neighbor in current_cell.neighbors if len(neighbor.links) == 0 ] if len(unvisited_neighbors) == 0: walked_path.pop() else: neighbor = choice(unvisited_neighbors) current_cell.link(neighbor) walked_path.append(neighbor) return grid
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