diff --git a/src/main.rs b/src/main.rs index 492e1ca..3475f8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -198,36 +198,38 @@ fn display_grid( w: &world::World, robot_pool: &Vec, h: &HashMap, -) { +) -> 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> { @@ -262,11 +264,13 @@ fn main() -> Result<(), Box> { let mut hash: HashMap = 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> { 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); } diff --git a/src/robot.rs b/src/robot.rs index 932fa85..2856d46 100644 --- a/src/robot.rs +++ b/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) { - println!("Robots ["); +pub fn print_robots(robot_pool: &Vec) -> 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) { r.i ); } - println!("]"); + res = format!("{}]\n", res); + res } #[cfg(test)] diff --git a/src/world.rs b/src/world.rs index 5ad774f..7f3f4c1 100644 --- a/src/world.rs +++ b/src/world.rs @@ -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 } }