2020-10-26 13:55:05 +01:00
|
|
|
// Dancing Droids
|
2020-10-26 15:29:14 +01:00
|
|
|
// Copyright (C) 2020 Martin HART, Volodymyr PATUTA, Stephane Elias BENABDESLAM
|
2020-10-26 13:55:05 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-11-05 00:06:48 +01:00
|
|
|
extern crate pest;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate pest_derive;
|
|
|
|
|
2020-10-20 12:48:28 +02:00
|
|
|
use clap::{App, Arg};
|
2020-11-05 00:06:48 +01:00
|
|
|
use pest::Parser;
|
|
|
|
use rand::Rng;
|
2020-10-29 10:30:11 +01:00
|
|
|
use std::collections::HashMap;
|
2020-10-13 10:31:50 +02:00
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
|
|
|
|
2020-10-27 22:55:31 +01:00
|
|
|
mod robot;
|
2020-10-28 12:36:56 +01:00
|
|
|
mod world;
|
|
|
|
|
2020-11-05 00:06:48 +01:00
|
|
|
#[derive(Parser)]
|
2020-11-05 16:47:22 +01:00
|
|
|
#[grammar = "../conf.pest"]
|
2020-11-05 00:06:48 +01:00
|
|
|
pub struct ConfParser;
|
|
|
|
|
2020-10-29 10:05:54 +01:00
|
|
|
/// Check if the robot is in the map.
|
|
|
|
fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> {
|
2020-10-30 15:59:01 +01:00
|
|
|
if r.p.x < 0 || r.p.y < 0 || r.p.x > w.x || r.p.y > w.y {
|
2020-10-29 10:05:54 +01:00
|
|
|
Err(format!("The robot {} is off map", r.id))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-29 09:28:13 +01:00
|
|
|
}
|
2020-10-29 10:47:49 +01:00
|
|
|
|
2020-10-29 10:32:32 +01:00
|
|
|
/// Check if the robot collide with another one at the given position.
|
2020-10-31 17:55:47 +01:00
|
|
|
fn check_collisions(r: &robot::Robot, h: &HashMap<robot::Position, u32>) -> Result<(), String> {
|
2020-10-29 10:30:11 +01:00
|
|
|
match h.get(&r.p) {
|
|
|
|
Some(&x) => Err(format!(
|
2020-10-29 10:52:06 +01:00
|
|
|
"The robot id: {} collided with robot id: {} in position: ({};{}) !",
|
2020-10-29 10:30:11 +01:00
|
|
|
&r.id, x, &r.p.x, &r.p.y
|
|
|
|
)),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 10:47:49 +01:00
|
|
|
|
2020-10-31 20:42:26 +01:00
|
|
|
/// Creates HashMap of robot position and it's id.
|
2020-10-31 18:05:23 +01:00
|
|
|
fn create_hash_map(pool: &Vec<robot::Robot>, hash: &mut HashMap<robot::Position, u32>) {
|
|
|
|
for r in pool {
|
|
|
|
hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 00:06:48 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:36:56 +01:00
|
|
|
/// Parse the config file, generate the world and robot pool.
|
2020-10-30 13:23:17 +01:00
|
|
|
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
|
2020-11-05 00:06:48 +01:00
|
|
|
let mut lines: Vec<&str> = conf.split('\n').collect();
|
|
|
|
let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) {
|
|
|
|
Ok(s) => s.as_str(),
|
|
|
|
Err(e) => return Err(format!("{}", e)),
|
2020-10-28 16:50:49 +01:00
|
|
|
};
|
2020-11-05 00:06:48 +01:00
|
|
|
let mut w: Vec<i32> = Vec::with_capacity(2);
|
|
|
|
for n in raw_world.split_whitespace() {
|
|
|
|
let v: i32 = match n.parse::<i32>() {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return Err(String::from("World config is broken.")),
|
|
|
|
};
|
|
|
|
w.push(v);
|
|
|
|
}
|
|
|
|
let world = world::World { x: w[0], y: w[1] };
|
|
|
|
lines.remove(0);
|
2020-10-30 13:23:17 +01:00
|
|
|
let mut r_id: u32 = 0;
|
2020-10-29 14:34:04 +01:00
|
|
|
loop {
|
2020-10-30 13:23:17 +01:00
|
|
|
r_id += 1;
|
2020-11-05 00:06:48 +01:00
|
|
|
if lines.len() == 0 {
|
|
|
|
break;
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-11-05 00:06:48 +01:00
|
|
|
let raw_setup = match ConfParser::parse(Rule::robot_init, lines.remove(0)) {
|
|
|
|
Ok(s) => s.as_str(),
|
|
|
|
Err(e) => return Err(format!("{}", e)),
|
2020-10-29 14:34:04 +01:00
|
|
|
};
|
2020-11-05 00:06:48 +01:00
|
|
|
|
|
|
|
let rand_instructions = gen_random_instructions();
|
|
|
|
let l = lines.remove(0);
|
|
|
|
|
|
|
|
let instructions = match ConfParser::parse(Rule::robot_instructions, l) {
|
|
|
|
Ok(s) => s.as_str(),
|
|
|
|
Err(_) => rand_instructions.as_str(),
|
2020-10-29 14:34:04 +01:00
|
|
|
};
|
2020-11-05 00:06:48 +01:00
|
|
|
|
2020-10-30 11:56:24 +01:00
|
|
|
let mut setup = raw_setup.split_whitespace();
|
|
|
|
let pos_x = match setup.next() {
|
2020-11-05 00:06:48 +01:00
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the first token of the setup line !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-30 11:56:24 +01:00
|
|
|
};
|
|
|
|
let pos_y = match setup.next() {
|
2020-11-05 00:06:48 +01:00
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the second token of the setup line !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-30 11:56:24 +01:00
|
|
|
};
|
|
|
|
let orientation = match setup.next() {
|
2020-11-05 00:06:48 +01:00
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the third token of the setup line !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-30 11:56:24 +01:00
|
|
|
};
|
2020-10-30 12:08:12 +01:00
|
|
|
// Convert values of the setup line
|
|
|
|
let r_x = match pos_x.parse::<i32>() {
|
2020-11-05 00:06:48 +01:00
|
|
|
Ok(raw) => raw,
|
2020-10-30 13:23:17 +01:00
|
|
|
Err(_) => {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from(
|
|
|
|
"Could not convert the first token of the setup ligne to i32 !",
|
|
|
|
))
|
2020-10-30 13:23:17 +01:00
|
|
|
}
|
2020-10-30 12:08:12 +01:00
|
|
|
};
|
|
|
|
let r_y = match pos_y.parse::<i32>() {
|
2020-11-05 00:06:48 +01:00
|
|
|
Ok(raw) => raw,
|
2020-10-30 13:23:17 +01:00
|
|
|
Err(_) => {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from(
|
|
|
|
"Could not convert the second token of the setup ligne to i32 !",
|
|
|
|
))
|
2020-10-30 13:23:17 +01:00
|
|
|
}
|
2020-10-30 12:08:12 +01:00
|
|
|
};
|
|
|
|
let r_o = match orientation {
|
|
|
|
"N" => robot::Orientation::N,
|
|
|
|
"E" => robot::Orientation::E,
|
|
|
|
"S" => robot::Orientation::S,
|
|
|
|
"W" => robot::Orientation::W,
|
2020-10-30 13:23:17 +01:00
|
|
|
_ => {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from(
|
|
|
|
"The third token of the setup line do not match any orientations !",
|
|
|
|
))
|
2020-10-30 13:23:17 +01:00
|
|
|
}
|
2020-10-30 12:08:12 +01:00
|
|
|
};
|
2020-10-30 13:23:17 +01:00
|
|
|
// Convert instructions line.
|
2020-11-05 00:06:48 +01:00
|
|
|
let inst: Vec<char> = instructions.chars().rev().collect();
|
2020-10-30 16:38:12 +01:00
|
|
|
if !robot::is_instructions(&inst) {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from("Invalid instructions !"));
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:23:17 +01:00
|
|
|
let r = robot::Robot::new(r_id, r_o, robot::Position { x: r_x, y: r_y }, inst);
|
|
|
|
|
|
|
|
// Load robot inside the pool.
|
2020-11-05 00:06:48 +01:00
|
|
|
match check_map(&r, &world) {
|
2020-10-30 13:23:17 +01:00
|
|
|
Ok(()) => pool.push(r),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
2020-11-05 00:06:48 +01:00
|
|
|
|
|
|
|
if lines.len() == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if l.len() == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lines.remove(0);
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-10-30 16:11:13 +01:00
|
|
|
|
2020-11-05 00:06:48 +01:00
|
|
|
Ok(world)
|
2020-10-28 12:36:56 +01:00
|
|
|
}
|
2020-10-27 22:55:31 +01:00
|
|
|
|
2020-10-17 18:16:40 +02:00
|
|
|
/// Retrieve the content of a file and return it as a string.
|
2020-10-13 10:19:34 +02:00
|
|
|
fn open_file(filename: &str) -> io::Result<String> {
|
|
|
|
let content = fs::read_to_string(filename)?;
|
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:45:51 +01:00
|
|
|
/// Here we display the grid by looping in every position checking if it exists in the HashMap.
|
2020-11-01 15:49:02 +01:00
|
|
|
fn display_grid(
|
|
|
|
w: &world::World,
|
|
|
|
robot_pool: &Vec<robot::Robot>,
|
|
|
|
h: &HashMap<robot::Position, u32>,
|
|
|
|
) {
|
|
|
|
for i in (0..w.y).rev() {
|
2020-11-05 13:53:02 +01:00
|
|
|
if i < 10 {
|
|
|
|
print!("{} ", i);
|
|
|
|
} else {
|
|
|
|
print!("{} ", i);
|
|
|
|
}
|
2020-11-01 15:49:02 +01:00
|
|
|
for j in 0..w.x {
|
2020-11-01 16:04:33 +01:00
|
|
|
match h.get(&robot::Position { x: j, y: i }) {
|
|
|
|
Some(id) => match robot_pool[(id - 1) as usize].o {
|
2020-11-01 16:21:27 +01:00
|
|
|
robot::Orientation::N => print!("↑ "),
|
|
|
|
robot::Orientation::E => print!("→ "),
|
|
|
|
robot::Orientation::S => print!("↓ "),
|
|
|
|
robot::Orientation::W => print!("← "),
|
2020-10-31 23:10:41 +01:00
|
|
|
},
|
2020-11-01 16:04:33 +01:00
|
|
|
|
2020-11-01 16:21:27 +01:00
|
|
|
None => print!(". "),
|
2020-11-01 15:49:02 +01:00
|
|
|
}
|
2020-10-31 23:10:41 +01:00
|
|
|
}
|
2020-11-01 15:55:06 +01:00
|
|
|
println!();
|
|
|
|
}
|
2020-11-01 16:21:27 +01:00
|
|
|
print!(" ");
|
2020-11-01 16:04:33 +01:00
|
|
|
for j in 0..w.x {
|
2020-11-05 13:53:02 +01:00
|
|
|
if j < 10 {
|
|
|
|
print!("{} ", j);
|
|
|
|
} else {
|
|
|
|
print!("{} ", j);
|
|
|
|
}
|
2020-10-31 23:10:41 +01:00
|
|
|
}
|
2020-11-01 16:21:27 +01:00
|
|
|
println!();
|
2020-10-31 23:10:41 +01:00
|
|
|
}
|
2020-11-01 20:24:38 +01:00
|
|
|
|
2020-10-19 12:50:48 +02:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2020-10-20 13:42:32 +02:00
|
|
|
// We handle CLI flags here.
|
2020-10-20 12:48:28 +02:00
|
|
|
let matches = App::new("DancingDroids")
|
|
|
|
.version("0.1.0")
|
|
|
|
.about("When droids dance togethers")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("file")
|
|
|
|
.short("f")
|
|
|
|
.long("file")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Configuration file"),
|
|
|
|
)
|
2020-11-04 18:10:42 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("random-world")
|
|
|
|
.long("random-world")
|
|
|
|
.takes_value(false)
|
|
|
|
.help("Generate random world"),
|
|
|
|
)
|
2020-10-20 12:48:28 +02:00
|
|
|
.get_matches();
|
|
|
|
|
2020-10-20 13:17:50 +02:00
|
|
|
let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?;
|
2020-10-20 14:06:10 +02:00
|
|
|
|
2020-10-28 16:17:34 +01:00
|
|
|
let mut robot_pool: Vec<robot::Robot> = Vec::new();
|
2020-11-05 00:34:25 +01:00
|
|
|
let world: world::World = match matches.is_present("random-world") {
|
|
|
|
false => parse_config(raw_conf, &mut robot_pool)?,
|
|
|
|
true => world::random_world(),
|
2020-11-04 21:43:02 +01:00
|
|
|
};
|
|
|
|
|
2020-10-31 18:05:23 +01:00
|
|
|
let mut hash: HashMap<robot::Position, u32> = HashMap::new();
|
|
|
|
create_hash_map(&robot_pool, &mut hash);
|
2020-11-01 21:19:36 +01:00
|
|
|
println!("World {{ x_max = {}; y_max = {} }}", world.x, world.y);
|
|
|
|
robot::print_robots(&robot_pool);
|
2020-11-01 20:23:47 +01:00
|
|
|
println!("Initial state");
|
|
|
|
println!("==============");
|
2020-11-01 16:21:27 +01:00
|
|
|
display_grid(&world, &robot_pool, &hash);
|
2020-10-31 19:23:04 +01:00
|
|
|
loop {
|
|
|
|
let mut piouff: u32 = 0;
|
2020-10-31 20:21:21 +01:00
|
|
|
for r in &mut robot_pool {
|
2020-10-31 18:50:29 +01:00
|
|
|
if robot::is_piouff(&r) {
|
|
|
|
piouff += 1;
|
|
|
|
} else {
|
2020-10-31 20:21:21 +01:00
|
|
|
hash.remove(&r.p);
|
2020-10-31 18:50:29 +01:00
|
|
|
r.execute_instruction();
|
2020-10-31 20:21:21 +01:00
|
|
|
check_map(&r, &world)?;
|
|
|
|
check_collisions(&r, &hash)?;
|
|
|
|
hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id);
|
2020-10-31 18:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 19:23:04 +01:00
|
|
|
if piouff == robot_pool.len() as u32 {
|
|
|
|
break;
|
|
|
|
}
|
2020-10-31 18:50:29 +01:00
|
|
|
}
|
2020-11-01 20:23:47 +01:00
|
|
|
println!("Final state");
|
|
|
|
println!("============");
|
|
|
|
display_grid(&world, &robot_pool, &hash);
|
2020-10-31 20:21:21 +01:00
|
|
|
for r in &robot_pool {
|
|
|
|
println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y);
|
|
|
|
}
|
2020-10-31 18:50:29 +01:00
|
|
|
|
2020-10-19 12:50:48 +02:00
|
|
|
Ok(())
|
2020-10-11 15:14:20 +02:00
|
|
|
}
|
2020-10-13 12:07:47 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-10-13 12:15:04 +02:00
|
|
|
#[test]
|
|
|
|
fn test_open_file() {
|
|
|
|
assert!(open_file("two_robots.txt").is_ok());
|
|
|
|
assert!(open_file("test_unexisting_file.extension").is_err());
|
|
|
|
}
|
2020-10-29 10:16:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_map() {
|
2020-10-30 16:38:12 +01:00
|
|
|
let r = robot::Robot::new(
|
2020-10-29 10:16:09 +01:00
|
|
|
0,
|
|
|
|
robot::Orientation::N,
|
|
|
|
robot::Position { x: 2, y: 3 },
|
|
|
|
vec!['F'],
|
|
|
|
);
|
|
|
|
let w = world::World { x: 10, y: 10 };
|
|
|
|
|
|
|
|
assert!(check_map(&r, &w).is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_check_map_fail() {
|
2020-10-30 16:38:12 +01:00
|
|
|
let r = robot::Robot::new(
|
2020-10-29 10:16:09 +01:00
|
|
|
0,
|
|
|
|
robot::Orientation::N,
|
2020-10-30 15:59:01 +01:00
|
|
|
robot::Position { x: 2, y: 4 },
|
2020-10-29 10:16:09 +01:00
|
|
|
vec!['F'],
|
|
|
|
);
|
2020-10-30 15:59:01 +01:00
|
|
|
let w = world::World { x: 3, y: 3 };
|
2020-10-29 10:16:09 +01:00
|
|
|
|
|
|
|
assert!(check_map(&r, &w).is_ok());
|
|
|
|
}
|
2020-10-29 10:47:49 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_check_collisions() {
|
2020-10-30 16:38:12 +01:00
|
|
|
let r = robot::Robot::new(
|
2020-10-29 10:47:49 +01:00
|
|
|
0,
|
|
|
|
robot::Orientation::N,
|
|
|
|
robot::Position { x: 2, y: 3 },
|
|
|
|
vec!['F'],
|
|
|
|
);
|
|
|
|
|
2020-10-31 17:55:47 +01:00
|
|
|
let mut h: HashMap<robot::Position, u32> = HashMap::new();
|
|
|
|
h.insert(robot::Position { x: 2, y: 3 }, 1);
|
|
|
|
assert!(check_collisions(&r, &h).is_ok());
|
2020-10-29 10:47:49 +01:00
|
|
|
}
|
2020-10-13 12:07:47 +02:00
|
|
|
}
|