implement orientation change and instructions to the robot

This commit is contained in:
Volodymyr Patuta 2020-10-20 11:53:53 +02:00
parent 659c79ae04
commit e58313d76c
1 changed files with 46 additions and 0 deletions

View File

@ -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,