function ladder

global h n m d ly lx p s ss txt

s='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
m=4;
d=0.1;

h.f = figure('menubar','none','numbertitle','off', 'Toolbar','none', 'name','Ladder');

h.e=uicontrol('style','edit','units','normalized','position',[.3 .88 .15 .1], 'string',10);
h.p=uicontrol('style','pushbutton','units','normalized','position',[.55 .88 .15 .1], 'string','갯수입력!!');
set(h.p,'Callback', @in)


function in(hObject, eventdata)
global h n m d s

set(h.p,'string','시작!!')
set(h.e,'visible', 'off')

n=str2num(get(h.e, 'string'));
m=4;
d=0.1;

h.a=axes('Position',[.1 .1 .8 .7], 'XTick',[], 'YTick',[], 'xlim', [1 20], 'ylim', [1 10]);
for i=1:n
    h.ed(i)=uicontrol('style','edit','units','normalized','position',[.15 .78-0.7*(i-1)/n .1 .06], 'string',i);
    h.ed(n+i)=uicontrol('style','edit','units','normalized','position',[.75 .78-0.7*(i-1)/n .1 .06], 'string',s(i));
end
    arr=plot([7 15 Inf],[5 5 Inf], '-k>', 'LineWidth',2, 'Parent', h.a);
    axis([1 20 1 10])
    axis off
    
   
set(h.p,'Callback', @pl)


function pl(hObject, eventdata)

global h n m d ly lx p ss txt

set(h.p,'string','다시!!')

for i=1:n
    ss{i,1}=get(h.ed(i),'string');
    ss{i,2}=get(h.ed(n+i),'string');
end  

delete(h.ed);
delete(h.a);
h.a=axes('Position',[.1 .1 .8 .7], 'XTick',[], 'YTick',[], 'xlim', [1 20], 'ylim', [1 10]);

n=str2num(get(h.e, 'string'));
m=4;
d=0.1;

ly(1)=plot([1 1 Inf],[1 10 Inf], '-ko', 'LineWidth',2);
hold on
txt(n+1)=text(1, 0.2, ss{1,2});
for k = 2:n
ly(k)=plot([k k Inf],[1 10 Inf], '-ko', 'LineWidth',2);
txt(n+k)=text(k, 0.2, ss{k,2});
end

r=randperm(n-1);
for m=2:m
r=[r; randperm(n-1)+(m-1)*(n-1)];
end
rn=round(8*r/m/n,1)+1.5;
for k=1:n-1
for j=1:m
lx(j,k)=plot([k k+1 Inf],[rn(j,k) rn(j,k) Inf],'-ko', 'LineWidth',2);
end
end

axis off

for j=1:n
p(j)=plot(j,10,'o', 'MarkerFaceColor','red', 'MarkerSize',12);
txt(j)=text(j, 10, ss{j,1});
play(j)
end

set(h.p,'Callback', @re)



function play(id)

global n m d lx p ss txt
x=id;

left_Y=zeros(1, m);
right_Y=zeros(1, m);
temp=0;

for y= 10:-d:1
% disp([id x left_Y get(p(id), 'YData') right_Y]);

for i=1:m
if x==1
left_Y=zeros(1, m);
temp=get(lx(i,x), 'YData'); right_Y(i)=temp(1);
elseif x==n
right_Y=zeros(1, m);
temp=get(lx(i,x-1), 'YData'); left_Y(i)=temp(1);
else
    temp=get(lx(i,x-1), 'YData'); left_Y(i)=temp(1);
    temp=get(lx(i,x), 'YData'); right_Y(i)=temp(1);
end;
end

if min(abs(y-left_Y))<10*eps
for x=x:-d:x-1
    set(p(id), 'XData', x);
    set(txt(id), 'position', [x y]);
    drawnow
end
elseif min(abs(y-right_Y))<10*eps
for x=x:d:x+1
    set(p(id), 'XData', x);
    set(txt(id), 'position', [x y]);
    drawnow
end
else
    set(p(id), 'YData', y);
    set(txt(id), 'position', [x y]);
    drawnow
end

end

disp([ss{id,1} ' -> ' ss{x,2}])


function re(hObject, eventdata)
global h
delete(h.a);
in;