pub fn random_walk(maze: &mut Maze, index: usize) -> Vec<usize> {
    let mut rng = thread_rng();
    if index >= maze.maze.len() { return vec![] }
    let mut cover: Vec<i32> = maze.maze.iter().map(|x| if *x == 0 { 0 } else { -1 }).collect();
    let mut walk: Vec<usize> = vec![index];
    let mut counter: i32 = 2;
    cover[index] = 1;
    let mut dirs: Vec<i32> = vec![-1, 1, -(maze.width as i32), maze.width as i32];
    // Random Walk
    'outer: loop {
        let pos = *walk.last().unwrap() as i32;
        if dirs.iter().map(|x| (pos + *x) as usize).filter(|x| *x < maze.maze.len() && maze.maze[*x] > 0).count() > 0 {
            break 'outer; // Found existing part of the maze, random walk is done
        }
        dirs.shuffle(&mut rng);
        for d in dirs.iter() {
            let di = (*d + pos) as usize;
            if di < maze.maze.len() && cover[di] == 0 && maze.maze[di] == 0 {
                walk.push(di);
                cover[di] = counter;
                counter += 1;
                continue 'outer;
            }
        }
        walk.pop(); // Backtrack if dead end
        if walk.len() == 0 { return vec![]; }
    }
    // Reconstruct the path
    let mut end: usize = *walk.last().unwrap();
    walk.clear();
    walk.push(end);
    while cover[end] > 1 {
        end = dirs.iter().fold(end, |x, di| {
            let d = (di + end as i32) as usize;
            if cover[d] > 0 && cover[d] < cover[x] { d } else { x }
        });
        walk.push(end);
    }
    walk
}
// This is a implementation of the Wilson (random walk) maze algorithm that has been
// modified to include backtacking and handle already existing rooms
This is a implementation of the Wilson (random walk) maze algorithm that has been
modified to include backtacking and handle already existing rooms
This is a implementation of the Wilson ( random walk ) maze algorithm that has been modified to include backtacking and handle already existing rooms
pub fn random_walk(maze: &mut Maze, index: usize) -> Vec<usize> {
    let mut rng = thread_rng();
    if index >= maze.maze.len() { return vec![] }
    let mut cover: Vec<i32> = maze.maze.iter().map(|x| if *x == 0 { 0 } else { -1 }).collect();
    let mut walk: Vec<usize> = vec![index];
    let mut counter: i32 = 2;
    cover[index] = 1;
    let mut dirs: Vec<i32> = vec![-1, 1, -(maze.width as i32), maze.width as i32];
    // Random Walk
    'outer: loop {
        let pos = *walk.last().unwrap() as i32;
        if dirs.iter().map(|x| (pos + *x) as usize).filter(|x| *x < maze.maze.len() && maze.maze[*x] > 0).count() > 0 {
            break 'outer; // Found existing part of the maze, random walk is done
        }
        dirs.shuffle(&mut rng);
        for d in dirs.iter() {
            let di = (*d + pos) as usize;
            if di < maze.maze.len() && cover[di] == 0 && maze.maze[di] == 0 {
                walk.push(di);
                cover[di] = counter;
                counter += 1;
                continue 'outer;
            }
        }
        walk.pop(); // Backtrack if dead end
        if walk.len() == 0 { return vec![]; }
    }
    // Reconstruct the path
    let mut end: usize = *walk.last().unwrap();
    walk.clear();
    walk.push(end);
    while cover[end] > 1 {
        end = dirs.iter().fold(end, |x, di| {
            let d = (di + end as i32) as usize;
            if cover[d] > 0 && cover[d] < cover[x] { d } else { x }
        });
        walk.push(end);
    }
    walk
}
pub fn random_walk ( maze : & mut Maze , index : usize ) -> Vec < usize > { let mut rng = thread_rng ( ) ; if index >= maze . maze . len ( ) { return vec ! [ ] } let mut cover : Vec < i32 > = maze . maze . iter ( ) . map ( | x | if * x == 0 { 0 } else { - 1 } ) . collect ( ) ; let mut walk : Vec < usize > = vec ! [ index ] ; let mut counter : i32 = 2 ; cover [ index ] = 1 ; let mut dirs : Vec < i32 > = vec ! [ 1 1 ( maze width as i32 ) maze width as i32 ] ; // Random Walk ' outer : loop { let pos = * walk . last ( ) . unwrap ( ) as i32 ; if dirs . iter ( ) . map ( | x | ( pos + * x ) as usize ) . filter ( | x | * x < maze . maze . len ( ) && maze . maze [ * x ] > 0 ) . count ( ) > 0 { break ' outer ; // Found existing part of the maze, random walk is done } dirs . shuffle ( & mut rng ) ; for d in dirs . iter ( ) { let di = ( * d + pos ) as usize ; if di < maze . maze . len ( ) && cover [ di ] == 0 && maze . maze [ di ] == 0 { walk . push ( di ) ; cover [ di ] = counter ; counter += 1 ; continue ' outer ; } } walk . pop ( ) ; // Backtrack if dead end if walk . len ( ) == 0 { return vec ! [ ] ; } } // Reconstruct the path let mut end : usize = * walk . last ( ) . unwrap ( ) ; walk . clear ( ) ; walk . push ( end ) ; while cover [ end ] > 1 { end = dirs . iter ( ) . fold ( end , | x , di | { let d = ( di + end as i32 ) as usize ; if cover [ d ] > 0 && cover [ d ] < cover [ x ] { d } else { x } } ) ; walk . push ( end ) ; } walk }
This is a implementation of the Wilson (random walk) maze algorithm that has been
modified to include backtacking and handle already existing rooms
This is a implementation of the Wilson ( random walk ) maze algorithm that has been modified to include backtacking and handle already existing rooms
maze
&mut Maze
index
usize
maze
&mut Maze
index
usize