Pull 2D turning into grid module

This commit is contained in:
kageru 2019-12-15 12:00:28 +01:00
parent 10b1b09fda
commit 5b007567c4
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 23 additions and 22 deletions

View File

@ -9,24 +9,6 @@ struct Robot {
}
impl Robot {
fn turn(&mut self, turn_value: i64) {
self.direction = match turn_value {
0 => match self.direction {
Direction::Up => Direction::Left,
Direction::Right => Direction::Up,
Direction::Down => Direction::Right,
Direction::Left => Direction::Down,
},
1 => match self.direction {
Direction::Up => Direction::Right,
Direction::Right => Direction::Down,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
},
_ => unreachable!("Illegal turn value"),
}
}
fn mov(&mut self) {
self.position.mov(&self.direction);
}
@ -50,7 +32,7 @@ fn start_with_input(input: Vec<i64>, color: i64) -> Robot {
while let IntComputerResult::Output(o) = pc.run() {
robot.paint(o);
let turn_int = pc.run().unwrap();
robot.turn(turn_int);
robot.direction.turn(turn_int * 2 - 1);
robot.mov();
pc.params.push(robot.current_color());
}

View File

@ -10,6 +10,7 @@ pub struct Position2D {
pub y: i64,
}
#[derive(Clone, Copy)]
pub enum Direction {
Up,
Down,
@ -53,6 +54,27 @@ pub fn draw_ascii<T: Display, S: BuildHasher>(
)
}
impl Direction {
pub fn turn(&mut self, turn_value: i64) {
*self = match turn_value {
-1 => match self {
Direction::Up => Direction::Left,
Direction::Right => Direction::Up,
Direction::Down => Direction::Right,
Direction::Left => Direction::Down,
},
1 => match self {
Direction::Up => Direction::Right,
Direction::Right => Direction::Down,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
},
0 => *self,
n => unreachable!(format!("Illegal turn value: {}", n)),
}
}
}
impl Position2D {
pub fn mov(&mut self, dir: &Direction) {
*self = *self
@ -90,6 +112,3 @@ impl From<(i64, i64)> for Position2D {
}
}
}
#[cfg(test)]
mod tests {}