From 4fc0f07e78eac5ac75585d96d0025c54e09dc2e4 Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 09:28:13 +0100 Subject: [PATCH 1/7] declare prototype func collision and check if robot is in the map --- src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.rs b/src/main.rs index 850f528..3ee6291 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,13 @@ use std::io; mod robot; mod world; +fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), &'static str> {} +fn check_collisions( + w: &world::World, + r: &robot::Robot, + h: &collection::HashMap, +) -> Result<(), &'static str> { +} /// Parse the config file, generate the world and robot pool. fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines = conf.lines(); From 2eaf624137247baff9b08bc5a47bd455bafd4bef Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:05:54 +0100 Subject: [PATCH 2/7] function to check if robot is in the map --- src/main.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3ee6291..62906d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,13 +20,15 @@ use std::io; mod robot; mod world; -fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), &'static str> {} -fn check_collisions( - w: &world::World, - r: &robot::Robot, - h: &collection::HashMap, -) -> Result<(), &'static str> { +/// Check if the robot is in the map. +fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> { + if (r.p.x, r.p.y) < (0, 0) && (r.p.x, r.p.y) > (w.x, w.y) { + Err(format!("The robot {} is off map", r.id)) + } else { + Ok(()) + } } + /// Parse the config file, generate the world and robot pool. fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines = conf.lines(); From d35da41c90adbb8f58cd214ba1cae9de8859f147 Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:16:09 +0100 Subject: [PATCH 3/7] test of the function check_map --- src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 62906d5..2b816e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ mod world; /// Check if the robot is in the map. fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> { - if (r.p.x, r.p.y) < (0, 0) && (r.p.x, r.p.y) > (w.x, w.y) { + if (r.p.x, r.p.y) < (0, 0) || (r.p.x, r.p.y) > (w.x, w.y) { Err(format!("The robot {} is off map", r.id)) } else { Ok(()) @@ -93,4 +93,31 @@ mod tests { assert!(open_file("two_robots.txt").is_ok()); assert!(open_file("test_unexisting_file.extension").is_err()); } + + #[test] + fn test_check_map() { + let mut r = robot::Robot::new( + 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() { + let mut r = robot::Robot::new( + 0, + robot::Orientation::N, + robot::Position { x: 2, y: 3 }, + vec!['F'], + ); + let w = world::World { x: 1, y: 1 }; + + assert!(check_map(&r, &w).is_ok()); + } } From e4b25281f7702987d0d9de3bca66ebee21e88cab Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:30:11 +0100 Subject: [PATCH 4/7] function to check collisions at given position --- src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2b816e8..8111819 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ // along with this program. If not, see . use clap::{App, Arg}; +use std::collections::HashMap; use std::fs; use std::io; @@ -28,7 +29,19 @@ fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> { Ok(()) } } - +fn check_collisions( + r: &robot::Robot, + w: &world::World, + h: HashMap<&robot::Position, &u32>, +) -> Result<(), String> { + match h.get(&r.p) { + Some(&x) => Err(format!( + "The robot id: {} collided with robot id: {} in position: ({};{}).", + &r.id, x, &r.p.x, &r.p.y + )), + None => Ok(()), + } +} /// Parse the config file, generate the world and robot pool. fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines = conf.lines(); From 782fccf31cdf543bec4dc4903d14a7711dc2819d Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:32:32 +0100 Subject: [PATCH 5/7] Added function's description to the doc for check_collisions --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 8111819..90adc6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> { Ok(()) } } +/// Check if the robot collide with another one at the given position. fn check_collisions( r: &robot::Robot, w: &world::World, From 506a71f678de00d537638aed4492c28a6689db5d Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:47:49 +0100 Subject: [PATCH 6/7] added test of function check collisions --- src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 90adc6d..c9abe05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,11 +29,12 @@ fn check_map(r: &robot::Robot, w: &world::World) -> Result<(), String> { Ok(()) } } + /// Check if the robot collide with another one at the given position. fn check_collisions( r: &robot::Robot, w: &world::World, - h: HashMap<&robot::Position, &u32>, + h: &HashMap<&robot::Position, &u32>, ) -> Result<(), String> { match h.get(&r.p) { Some(&x) => Err(format!( @@ -43,6 +44,7 @@ fn check_collisions( None => Ok(()), } } + /// Parse the config file, generate the world and robot pool. fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines = conf.lines(); @@ -134,4 +136,20 @@ mod tests { assert!(check_map(&r, &w).is_ok()); } + + #[test] + #[should_panic] + fn test_check_collisions() { + let mut r = robot::Robot::new( + 0, + robot::Orientation::N, + robot::Position { x: 2, y: 3 }, + vec!['F'], + ); + let w = world::World { x: 10, y: 10 }; + + let mut h: HashMap<&robot::Position, &u32> = HashMap::new(); + h.insert(&robot::Position { x: 2, y: 3 }, &1); + assert!(check_collisions(&r, &w, &h).is_ok()); + } } From 15d8cc473a43876461a79c63d1413537d5a46a2c Mon Sep 17 00:00:00 2001 From: EliasCubz Date: Thu, 29 Oct 2020 10:52:06 +0100 Subject: [PATCH 7/7] change of style --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c9abe05..814bd7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ fn check_collisions( ) -> Result<(), String> { match h.get(&r.p) { Some(&x) => Err(format!( - "The robot id: {} collided with robot id: {} in position: ({};{}).", + "The robot id: {} collided with robot id: {} in position: ({};{}) !", &r.id, x, &r.p.x, &r.p.y )), None => Ok(()),