gen-world
This commit is contained in:
		
							parent
							
								
									ae9dd6eb98
								
							
						
					
					
						commit
						b24d023f1a
					
				
							
								
								
									
										44
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								src/main.rs
									
									
									
									
									
								
							@ -73,6 +73,33 @@ fn gen_random_instructions() -> String {
 | 
			
		||||
    instructions
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn gen_world(pool: &mut Vec<robot::Robot>) -> 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<char> = gen_random_instructions().chars().rev().collect();
 | 
			
		||||
    let instructions2: Vec<char> = 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<robot::Robot>) -> Result<world::World, String> {
 | 
			
		||||
    let mut lines: Vec<&str> = conf.split('\n').collect();
 | 
			
		||||
@ -213,16 +240,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
                .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<robot::Robot> = Vec::new();
 | 
			
		||||
    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<robot::Position, u32> = HashMap::new();
 | 
			
		||||
    create_hash_map(&robot_pool, &mut hash);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user