From b24d023f1ac2789815610a21af70f59d9fe60d60 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sun, 8 Nov 2020 13:28:58 +0100 Subject: [PATCH] gen-world --- src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index c1bba05..5439ddc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,33 @@ fn gen_random_instructions() -> String { instructions } +fn gen_world(pool: &mut Vec) -> world::World { + let w = world::random_world(); + let mut rng = rand::thread_rng(); + let x1 = rng.gen_range(2, w.x - 2); + let x2 = rng.gen_range(2, w.x - 2); + let y1 = rng.gen_range(2, w.y - 2); + let y2 = rng.gen_range(2, w.y - 2); + let instructions1: Vec = gen_random_instructions().chars().rev().collect(); + let instructions2: Vec = gen_random_instructions().chars().rev().collect(); + let o1: robot::Orientation = rand::random(); + let o2: robot::Orientation = rand::random(); + + pool.push(robot::Robot::new( + 1, + o1, + robot::Position { x: x1, y: y1 }, + instructions1, + )); + pool.push(robot::Robot::new( + 2, + o2, + robot::Position { x: x2, y: y2 }, + instructions2, + )); + w +} + /// Parse the config file, generate the world and robot pool. fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines: Vec<&str> = conf.split('\n').collect(); @@ -213,15 +240,26 @@ fn main() -> Result<(), Box> { .takes_value(false) .help("Generate random world"), ) + .arg( + Arg::with_name("gen-world") + .short("g") + .long("gen-world") + .takes_value(false) + .help("Generate random world with 2 random robots"), + ) .get_matches(); - - let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?; - let mut robot_pool: Vec = Vec::new(); - let mut world: world::World = parse_config(raw_conf, &mut robot_pool)?; - world = match matches.is_present("random-world") { - false => world, - true => world::random_world(), + let world = match matches.is_present("gen-world") { + true => gen_world(&mut robot_pool), + false => { + let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?; + let mut world: world::World = parse_config(raw_conf, &mut robot_pool)?; + world = match matches.is_present("random-world") { + false => world, + true => world::random_world(), + }; + world + } }; let mut hash: HashMap = HashMap::new();