let rec loop s =
       s.dst <- s.cursor;
       (if s.auto_path
        then set_path s);
       my_draw s;
       match getch () with
         | "q" ->
             printf "Quit.\n"
         | "enter" ->
             new_map s;
             loop s
         | "up" | "8" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x, y-1));
             loop s
         | "9" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x+1, y-1));
             loop s
         | "right" | "6" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x+1, y));
             loop s
         | "3" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x+1, y+1));
             loop s
         | "down" | "2" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x, y+1));
             loop s
         | "1" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x-1, y+1));
             loop s
         | "left" | "4" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x-1, y));
             loop s
         | "7" ->
             (match s.cursor with (x,y) ->
                set_cursor_ifvalid s (x-1, y-1));
             loop s
         | "s" ->
             s.src <- s.cursor;
             loop s
         | "a" ->
             s.alg <- (s.alg+1) mod 3;
             loop s
         | "z" ->
             debug_path s;
             loop s
         | "x" ->
             debug_opt s;
             loop s
         | "p" ->
             s.auto_path <- not s.auto_path;
             s.path <- [];
             loop s
         | "d" ->
             s.dist_alg <- (s.dist_alg+1) mod 2;
             loop s
         | "o" ->
             s.path_opt_alg <- (s.path_opt_alg+1) mod 3;
             loop s
         | "m" ->
             s.map_type <- (s.map_type+1) mod 2;
             loop s
         | _ ->
             loop s