Pull 2D turning into grid module
This commit is contained in:
parent
10b1b09fda
commit
5b007567c4
|
@ -9,24 +9,6 @@ struct Robot {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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) {
|
fn mov(&mut self) {
|
||||||
self.position.mov(&self.direction);
|
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() {
|
while let IntComputerResult::Output(o) = pc.run() {
|
||||||
robot.paint(o);
|
robot.paint(o);
|
||||||
let turn_int = pc.run().unwrap();
|
let turn_int = pc.run().unwrap();
|
||||||
robot.turn(turn_int);
|
robot.direction.turn(turn_int * 2 - 1);
|
||||||
robot.mov();
|
robot.mov();
|
||||||
pc.params.push(robot.current_color());
|
pc.params.push(robot.current_color());
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ pub struct Position2D {
|
||||||
pub y: i64,
|
pub y: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Up,
|
Up,
|
||||||
Down,
|
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 {
|
impl Position2D {
|
||||||
pub fn mov(&mut self, dir: &Direction) {
|
pub fn mov(&mut self, dir: &Direction) {
|
||||||
*self = *self
|
*self = *self
|
||||||
|
@ -90,6 +112,3 @@ impl From<(i64, i64)> for Position2D {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user