use rand::Rng;
use rand::seq::SliceRandom;
use crate::maze::{WALL, PATH};
use crate::position::Position;
pub fn dig_maze(grid: &mut Vec<Vec<char>>,
width: usize, height: usize,
start_x: usize, start_y: usize,
rng: &mut impl Rng) {
grid[start_y][start_x] = PATH;
let directions = [(0, -2), (2, 0), (0, 2), (-2, 0)];
let mut shuffled_directions = directions.to_vec();
shuffled_directions.shuffle(rng);
for (dx, dy) in shuffled_directions {
let nx = start_x as isize + dx;
let ny = start_y as isize + dy;
if nx > 0 && nx < width as isize - 1 && ny > 0 && ny < height as isize - 1
&& grid[ny as usize][nx as usize] == WALL {
grid[(start_y as isize + dy / 2) as usize][(start_x as isize + dx / 2) as usize] = PATH;
dig_maze(grid, width, height, nx as usize, ny as usize, rng);
}
}
}
pub fn find_path_position(grid: &Vec<Vec<char>>,
width: usize, height: usize,
start_x: usize, start_y: usize) -> Option<Position> {
if grid[start_y][start_x] == PATH {
return Some(Position::new(start_x, start_y));
}
for y in 0..height {
for x in 0..width {
if grid[y][x] == PATH {
return Some(Position::new(x, y));
}
}
}
None
}
pub fn find_path_position_from_bottom(
grid: &Vec<Vec<char>>,
width: usize, height: usize,
start_x: usize,
start_y: usize,
avoid: &Position
) -> Option<Position> {
if grid[start_y][start_x] == PATH && !(start_x == avoid.x && start_y == avoid.y) {
return Some(Position::new(start_x, start_y));
}
for y in (0..height).rev() {
for x in (0..width).rev() {
if grid[y][x] == PATH && !(x == avoid.x && y == avoid.y) {
return Some(Position::new(x, y));
}
}
}
None
}