diff --git a/src/main.rs b/src/main.rs index c4a4c3c..234818d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,52 @@ struct Robot { p: Position, } +impl Robot { + /// Apply forward instruction to the robot. + fn move_forward(&mut self) { + match self.o { + Orientation::N => self.p.y -= 1, + Orientation::S => self.p.y += 1, + Orientation::E => self.p.x += 1, + Orientation::W => self.p.x -= 1, + } + } + /// Apply right instruction to the robot. + fn move_right(&mut self) { + match self.o { + Orientation::N => self.p.x += 1, + Orientation::S => self.p.x -= 1, + Orientation::E => self.p.y += 1, + Orientation::W => self.p.y -= 1, + } + } + /// Apply left instruction to the robot. + fn move_left(&mut self) { + match self.o { + Orientation::N => self.p.x -= 1, + Orientation::S => self.p.x += 1, + Orientation::E => self.p.y -= 1, + Orientation::W => self.p.y += 1, + } + } + /// Apply North orientation to the robot. + fn to_north(&mut self) { + self.o = Orientation::N + } + /// Apply South orientation to the robot. + fn to_south(&mut self) { + self.o = Orientation::S + } + /// Apply East orientation to the robot. + fn to_east(&mut self) { + self.o = Orientation::E + } + /// Apply West orientation to the robot. + fn to_west(&mut self) { + self.o = Orientation::W + } +} + /// Enum to store all possible orientations. enum Orientation { N,