From c9ad3107007d4172ec403bfd15f54686986fb613 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 17:30:45 +0100 Subject: [PATCH] create and move robot + tests --- src/robot.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/robot.rs b/src/robot.rs index a9d6e6a..da57e61 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -1,9 +1,44 @@ /// A Robot *aka droid* is represented here. /// Each robot must have a unique id. pub struct Robot { - id: u32, + id: i32, o: Orientation, p: Position, + i: Vec, +} + +impl Robot { + /// Create new `Robot` with given id, `Orientation`, `Position` and instructions. + fn new(id: i32, o: Orientation, p: Position, i: Vec) -> Robot { + Robot { id, o, p, i } + } + /// Apply given instruction to a `Robot`. + fn execute_instruction(&mut self) -> Result<(), &'static str> { + match self.i.pop() { + Some(instruction) => match instruction { + 'L' => Ok(match self.o { + Orientation::N => self.o = Orientation::W, + Orientation::E => self.o = Orientation::N, + Orientation::S => self.o = Orientation::E, + Orientation::W => self.o = Orientation::S, + }), + 'R' => Ok(match self.o { + Orientation::N => self.o = Orientation::E, + Orientation::E => self.o = Orientation::S, + Orientation::S => self.o = Orientation::W, + Orientation::W => self.o = Orientation::N, + }), + 'F' => Ok(match self.o { + Orientation::N => self.p.y += 1, + Orientation::E => self.p.x += 1, + Orientation::S => self.p.y -= 1, + Orientation::W => self.p.x -= 1, + }), + _ => Err("Invalid instruction."), + }, + None => Ok(()), + } + } } /// Enum to store all possible orientations. @@ -19,3 +54,36 @@ struct Position { x: i32, y: i32, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_new_robot() { + let r: Robot = Robot::new( + 0, + Orientation::N, + Position { x: 1, y: 2 }, + vec!['L', 'L', 'F', 'R'], + ); + assert_eq!(r.id, 0); + assert!(matches!(r.o, Orientation::N)); + assert_eq!(r.p.x, 1); + assert_eq!(r.p.y, 2); + assert_eq!(r.i, vec!['L', 'L', 'F', 'R']); + } + + #[test] + fn test_execute_instruction() { + let mut r: Robot = Robot::new( + 0, + Orientation::N, + Position { x: 1, y: 2 }, + vec!['R', 'L', 'F', 'F'], + ); + assert!(r.execute_instruction().is_ok()); + assert_eq!(r.p.x, 1); + assert_eq!(r.p.y, 3); + } +}