Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
20 lines (16 sloc) 429 Bytes
open class RecursiveBacktracker : Algorithm {
public init() { }
open func applyTo(_ grid: Grid) {
var stack = [ grid.sample() ]
while (stack.count > 0) {
let cell = stack.last!
let neighbors = cell.neighbors.filter({$0.links.isEmpty})
if let neighbor = neighbors.sample() {
cell.linkWith(neighbor)
stack.append(neighbor)
} else {
stack.popLast()
}
}
}
}