rust

Générer un labyrinthe avec le langage RUST et claude.ia

Posted by admin on avril 21, 2025
GNU/Linux, rust / No Comments
use rand::prelude::*;
use std::collections::HashSet;

fn main() {
    // Dimensions du labyrinthe (doit être impair pour avoir des murs aux bords)
    let width = 29;
    let height = 19;
    
    // Générer le labyrinthe
    let maze = generate_maze(width, height);
    
    // Afficher le labyrinthe
    print_maze(&maze, width, height);
}

fn generate_maze(width: usize, height: usize) -> Vec<Vec<char>> {
    // Initialiser le labyrinthe avec des murs partout
    let mut maze = vec![vec!['#'; width]; height];
    let mut rng = rand::thread_rng();
    
    // Ensemble pour suivre les cellules visitées
    let mut visited = HashSet::new();
    
    // Pile pour l'algorithme DFS
    let mut stack = Vec::new();
    
    // Point de départ (coordonnées impaires)
    let start_x = 1;
    let start_y = 1;
    
    // Marquer le point de départ comme un chemin
    maze[start_y][start_x] = ' ';
    visited.insert((start_x, start_y));
    stack.push((start_x, start_y));
    
    // Directions possibles: droite, bas, gauche, haut
    let directions = [(2, 0), (0, 2), (-2, 0), (0, -2)];
    
    // Tant que la pile n'est pas vide
    while !stack.is_empty() {
        let current = stack.last().unwrap().clone();
        let (x, y) = current;
        
        // Trouver les voisins non visités
        let mut neighbors = Vec::new();
        
        for (dx, dy) in directions.iter() {
            let nx = (x as isize + dx) as usize;
            let ny = (y as isize + dy) as usize;
            
            // Vérifier si le voisin est dans les limites du labyrinthe
            if nx > 0 && nx < width - 1 && ny > 0 && ny < height - 1 
               && !visited.contains(&(nx, ny)) {
                neighbors.push((nx, ny, *dx, *dy));
            }
        }
        
        // S'il y a des voisins non visités
        if !neighbors.is_empty() {
            // Choisir un voisin au hasard
            let (nx, ny, dx, dy) = neighbors[rng.gen_range(0..neighbors.len())];
            
            // Creuser un passage
            let wall_x = (x as isize + dx / 2) as usize;
            let wall_y = (y as isize + dy / 2) as usize;
            maze[wall_y][wall_x] = ' ';
            
            // Marquer le voisin comme visité
            maze[ny][nx] = ' ';
            visited.insert((nx, ny));
            
            // Pousser le nouveau voisin sur la pile
            stack.push((nx, ny));
        } else {
            // Retour en arrière si aucun voisin non visité
            stack.pop();
        }
    }
    
    // Définir l'entrée et la sortie
    maze[0][1] = ' '; // Entrée en haut
    maze[height - 1][width - 2] = ' '; // Sortie en bas
    
    maze
}

fn print_maze(maze: &Vec<Vec<char>>, width: usize, height: usize) {
    for y in 0..height {
        for x in 0..width {
            print!("{}", maze[y][x]);
        }
        println!();
    }
}


cargo.toml:

[dependencies]
rand = "0.8.5"

résultat:

$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/maze_claudeia`
# ###########################
# #         #   #         # #
# ####### # # # ### ##### # #
#   #     #   #     #   # # #
### # ############### ### # #
#   #   #           #   #   #
# ##### # ### ##### ### ### #
#       # #   #   #     #   #
########### ### # ####### ###
#   #       #   #         # #
# ### ### ### ############# #
# #   # #     #         #   #
# # ### ######### ##### ### #
#   #     #     # #   #     #
# ### ### # ### # ### ##### #
# #   #   #   # #   # #   # #
# ### # ##### # ### # # # # #
#     #       #     #   #   #
########################### #

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: