Pull 2D turning into grid module
This commit is contained in:
parent
10b1b09fda
commit
5b007567c4
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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 {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user