Merge branch 'revert-cfbf0f36' into 'master'
Revert that huge stinky shit See merge request mhart/DancingDroids!62
This commit is contained in:
		
						commit
						971bfb615f
					
				
							
								
								
									
										36
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/main.rs
									
									
									
									
									
								
							@ -59,15 +59,18 @@ fn create_hash_map(pool: &Vec<robot::Robot>, hash: &mut HashMap<robot::Position,
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn gen_world(pool: &mut Vec<robot::Robot>) -> world::World {
 | 
					/// Generate random instructions.
 | 
				
			||||||
    let w = world::random_world();
 | 
					fn gen_random_instructions() -> String {
 | 
				
			||||||
    let mut rng = rand::thread_rng();
 | 
					    let mut rng = rand::thread_rng();
 | 
				
			||||||
    let x = rng.gen_range(2, 10);
 | 
					    let n = rng.gen_range(5, 10);
 | 
				
			||||||
 | 
					    let mut instructions = String::with_capacity(n);
 | 
				
			||||||
    for i in 0..=x {
 | 
					    const CHARSET: &[u8] = b"LRF";
 | 
				
			||||||
        pool.push(robot::Robot::new_random(i, w.x - 2, w.y - 2));
 | 
					    for _ in 0..n {
 | 
				
			||||||
 | 
					        let l = rng.gen_range(0, CHARSET.len());
 | 
				
			||||||
 | 
					        instructions.push(CHARSET[l] as char);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    w
 | 
					
 | 
				
			||||||
 | 
					    instructions
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Parse the config file, generate the world and robot pool.
 | 
					/// Parse the config file, generate the world and robot pool.
 | 
				
			||||||
@ -95,7 +98,7 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
 | 
				
			|||||||
            Err(_) => return Err(String::from("Robot setup is broken.")),
 | 
					            Err(_) => return Err(String::from("Robot setup is broken.")),
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let rand_instructions = robot::gen_random_instructions();
 | 
					        let rand_instructions = gen_random_instructions();
 | 
				
			||||||
        let l = lines.remove(0);
 | 
					        let l = lines.remove(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let instructions = match ConfParser::parse(Rule::robot_instructions, l) {
 | 
					        let instructions = match ConfParser::parse(Rule::robot_instructions, l) {
 | 
				
			||||||
@ -210,27 +213,16 @@ 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 with 2 random robots"),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        .get_matches();
 | 
					        .get_matches();
 | 
				
			||||||
    let mut robot_pool: Vec<robot::Robot> = Vec::new();
 | 
					
 | 
				
			||||||
    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"))?;
 | 
					    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 world: world::World = parse_config(raw_conf, &mut robot_pool)?;
 | 
					    let mut world: world::World = parse_config(raw_conf, &mut robot_pool)?;
 | 
				
			||||||
    world = match matches.is_present("random-world") {
 | 
					    world = match matches.is_present("random-world") {
 | 
				
			||||||
        false => world,
 | 
					        false => world,
 | 
				
			||||||
        true => world::random_world(),
 | 
					        true => world::random_world(),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
            world
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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);
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										27
									
								
								src/robot.rs
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								src/robot.rs
									
									
									
									
									
								
							@ -17,19 +17,6 @@ impl Robot {
 | 
				
			|||||||
    pub fn new(id: u32, o: Orientation, p: Position, i: Vec<char>) -> Robot {
 | 
					    pub fn new(id: u32, o: Orientation, p: Position, i: Vec<char>) -> Robot {
 | 
				
			||||||
        Robot { id, o, p, i }
 | 
					        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<char> = 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`.
 | 
					    /// 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() {
 | 
				
			||||||
@ -99,20 +86,6 @@ pub fn is_instructions(v: &Vec<char>) -> bool {
 | 
				
			|||||||
    true
 | 
					    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.
 | 
					/// Check if a robot is piouff.
 | 
				
			||||||
pub fn is_piouff(r: &Robot) -> bool {
 | 
					pub fn is_piouff(r: &Robot) -> bool {
 | 
				
			||||||
    if r.i.len() == 0 {
 | 
					    if r.i.len() == 0 {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user