function moves = LEM_rand_grid_path(r1,c1,r2,c2)
% LEM_rand_grid_path - choose randomly from shortest paths
% On input:
%     r1 (int): row of first grid point
%     c1 (int): col of first grid point
%     r2 (int): row of second grid point
%     c2 (int): col of second grid point
% On output:
%     moves (px2 array): [r_move,y_move]
%       signed moves to traverse grid from (r1,c1) to (r2,c2)
%       moves alternate in rows and columns
% Call:
%     moves = LEM_rand_grid_path(3,1,1,4);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

PROB_X_MOVE = 0.5;

m = abs(r2-r1);
n = abs(c2-c1);
del_y = sign(r2-r1);
del_x = sign(c2-c1);
b = rand<PROB_X_MOVE;
if b==0
    xm = del_x;
    ym = 0;
    xs = 1;
    ys = 0;
else
    xm = 0;
    ym = del_y;
    xs = 0;
    ys = 1;
end
moves = [xm,ym];
b = 1 - b;
done = 0;
while done==0
    xm = 0;
    ym = 0;
    if b==0
        b = 1;
        if ys==m
            xm = del_x*(n-xs);
        elseif xs<n
            xm = del_x*(rand<PROB_X_MOVE);
        end
    else
        b = 0;
        if xs==n
            ym = del_y*(m-ys);
        elseif ys<m
            ym = del_y*(rand>=PROB_X_MOVE);
        end
    end
    if abs(xm)+abs(ym)>0
        moves = [moves;xm,ym];
        xs = xs + abs(xm);
        ys = ys + abs(ym);
        if xs==n&ys==m
            done = 1;
        end
    end
end
tmp = moves;
moves(:,1) = tmp(:,2);
moves(:,2) = tmp(:,1);
tmp = moves;
num_tmp = length(tmp(:,1));
moves = tmp(1,:);
for k = 2:num_tmp
    last_col = find(moves(end,:)~=0);
    this_col = find(tmp(k,:)~=0);
    if last_col==this_col
        moves(end,last_col) = moves(end,last_col) + tmp(k,last_col);
    else
        moves = [moves;tmp(k,:)];
    end
end

tch = 0;
