~jpl8/piet_interpreter

44cce5b0adf71082254869f0f5964bab36668393 — jpl 2 years ago 9364aa2
cleanup move_next, add rules
4 files changed, 22 insertions(+), 10 deletions(-)

M .gitignore
M src/interpreter/interpreter_tests.rs
M src/interpreter/mod.rs
A src/rules.rs
M .gitignore => .gitignore +1 -0
@@ 1,1 1,2 @@
/target
/spec

M src/interpreter/interpreter_tests.rs => src/interpreter/interpreter_tests.rs +3 -3
@@ 204,15 204,15 @@ fn move_next_works_turn() {

    let m = PietImage::new(
            (4, 3),
            vec!( vec!( r, r, r, b),
                  vec!( r, g, g, b),
            vec!( vec!( r, b, g, b),
                  vec!( r, b, g, b),
                  vec!( g, g, g, b),
                )
        ); 

    let mut interpreter = Interpreter::from_img(m);

    assert_eq!(interpreter.move_next(), Some((2,1)));
    assert_eq!(interpreter.move_next(), Some((0,2)));
}

#[test]

M src/interpreter/mod.rs => src/interpreter/mod.rs +1 -7
@@ 47,15 47,13 @@ impl Interpreter {
    fn move_next(&mut self) -> Option<(u32, u32)> {
        let curr_codel = self.head.pos;
        let block = &self.img.get_block(curr_codel.0, curr_codel.1);
        let edge = self.head.get_furthest_edge(&block);

        //self.head.pos = codel_on_edge;
       
        let mut cc_switches = 0;
        let mut tries = 0;

        loop {

            let edge = self.head.get_furthest_edge(&block);
            let codel_on_edge = self.head.get_furthest_codel_on_edge(&edge);
            if tries == 8 { return None }



@@ 94,22 92,18 @@ impl Interpreter {
        match self.head.direction_pointer {
            Direction::Up => {
                if on_top_edge || self.img.get(x, y-1).color() == Color::Black { None }
                else if self.img.get(x, y-1).color() == head_color { None }
                else { Some((x, y-1)) }
            }
            Direction::Right => {
                if on_right_edge || self.img.get(x+1, y).color() == Color::Black{ None }
                else if self.img.get(x+1, y).color() == head_color { None }
                else { Some((x+1, y)) }
            }
            Direction::Down => {
                if on_bottom_edge || self.img.get(x, y+1).color() == Color::Black { None }
                else if self.img.get(x, y+1).color() == head_color { None }
                else { Some((x, y+1)) }
            }
            Direction::Left => {
                if on_left_edge || self.img.get(x-1, y).color() == Color::Black { None }
                else if self.img.get(x-1, y).color() == head_color { None }
                else { Some((x-1, y)) }
            }
        }

A src/rules.rs => src/rules.rs +17 -0
@@ 0,0 1,17 @@
enum Ops {
    Push,
    Pop,
    Add,
    Sub,
    Mult,
    Div,
    Mod,
    Not,
    Greater,
    Pointer,
    Switch,
    Duplicate,
    Roll,
    In,
    Out,
}