Skip to content

Commit

Permalink
Implemented Wilson's Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
89netraM committed Jun 2, 2021
1 parent ab4bc93 commit 32f2a51
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Here is a demonstration of mazes being generated with the various algorithms.
[Wikipedia](https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_division_method)
[Code](./src/map.rs#L287)

### Wilson's algorithm `--wilson`

![Animated demo of the algorithm](./animations/wilson.webp)
[Wikipedia](https://en.wikipedia.org/wiki/Maze_generation_algorithm#Wilson's_algorithm)
[Code](./src/map.rs#L360)

## Usage

```
Expand All @@ -48,6 +54,7 @@ FLAGS:
--prim Use Prim's algorithm for maze generation
--ab Use the Aldous-Broder algorithm for maze generation
--div Use the recursive division method for maze generation
--wilson Use Wilson's algorithm (loop-erased random walk) for maze generation
-h, --help Prints help information
OPTIONS:
Expand Down
Binary file added animations/wilson.webp
Binary file not shown.
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,19 @@ fn main() {
.help("Use the recursive division method for maze generation")
.display_order(9),
)
.arg(
Arg::with_name("WILSON")
.long("wilson")
.help("Use Wilson's algorithm (loop-erased random walk) for maze generation")
.display_order(9),
)
.group(ArgGroup::with_name("ALGORITHM").args(&[
"DFS",
"TREE",
"PRIM",
"AB",
"DIV",
"WILSON",
]))
.get_matches();

Expand Down Expand Up @@ -158,6 +165,8 @@ fn main() {
Map::generate_ab(rows, columns, start_pos, initial_peek_fn, peek_fn)
} else if matches.is_present("DIV") {
Map::generate_div(rows, columns, initial_peek_fn, peek_fn)
} else if matches.is_present("WILSON") {
Map::generate_wilson(rows, columns, start_pos, initial_peek_fn, peek_fn)
} else {
Map::generate_dfs(rows, columns, start_pos, initial_peek_fn, peek_fn)
};
Expand Down
51 changes: 51 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,57 @@ impl Map {
map
}

pub fn generate_wilson<F, G>(rows: usize, columns: usize, start: Position, mut initial_peek: F, mut peek: G) -> Map
where
F: FnMut(&Map),
G: FnMut(&Map, &Position, &Direction),
{
let mut map = Map::new(rows, columns);
initial_peek(&map);
let mut rng = thread_rng();

let mut in_map = HashSet::new();
in_map.insert(start);
let mut unvisited: Vec<_> = (0..rows)
.flat_map(|r| (0..columns).filter_map(move |c| Some(Position(r, c)).filter(|p| p != &start)))
.collect();

let mut path: Vec<(Position, Direction)> = Vec::new();
let mut moved_positions = Vec::with_capacity(4);
while !unvisited.is_empty() {
let mut current = unvisited[rng.gen_range(0, unvisited.len())];

while !in_map.contains(&current) {
moved_positions.clear();
moved_positions.extend(
DIRECTIONS
.iter()
.filter_map(|d| map.move_in_direction(&current, d).map(|m| (m, d))),
);
if let Some((next, direction)) = moved_positions.choose(&mut rng) {
if let Some(index) = path.iter().position(|p| &p.0 == next) {
for (p, d) in path.drain(index..).rev() {
map.set(&p, &d, true);
peek(&map, &p, &d);
}
} else {
map.set(&current, direction, false);
peek(&map, &current, direction);
path.push((current, **direction));
}
current = *next;
}
}

for (p, _) in path.drain(..) {
in_map.insert(p);
unvisited.swap_remove(unvisited.iter().position(|u| u == &p).unwrap());
}
}

map
}

pub fn set_above(&mut self, pos: &Position, closed: bool) {
self.set_below(&Position(pos.0 - 1, pos.1), closed);
}
Expand Down

0 comments on commit 32f2a51

Please sign in to comment.