gen-world

This commit is contained in:
Volodymyr Patuta 2020-11-08 13:28:58 +01:00
parent ae9dd6eb98
commit b24d023f1a
1 changed files with 45 additions and 7 deletions

View File

@ -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,15 +240,26 @@ 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 mut world: world::World = parse_config(raw_conf, &mut robot_pool)?;
world = match matches.is_present("random-world") {
false => world,
true => world::random_world(),
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();