gen-world
This commit is contained in:
parent
ae9dd6eb98
commit
b24d023f1a
52
src/main.rs
52
src/main.rs
@ -73,6 +73,33 @@ fn gen_random_instructions() -> String {
|
|||||||
instructions
|
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.
|
/// Parse the config file, generate the world and robot pool.
|
||||||
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
|
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
|
||||||
let mut lines: Vec<&str> = conf.split('\n').collect();
|
let mut lines: Vec<&str> = conf.split('\n').collect();
|
||||||
@ -213,15 +240,26 @@ 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("gen-world")
|
||||||
|
.short("g")
|
||||||
|
.long("gen-world")
|
||||||
|
.takes_value(false)
|
||||||
|
.help("Generate random world with 2 random robots"),
|
||||||
|
)
|
||||||
.get_matches();
|
.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 robot_pool: Vec<robot::Robot> = Vec::new();
|
||||||
let mut world: world::World = parse_config(raw_conf, &mut robot_pool)?;
|
let world = match matches.is_present("gen-world") {
|
||||||
world = match matches.is_present("random-world") {
|
true => gen_world(&mut robot_pool),
|
||||||
false => world,
|
false => {
|
||||||
true => world::random_world(),
|
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();
|
let mut hash: HashMap<robot::Position, u32> = HashMap::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user