Need to fix a lot of bugs...

This commit is contained in:
Martin HART 2020-10-30 13:23:17 +01:00
parent f4a1eac5b7
commit 6fa55b6924
1 changed files with 57 additions and 20 deletions

View File

@ -46,74 +46,89 @@ fn check_collisions(
}
/// Parse the config file, generate the world and robot pool.
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, &'static str> {
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
let mut lines = conf.lines();
// The first line of the config file should be the World.
let raw_line: &str = match lines.next() {
Some(raw) => raw,
None => return Err("Could not read the first line of the config file !"),
None => return Err("Could not read the first line of the config file !".to_string()),
};
let mut tokens = raw_line.split_whitespace();
let token1 = match tokens.next() {
Some(raw) => raw,
None => return Err("Could not read the first token of the first line !"),
None => return Err("Could not read the first token of the first line !".to_string()),
};
let token2 = match tokens.next() {
Some(raw) => raw,
None => return Err("Could not read the second token of the first line !"),
None => return Err("Could not read the second token of the first line !".to_string()),
};
let x: i32 = match token1.parse::<i32>() {
Ok(x) => x,
Err(_) => return Err("Could not convert token one from the first string to i32"),
Err(_) => {
return Err("Could not convert token one from the first string to i32".to_string())
}
};
let y: i32 = match token2.parse::<i32>() {
Ok(x) => x,
Err(_) => return Err("Could not convert token two from the first string to i32"),
Err(_) => {
return Err("Could not convert token two from the first string to i32".to_string())
}
};
let w = world::World { x, y };
let mut r_id: u32 = 0;
loop {
r_id += 1;
// This line should be empty.
let empty_line = match lines.next() {
None => break,
Some(x) => x,
};
if !empty_line.is_empty() {
return Err("This line should be empty !");
return Err("This line should be empty !".to_string());
}
let raw_setup = match lines.next() {
None => return Err("This line should be the config !"),
None => return Err("This line should be the config !".to_string()),
Some(raw) => raw,
};
if raw_setup.is_empty() {
return Err("This line should not be empty !");
return Err("This line should not be empty !".to_string());
}
let raw_inst = match lines.next() {
None => return Err("This line should be the instruction !"),
None => return Err("This line should be the instruction !".to_string()),
Some(raw) => raw,
};
if raw_inst.is_empty() {
return Err("This line should not be empty !");
return Err("This line should not be empty !".to_string());
}
// Parse the setup line of the robot.
let mut setup = raw_setup.split_whitespace();
let pos_x = match setup.next() {
None => return Err("Could not read the first token of the setup line !"),
None => return Err("Could not read the first token of the setup line !".to_string()),
Some(raw) => raw,
};
let pos_y = match setup.next() {
None => return Err("Could not read the second token of the setup line !"),
None => return Err("Could not read the second token of the setup line !".to_string()),
Some(raw) => raw,
};
let orientation = match setup.next() {
None => return Err("Could not read the third token of the setup line !"),
None => return Err("Could not read the third token of the setup line !".to_string()),
Some(raw) => raw,
};
// Convert values of the setup line
let r_x = match pos_x.parse::<i32>() {
Err(_) => return Err("Could not convert the first token of the setup ligne to i32 !"),
Err(_) => {
return Err(
"Could not convert the first token of the setup ligne to i32 !".to_string(),
)
}
Ok(raw) => raw,
};
let r_y = match pos_y.parse::<i32>() {
Err(_) => return Err("Could not convert the second token of the setup ligne to i32 !"),
Err(_) => {
return Err(
"Could not convert the second token of the setup ligne to i32 !".to_string(),
)
}
Ok(raw) => raw,
};
let r_o = match orientation {
@ -121,12 +136,24 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
"E" => robot::Orientation::E,
"S" => robot::Orientation::S,
"W" => robot::Orientation::W,
_ => return Err("The third token of the setup line do not match any orientations !"),
};
println!("{}", raw_setup);
println!("{}", raw_inst);
_ => {
return Err(
"The third token of the setup line do not match any orientations !".to_string(),
)
}
Ok(world::World { x, y })
};
// Convert instructions line.
let inst: Vec<char> = raw_inst.chars().collect();
let r = robot::Robot::new(r_id, r_o, robot::Position { x: r_x, y: r_y }, inst);
// Load robot inside the pool.
match check_map(&r, &w) {
Ok(()) => pool.push(r),
Err(err) => return Err(err),
}
}
Ok(w)
}
/// Retrieve the content of a file and return it as a string.
@ -154,6 +181,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut robot_pool: Vec<robot::Robot> = Vec::new();
let world: world::World = parse_config(raw_conf, &mut robot_pool)?;
println!("World -> x: {}, y: {}", world.x, world.y);
println!("Number of robot -> {}", robot_pool.len());
println!(
"robot_pool[0] -> id: {}, x: {}, y: {}",
robot_pool[0].id, robot_pool[0].p.x, robot_pool[0].p.y
);
println!(
"robot_pool[1] -> id: {}, x: {}, y: {}",
robot_pool[1].id, robot_pool[1].p.x, robot_pool[1].p.y
);
Ok(())
}