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-10-20 12:48:28 +02:00
|
|
|
use clap::{App, Arg};
|
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-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 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-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-10-28 16:17:34 +01:00
|
|
|
let mut lines = conf.lines();
|
2020-10-28 13:09:07 +01:00
|
|
|
// The first line of the config file should be the World.
|
2020-10-28 14:06:03 +01:00
|
|
|
let raw_line: &str = match lines.next() {
|
2020-10-28 13:09:07 +01:00
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the first line of the config file !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-28 13:09:07 +01:00
|
|
|
};
|
2020-10-28 16:42:36 +01:00
|
|
|
let mut tokens = raw_line.split_whitespace();
|
2020-10-28 16:50:49 +01:00
|
|
|
let token1 = match tokens.next() {
|
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the first token of the first line !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-28 16:50:49 +01:00
|
|
|
};
|
|
|
|
let token2 = match tokens.next() {
|
|
|
|
Some(raw) => raw,
|
2020-10-30 15:29:52 +01:00
|
|
|
None => {
|
|
|
|
return Err(String::from(
|
|
|
|
"Could not read the second token of the first line !",
|
|
|
|
))
|
|
|
|
}
|
2020-10-28 18:05:22 +01:00
|
|
|
};
|
|
|
|
let x: i32 = match token1.parse::<i32>() {
|
|
|
|
Ok(x) => x,
|
2020-10-30 13:23:17 +01:00
|
|
|
Err(_) => {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from(
|
|
|
|
"Could not convert token one from the first string to i32",
|
|
|
|
))
|
2020-10-30 13:23:17 +01:00
|
|
|
}
|
2020-10-28 18:05:22 +01:00
|
|
|
};
|
|
|
|
let y: i32 = match token2.parse::<i32>() {
|
|
|
|
Ok(x) => x,
|
2020-10-30 13:23:17 +01:00
|
|
|
Err(_) => {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from(
|
|
|
|
"Could not convert token two from the first string to i32",
|
|
|
|
))
|
2020-10-30 13:23:17 +01:00
|
|
|
}
|
2020-10-28 16:50:49 +01:00
|
|
|
};
|
2020-10-30 13:23:17 +01:00
|
|
|
let w = world::World { x, y };
|
|
|
|
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-10-29 14:34:04 +01:00
|
|
|
// This line should be empty.
|
|
|
|
let empty_line = match lines.next() {
|
|
|
|
None => break,
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
if !empty_line.is_empty() {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from("This line should be empty !"));
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-10-29 14:52:43 +01:00
|
|
|
let raw_setup = match lines.next() {
|
2020-10-30 15:29:52 +01:00
|
|
|
None => return Err(String::from("This line should be the config !")),
|
2020-10-29 14:34:04 +01:00
|
|
|
Some(raw) => raw,
|
|
|
|
};
|
2020-10-29 14:52:43 +01:00
|
|
|
if raw_setup.is_empty() {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from("This line should not be empty !"));
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-10-29 14:52:43 +01:00
|
|
|
let raw_inst = match lines.next() {
|
2020-10-30 15:29:52 +01:00
|
|
|
None => return Err(String::from("This line should be the instruction !")),
|
2020-10-29 14:34:04 +01:00
|
|
|
Some(raw) => raw,
|
|
|
|
};
|
2020-10-29 14:52:43 +01:00
|
|
|
if raw_inst.is_empty() {
|
2020-10-30 15:29:52 +01:00
|
|
|
return Err(String::from("This line should not be empty !"));
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-10-30 12:08:12 +01:00
|
|
|
// Parse the setup line of the robot.
|
2020-10-30 11:56:24 +01:00
|
|
|
let mut setup = raw_setup.split_whitespace();
|
|
|
|
let pos_x = match setup.next() {
|
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
|
|
|
Some(raw) => raw,
|
|
|
|
};
|
|
|
|
let pos_y = match setup.next() {
|
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
|
|
|
Some(raw) => raw,
|
|
|
|
};
|
|
|
|
let orientation = match setup.next() {
|
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
|
|
|
Some(raw) => raw,
|
|
|
|
};
|
2020-10-30 12:08:12 +01:00
|
|
|
// Convert values of the setup line
|
|
|
|
let r_x = match pos_x.parse::<i32>() {
|
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
|
|
|
Ok(raw) => raw,
|
|
|
|
};
|
|
|
|
let r_y = match pos_y.parse::<i32>() {
|
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
|
|
|
Ok(raw) => raw,
|
|
|
|
};
|
|
|
|
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-10-31 17:40:53 +01:00
|
|
|
let inst: Vec<char> = raw_inst.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.
|
|
|
|
match check_map(&r, &w) {
|
|
|
|
Ok(()) => pool.push(r),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
2020-10-29 14:34:04 +01:00
|
|
|
}
|
2020-10-30 16:11:13 +01:00
|
|
|
|
2020-10-30 13:23:17 +01:00
|
|
|
Ok(w)
|
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-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"),
|
|
|
|
)
|
|
|
|
.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();
|
|
|
|
let world: world::World = parse_config(raw_conf, &mut robot_pool)?;
|
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-10-28 12:00:17 +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
|
|
|
}
|