random number of robots
This commit is contained in:
parent
a91eefaf41
commit
8b2cf6b530
40
src/main.rs
40
src/main.rs
@ -59,44 +59,14 @@ fn create_hash_map(pool: &Vec<robot::Robot>, hash: &mut HashMap<robot::Position,
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate random instructions.
|
||||
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
|
||||
}
|
||||
|
||||
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();
|
||||
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<robot::Robot>) -> Result<world::Wor
|
||||
Err(_) => 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) {
|
||||
|
27
src/robot.rs
27
src/robot.rs
@ -17,6 +17,19 @@ impl Robot {
|
||||
pub fn new(id: u32, o: Orientation, p: Position, i: Vec<char>) -> 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<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`.
|
||||
pub fn execute_instruction(&mut self) {
|
||||
match self.i.pop() {
|
||||
@ -86,6 +99,20 @@ pub fn is_instructions(v: &Vec<char>) -> 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 {
|
||||
|
Loading…
Reference in New Issue
Block a user