Merge branch 'volodymyr-dev' into 'master'

implement orientation change and instructions to the robot

See merge request mhart/DancingDroids!15
This commit is contained in:
Martin HART 2020-10-20 12:15:59 +02:00
commit 1bd216c50b
1 changed files with 46 additions and 0 deletions

View File

@ -22,6 +22,52 @@ struct Robot {
p: Position, 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 to store all possible orientations.
enum Orientation { enum Orientation {
N, N,