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 1/3] 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(); From a91eefaf4115dc82bad7f043482309f89a5b3d3d Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sun, 8 Nov 2020 14:22:27 +0100 Subject: [PATCH 2/3] flag rename --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5439ddc..300d62f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -241,15 +241,15 @@ fn main() -> Result<(), Box> { .help("Generate random world"), ) .arg( - Arg::with_name("gen-world") - .short("g") - .long("gen-world") + Arg::with_name("random") + .short("r") + .long("random") .takes_value(false) .help("Generate random world with 2 random robots"), ) .get_matches(); let mut robot_pool: Vec = Vec::new(); - let world = match matches.is_present("gen-world") { + let world = match matches.is_present("random") { true => gen_world(&mut robot_pool), false => { let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?; From 8b2cf6b5301099cd1e4ba786c28c86ac8c0018c7 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sun, 8 Nov 2020 14:59:06 +0100 Subject: [PATCH 3/3] random number of robots --- src/main.rs | 40 +++++----------------------------------- src/robot.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 300d62f..67a2ad4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,44 +59,14 @@ fn create_hash_map(pool: &Vec, hash: &mut HashMap String { - let mut rng = rand::thread_rng(); - let n = rng.gen_range(5, 10); - let mut instructions = String::with_capacity(n); - const CHARSET: &[u8] = b"LRF"; - for _ in 0..n { - let l = rng.gen_range(0, CHARSET.len()); - instructions.push(CHARSET[l] as char); - } - - 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(); + let x = rng.gen_range(2, 10); - 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, - )); + for i in 0..=x { + pool.push(robot::Robot::new_random(i, w.x - 2, w.y - 2)); + } w } @@ -125,7 +95,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result return Err(String::from("Robot setup is broken.")), }; - let rand_instructions = gen_random_instructions(); + let rand_instructions = robot::gen_random_instructions(); let l = lines.remove(0); let instructions = match ConfParser::parse(Rule::robot_instructions, l) { diff --git a/src/robot.rs b/src/robot.rs index 2856d46..031f02a 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -17,6 +17,19 @@ impl Robot { pub fn new(id: u32, o: Orientation, p: Position, i: Vec) -> Robot { Robot { id, o, p, i } } + pub fn new_random(id: u32, posx_max: i32, posy_max: i32) -> Robot { + let mut rng = rand::thread_rng(); + let x = rng.gen_range(2, posx_max); + let y = rng.gen_range(2, posy_max); + let instructions: Vec = gen_random_instructions().chars().rev().collect(); + let o: Orientation = rand::random(); + Robot { + id, + o, + p: Position { x, y }, + i: instructions, + } + } /// Apply given instruction to a `Robot`. pub fn execute_instruction(&mut self) { match self.i.pop() { @@ -86,6 +99,20 @@ pub fn is_instructions(v: &Vec) -> bool { true } +/// Generate random instructions. +pub fn gen_random_instructions() -> String { + let mut rng = rand::thread_rng(); + let n = rng.gen_range(5, 10); + let mut instructions = String::with_capacity(n); + const CHARSET: &[u8] = b"LRF"; + for _ in 0..n { + let l = rng.gen_range(0, CHARSET.len()); + instructions.push(CHARSET[l] as char); + } + + instructions +} + /// Check if a robot is piouff. pub fn is_piouff(r: &Robot) -> bool { if r.i.len() == 0 {