■ 미로를 자동으로 생성하는 방법을 보여준다.
▶ main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
use rand::Rng; const CELL_COUNT : usize = 25; fn main() { let mut random_number_generator = rand::thread_rng(); let mut maze_array = [[0; CELL_COUNT]; CELL_COUNT]; for n in 0..CELL_COUNT { maze_array[n ][0 ] = 1; // 위쪽 벽 maze_array[0 ][n ] = 1; // 왼쪽 벽 maze_array[n ][CELL_COUNT-1] = 1; // 오른쪽 벽 maze_array[CELL_COUNT - 1][n ] = 1; // 아래쪽 벽 } for y in 2..CELL_COUNT - 2 { for x in 2..CELL_COUNT - 2 { if x % 2 == 1 || y % 2 == 1 { continue; } maze_array[y][x] = 1; // 벽 let random_number = random_number_generator.gen_range(0..=3); match random_number { 0 => maze_array[y - 1][x ] = 1, // 상 1 => maze_array[y + 1][x ] = 1, // 하 2 => maze_array[y ][x - 1] = 1, // 좌 3 => maze_array[y ][x + 1] = 1, // 우 _ => {}, } } } let tile_array = [" ", "##"]; for y in 0..CELL_COUNT { for x in 0..CELL_COUNT { print!("{}", tile_array[maze_array[y][x]]); } println!(""); } } |