From 62433b0fd2f573f10f3cd8a7f873569ebc024654 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Mon, 26 Oct 2020 13:32:14 +0100 Subject: [PATCH] change position x,y type + method is_valid and test for move robot --- src/main.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47f69ed..b483166 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,19 @@ impl World { /// Struct to store robot position. struct Position { - x: u32, - y: u32, + x: i32, + y: i32, +} + +impl Position { + /// Check if position is in the map. + fn is_valid(self, w: World) -> Result<(), String> { + if (self.x, self.y) > (0, 0) && (self.x, self.y) < (w.x as i32, w.y as i32) { + Ok(()) + } else { + Err(String::from("Invalid position.")) + } + } } /// A Robot *aka droid* is represented here. @@ -257,6 +268,22 @@ mod tests { assert_eq!(w.map, vec!['↑', '.', '.', '.']); } + #[test] + fn test_move_robot() { + let mut r: Robot = Robot { + id: 0, + o: Orientation::N, + p: Position { x: 1, y: 1 }, + q: Queue::new(), + }; + r.move_forward(); + assert_eq!(0, r.p.y); + r.move_right(); + assert_eq!(2, r.p.x); + r.move_left(); + assert_eq!(1, r.p.x); + } + #[test] fn test_empty_position() { let mut w: World = World {