Merge branch 'master' into unwrap-lol

This commit is contained in:
Volodymyr Patuta 2020-11-07 16:28:06 +01:00
commit b868a7978b
3 changed files with 43 additions and 27 deletions

View File

@ -59,6 +59,7 @@ fn create_hash_map(pool: &Vec<robot::Robot>, hash: &mut HashMap<robot::Position,
} }
} }
/// Generate random instructions.
fn gen_random_instructions() -> String { fn gen_random_instructions() -> String {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let n = rng.gen_range(5, 10); let n = rng.gen_range(5, 10);
@ -159,36 +160,38 @@ fn display_grid(
w: &world::World, w: &world::World,
robot_pool: &Vec<robot::Robot>, robot_pool: &Vec<robot::Robot>,
h: &HashMap<robot::Position, u32>, h: &HashMap<robot::Position, u32>,
) { ) -> String {
let mut grid = String::new();
for i in (0..w.y + 1).rev() { for i in (0..w.y + 1).rev() {
if i < 10 { if i < 10 {
print!("{} ", i); grid = format!("{}{} ", grid, i);
} else { } else {
print!("{} ", i); grid = format!("{}{} ", grid, i);
} }
for j in 0..w.x + 1 { for j in 0..w.x + 1 {
match h.get(&robot::Position { x: j, y: i }) { match h.get(&robot::Position { x: j, y: i }) {
Some(id) => match robot_pool[(id - 1) as usize].o { Some(id) => match robot_pool[(id - 1) as usize].o {
robot::Orientation::N => print!(""), robot::Orientation::N => grid = format!("{}", grid),
robot::Orientation::E => print!(""), robot::Orientation::E => grid = format!("{}", grid),
robot::Orientation::S => print!(""), robot::Orientation::S => grid = format!("{}", grid),
robot::Orientation::W => print!(""), 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 { for j in 0..w.x + 1 {
if j < 10 { if j < 10 {
print!("{} ", j); grid = format!("{}{} ", grid, j);
} else { } else {
print!("{} ", j); grid = format!("{}{} ", grid, j);
} }
} }
println!(); grid = format!("{}\n", grid);
grid
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -223,11 +226,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hash: HashMap<robot::Position, u32> = HashMap::new(); let mut hash: HashMap<robot::Position, u32> = HashMap::new();
create_hash_map(&robot_pool, &mut hash); create_hash_map(&robot_pool, &mut hash);
println!("World {{ x_max = {}; y_max = {} }}", world.x, world.y); let mut print_all = format!("World {{ x_max = {}; y_max = {} }}\n", world.x, world.y);
robot::print_robots(&robot_pool); print_all = format!("{}{}", print_all, robot::print_robots(&robot_pool));
println!("Initial state"); if world.x <= 60 && world.y <= 45 {
println!("=============="); print_all = format!("{}Initial state\n", print_all);
display_grid(&world, &robot_pool, &hash); print_all = format!("{}==============\n", print_all);
print_all = format!("{}{}", print_all, display_grid(&world, &robot_pool, &hash));
}
loop { loop {
let mut piouff: u32 = 0; let mut piouff: u32 = 0;
for r in &mut robot_pool { for r in &mut robot_pool {
@ -241,13 +246,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id); 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 { if piouff == robot_pool.len() as u32 {
break; break;
} }
} }
print!("\x1B[2J\x1B[1;1H");
print!("{}", print_all);
if world.x <= 60 && world.y <= 45 {
println!("Final state"); println!("Final state");
println!("============"); println!("============");
display_grid(&world, &robot_pool, &hash); print!("{}", display_grid(&world, &robot_pool, &hash));
}
for r in &robot_pool { for r in &robot_pool {
println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y); println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y);
} }

View File

@ -95,11 +95,12 @@ pub fn is_piouff(r: &Robot) -> bool {
} }
/// Print robots id, position and instructions. /// Print robots id, position and instructions.
pub fn print_robots(robot_pool: &Vec<Robot>) { pub fn print_robots(robot_pool: &Vec<Robot>) -> String {
println!("Robots ["); let mut res = format!("Robots [\n");
for r in robot_pool { for r in robot_pool {
println!( res = format!(
"{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},", "{}{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},\n",
res,
r.id, r.id,
r.p.x, r.p.x,
r.p.y, r.p.y,
@ -112,7 +113,8 @@ pub fn print_robots(robot_pool: &Vec<Robot>) {
r.i r.i
); );
} }
println!("]"); res = format!("{}]\n", res);
res
} }
#[cfg(test)] #[cfg(test)]

View File

@ -9,7 +9,7 @@ pub struct World {
/// Create a random World. /// Create a random World.
pub fn random_world() -> World { pub fn random_world() -> World {
let mut rng = rand::thread_rng(); 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 } World { x: z, y: z }
} }