0.3.1
This commit is contained in:
		
							parent
							
								
									6e55705184
								
							
						
					
					
						commit
						68a23bafb9
					
				
							
								
								
									
										41
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/main.rs
									
									
									
									
									
								
							@ -59,18 +59,15 @@ fn create_hash_map(pool: &Vec<robot::Robot>, hash: &mut HashMap<robot::Position,
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Generate random instructions.
 | 
					fn gen_world(pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
 | 
				
			||||||
fn gen_random_instructions() -> String {
 | 
					    let w = world::random_world();
 | 
				
			||||||
    let mut rng = rand::thread_rng();
 | 
					    let mut rng = rand::thread_rng();
 | 
				
			||||||
    let n = rng.gen_range(5, 10);
 | 
					    let x = rng.gen_range(2, 10);
 | 
				
			||||||
    let mut instructions = String::with_capacity(n);
 | 
					    for i in 0..=x {
 | 
				
			||||||
    const CHARSET: &[u8] = b"LRF";
 | 
					        let r = robot::Robot::new_random(i + 1, w.x - 2, w.y - 2)?;
 | 
				
			||||||
    for _ in 0..n {
 | 
					        pool.push(r);
 | 
				
			||||||
        let l = rng.gen_range(0, CHARSET.len());
 | 
					 | 
				
			||||||
        instructions.push(CHARSET[l] as char);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    Ok(w)
 | 
				
			||||||
    instructions
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Parse the config file, generate the world and robot pool.
 | 
					/// Parse the config file, generate the world and robot pool.
 | 
				
			||||||
@ -207,15 +204,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
				
			|||||||
                .takes_value(false)
 | 
					                .takes_value(false)
 | 
				
			||||||
                .help("Generate random world"),
 | 
					                .help("Generate random world"),
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					        .arg(
 | 
				
			||||||
 | 
					            Arg::with_name("random")
 | 
				
			||||||
 | 
					                .short("r")
 | 
				
			||||||
 | 
					                .long("random")
 | 
				
			||||||
 | 
					                .takes_value(false)
 | 
				
			||||||
 | 
					                .help("Generate random world"),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        .get_matches();
 | 
					        .get_matches();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let mut robot_pool: Vec<robot::Robot> = Vec::new();
 | 
					    let mut robot_pool: Vec<robot::Robot> = Vec::new();
 | 
				
			||||||
    let mut world: world::World = parse_config(raw_conf, &mut robot_pool)?;
 | 
					    let world = match matches.is_present("random") {
 | 
				
			||||||
    world = match matches.is_present("random-world") {
 | 
					        true => gen_world(&mut robot_pool)?,
 | 
				
			||||||
        false => world,
 | 
					        false => {
 | 
				
			||||||
        true => world::random_world(),
 | 
					            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<robot::Position, u32> = HashMap::new();
 | 
					    let mut hash: HashMap<robot::Position, u32> = HashMap::new();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										36
									
								
								src/robot.rs
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/robot.rs
									
									
									
									
									
								
							@ -17,6 +17,21 @@ impl Robot {
 | 
				
			|||||||
    pub fn new(id: u32, o: Orientation, p: Position, i: Vec<Instruction>) -> Robot {
 | 
					    pub fn new(id: u32, o: Orientation, p: Position, i: Vec<Instruction>) -> Robot {
 | 
				
			||||||
        Robot { id, o, p, i }
 | 
					        Robot { id, o, p, i }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    /// Create new random `Robot`.
 | 
				
			||||||
 | 
					    pub fn new_random(id: u32, posx_max: i32, posy_max: i32) -> Result<Robot, String> {
 | 
				
			||||||
 | 
					        let mut rng = rand::thread_rng();
 | 
				
			||||||
 | 
					        let x = rng.gen_range(2, posx_max);
 | 
				
			||||||
 | 
					        let y = rng.gen_range(2, posy_max);
 | 
				
			||||||
 | 
					        let inst = gen_random_instructions();
 | 
				
			||||||
 | 
					        let instructions: Vec<Instruction> = instructions_from_string(inst)?;
 | 
				
			||||||
 | 
					        let o: Orientation = rand::random();
 | 
				
			||||||
 | 
					        Ok(Robot {
 | 
				
			||||||
 | 
					            id,
 | 
				
			||||||
 | 
					            o,
 | 
				
			||||||
 | 
					            p: Position { x, y },
 | 
				
			||||||
 | 
					            i: instructions,
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    /// Apply given instruction to a `Robot`.
 | 
					    /// Apply given instruction to a `Robot`.
 | 
				
			||||||
    pub fn execute_instruction(&mut self) {
 | 
					    pub fn execute_instruction(&mut self) {
 | 
				
			||||||
        match self.i.pop() {
 | 
					        match self.i.pop() {
 | 
				
			||||||
@ -100,17 +115,18 @@ pub struct Position {
 | 
				
			|||||||
    pub y: i32,
 | 
					    pub y: i32,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Check if instructions list is valid.
 | 
					/// Generate random instructions.
 | 
				
			||||||
pub fn is_instructions(s: &str) -> bool {
 | 
					fn gen_random_instructions() -> String {
 | 
				
			||||||
    for c in s.chars() {
 | 
					    let mut rng = rand::thread_rng();
 | 
				
			||||||
        match c {
 | 
					    let n = rng.gen_range(5, 10);
 | 
				
			||||||
            'F' => continue,
 | 
					    let mut instructions = String::with_capacity(n);
 | 
				
			||||||
            'R' => continue,
 | 
					    const CHARSET: &[u8] = b"LRF";
 | 
				
			||||||
            'L' => continue,
 | 
					    for _ in 0..n {
 | 
				
			||||||
            _ => return false,
 | 
					        let l = rng.gen_range(0, CHARSET.len());
 | 
				
			||||||
        }
 | 
					        instructions.push(CHARSET[l] as char);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    true
 | 
					
 | 
				
			||||||
 | 
					    instructions
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Check if a robot is piouff.
 | 
					/// Check if a robot is piouff.
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user