Merge branch 'display' into 'master'
some cheat animation See merge request mhart/DancingDroids!55
This commit is contained in:
		
						commit
						7890612825
					
				
							
								
								
									
										55
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								src/main.rs
									
									
									
									
									
								
							@ -198,36 +198,38 @@ fn display_grid(
 | 
			
		||||
    w: &world::World,
 | 
			
		||||
    robot_pool: &Vec<robot::Robot>,
 | 
			
		||||
    h: &HashMap<robot::Position, u32>,
 | 
			
		||||
) {
 | 
			
		||||
) -> String {
 | 
			
		||||
    let mut grid = String::new();
 | 
			
		||||
    for i in (0..w.y + 1).rev() {
 | 
			
		||||
        if i < 10 {
 | 
			
		||||
            print!("{}  ", i);
 | 
			
		||||
            grid = format!("{}{}  ", grid, i);
 | 
			
		||||
        } else {
 | 
			
		||||
            print!("{} ", i);
 | 
			
		||||
            grid = format!("{}{} ", grid, i);
 | 
			
		||||
        }
 | 
			
		||||
        for j in 0..w.x + 1 {
 | 
			
		||||
            match h.get(&robot::Position { x: j, y: i }) {
 | 
			
		||||
                Some(id) => match robot_pool[(id - 1) as usize].o {
 | 
			
		||||
                    robot::Orientation::N => print!("↑  "),
 | 
			
		||||
                    robot::Orientation::E => print!("→  "),
 | 
			
		||||
                    robot::Orientation::S => print!("↓  "),
 | 
			
		||||
                    robot::Orientation::W => print!("←  "),
 | 
			
		||||
                    robot::Orientation::N => grid = format!("{}↑  ", grid),
 | 
			
		||||
                    robot::Orientation::E => grid = format!("{}→  ", grid),
 | 
			
		||||
                    robot::Orientation::S => grid = format!("{}↓  ", grid),
 | 
			
		||||
                    robot::Orientation::W => grid = format!("{}←  ", grid),
 | 
			
		||||
                },
 | 
			
		||||
 | 
			
		||||
                None => print!(".  "),
 | 
			
		||||
                None => grid = format!("{}.  ", grid),
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        println!();
 | 
			
		||||
        grid = format!("{}\n", grid);
 | 
			
		||||
    }
 | 
			
		||||
    print!("   ");
 | 
			
		||||
    grid = format!("{}   ", grid);
 | 
			
		||||
    for j in 0..w.x + 1 {
 | 
			
		||||
        if j < 10 {
 | 
			
		||||
            print!("{}  ", j);
 | 
			
		||||
            grid = format!("{}{}  ", grid, j);
 | 
			
		||||
        } else {
 | 
			
		||||
            print!("{} ", j);
 | 
			
		||||
            grid = format!("{}{} ", grid, j);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    println!();
 | 
			
		||||
    grid = format!("{}\n", grid);
 | 
			
		||||
    grid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
@ -262,11 +264,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
 | 
			
		||||
    let mut hash: HashMap<robot::Position, u32> = HashMap::new();
 | 
			
		||||
    create_hash_map(&robot_pool, &mut hash);
 | 
			
		||||
    println!("World {{ x_max = {}; y_max = {} }}", world.x, world.y);
 | 
			
		||||
    robot::print_robots(&robot_pool);
 | 
			
		||||
    println!("Initial state");
 | 
			
		||||
    println!("==============");
 | 
			
		||||
    display_grid(&world, &robot_pool, &hash);
 | 
			
		||||
    let mut print_all = format!("World {{ x_max = {}; y_max = {} }}\n", world.x, world.y);
 | 
			
		||||
    print_all = format!("{}{}", print_all, robot::print_robots(&robot_pool));
 | 
			
		||||
    if world.x <= 60 && world.y <= 45 {
 | 
			
		||||
        print_all = format!("{}Initial state\n", print_all);
 | 
			
		||||
        print_all = format!("{}==============\n", print_all);
 | 
			
		||||
        print_all = format!("{}{}", print_all, display_grid(&world, &robot_pool, &hash));
 | 
			
		||||
    }
 | 
			
		||||
    loop {
 | 
			
		||||
        let mut piouff: u32 = 0;
 | 
			
		||||
        for r in &mut robot_pool {
 | 
			
		||||
@ -280,13 +284,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
                hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if world.x <= 60 && world.y <= 45 {
 | 
			
		||||
            std::thread::sleep(std::time::Duration::from_millis(600));
 | 
			
		||||
            print!("\x1B[2J\x1B[1;1H");
 | 
			
		||||
            print!("{}", display_grid(&world, &robot_pool, &hash));
 | 
			
		||||
        }
 | 
			
		||||
        if piouff == robot_pool.len() as u32 {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    println!("Final state");
 | 
			
		||||
    println!("============");
 | 
			
		||||
    display_grid(&world, &robot_pool, &hash);
 | 
			
		||||
    print!("\x1B[2J\x1B[1;1H");
 | 
			
		||||
    print!("{}", print_all);
 | 
			
		||||
    if world.x <= 60 && world.y <= 45 {
 | 
			
		||||
        println!("Final state");
 | 
			
		||||
        println!("============");
 | 
			
		||||
        print!("{}", display_grid(&world, &robot_pool, &hash));
 | 
			
		||||
    }
 | 
			
		||||
    for r in &robot_pool {
 | 
			
		||||
        println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								src/robot.rs
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/robot.rs
									
									
									
									
									
								
							@ -95,11 +95,12 @@ pub fn is_piouff(r: &Robot) -> bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Print robots id, position and instructions.
 | 
			
		||||
pub fn print_robots(robot_pool: &Vec<Robot>) {
 | 
			
		||||
    println!("Robots [");
 | 
			
		||||
pub fn print_robots(robot_pool: &Vec<Robot>) -> String {
 | 
			
		||||
    let mut res = format!("Robots [\n");
 | 
			
		||||
    for r in robot_pool {
 | 
			
		||||
        println!(
 | 
			
		||||
            "{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},",
 | 
			
		||||
        res = format!(
 | 
			
		||||
            "{}{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},\n",
 | 
			
		||||
            res,
 | 
			
		||||
            r.id,
 | 
			
		||||
            r.p.x,
 | 
			
		||||
            r.p.y,
 | 
			
		||||
@ -112,7 +113,8 @@ pub fn print_robots(robot_pool: &Vec<Robot>) {
 | 
			
		||||
            r.i
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
    println!("]");
 | 
			
		||||
    res = format!("{}]\n", res);
 | 
			
		||||
    res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ pub struct World {
 | 
			
		||||
/// Create a random World.
 | 
			
		||||
pub fn random_world() -> World {
 | 
			
		||||
    let mut rng = rand::thread_rng();
 | 
			
		||||
    let z = rng.gen_range(5, 50);
 | 
			
		||||
    let z = rng.gen_range(5, 45);
 | 
			
		||||
    World { x: z, y: z }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user