From 9857639b127b9d31c871eec0ff18c429682a6cb6 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sat, 7 Nov 2020 15:05:47 +0100 Subject: [PATCH] limit grid display to (60x45)max --- src/main.rs | 24 +++++++++++++++--------- src/world.rs | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 634426a..3475f8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -266,9 +266,11 @@ fn main() -> Result<(), Box> { create_hash_map(&robot_pool, &mut 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)); - 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)); + 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 { @@ -282,18 +284,22 @@ fn main() -> Result<(), Box> { hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id); } } - std::thread::sleep(std::time::Duration::from_millis(600)); - print!("\x1B[2J\x1B[1;1H"); - print!("{}", display_grid(&world, &robot_pool, &hash)); + 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; } } print!("\x1B[2J\x1B[1;1H"); print!("{}", print_all); - println!("Final state"); - println!("============"); - print!("{}", display_grid(&world, &robot_pool, &hash)); + 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/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 } }