function Slither(action)
global fig Maze Row Col sf counter LIJ xb yb uh Sel_Mat Coord Maze_soln;
global ro co rc LinkNum linker CLink;
%--------------------------------------------------------------------------
if nargin < 1,
clc
msg={'Click on the White Lines to solve the puzzle...'};
msg_handle=msgbox(msg,'Slither Link','Help');
pause(3);
close(msg_handle);
Slither('Start');
action='';
end
%--------------------------------------------------------------------------
if strcmp(action,'Start'),
linker=0;
LinkNum=[];
CLink=[];
Row=2+mod(fix(10*rand(1,1)),3);
Col=2+mod(fix(10*rand(1,1)),3);
ro=Row; co=Col;
flag=1;
while flag,
%Defining Maze
sf=4;%Scaling Factor
[Row,Col,Maze]=Maze_Def(Row,Col,sf);
%Arriving at Maze Boundary
Maze_Bounds=Boundary(Maze,Row,Col);
xb=Maze_Bounds(:,1); yb=Maze_Bounds(:,2);
% (xb+1)/sf,(yb+1)/sf,
%Defining SlitherLink Numbers
Maze_Bounds_Fill=0*Maze;
Maze_soln=0*Maze;
for ii=1:length(xb)-1,
if xb(ii)>xb(ii+1),
minr=xb(ii+1); maxr=xb(ii);
else
minr=xb(ii); maxr=xb(ii+1);
end
if yb(ii)>yb(ii+1),
minc=yb(ii+1); maxc=yb(ii);
else
minc=yb(ii); maxc=yb(ii+1);
end
Maze_Bounds_Fill(minr:maxr,minc:maxc)=1;
end
[Maze_Num]=SLN(Row,Col,Maze_Bounds_Fill);
if ~isempty(find(Maze_Num>1)), flag=0; end
end
%Displaying the figure
fig=figure('Name','Slither Link','NumberTitle','off','Visible','off',...
'units','centimeters');
%creating default GUI buttons
pos=linspace(0.25*Row,0.75*Row,4);
uicontrol('units','centimeters','position',[pos(1) 0.5 1 0.75],...
'string','New','callback','Slither(''New'')',...
'interruptible','on','BackgroundColor','w');
uicontrol('units','centimeters','position',[pos(2) 0.5 1 0.75],...
'string','Help','callback','Slither(''Help'')',...
'interruptible','on','BackgroundColor','w');
uh=uicontrol('units','centimeters','position',[pos(3) 0.5 1 0.75],...
'string','Ans.','callback','Slither(''Ans.'')',...
'interruptible','on','BackgroundColor','w');
uicontrol('units','centimeters','position',[pos(4) 0.5 1 0.75],...
'string','Exit','callback','delete(gcf)',...
'interruptible','on','BackgroundColor','w');
figure(fig);
axis([0 1+(Row+1)/sf 0 1+(Col+1)/sf]);
hold on
%creating Line-Link Matrix
Maze_Bounds_Fill_Rot=rot90(Maze_Bounds_Fill);
Lin_Mat=0*Maze_Bounds_Fill_Rot;
for ii=1:size(Maze_Bounds_Fill_Rot,1),
for jj=1:size(Maze_Bounds_Fill_Rot,2),
if (mod(ii,2)==mod(jj,2) && Maze_Bounds_Fill_Rot(ii,jj)),
Lin_Mat(ii,jj)=1;
end
end
end
Sel_Mat=0*Lin_Mat;
%drawing Links
counter=0;
for ii=1:2/sf:ro,
for jj=1-1/sf:2/sf:2/sf+co,
counter=counter+1;
LIJ(counter)=line(ii+[-1/sf 1/sf],jj+[0 0],'color','w','ButtonDownFcn',{@Callback_Line,counter});
Coord(counter,:)=[ii jj];
end
end
rc=counter;
for ii=1:2/sf:co,
for jj=1-1/sf:2/sf:2/sf+ro,
counter=counter+1;
LIJ(counter)=line(jj+[0 0],ii+[-1/sf 1/sf],'color','w','ButtonDownFcn',{@Callback_Line,counter});
Coord(counter,:)=[jj ii];
end
end
%Generating Slither Link Coordinates
addon=0;
county=0;
for ii=1:length(xb)-1,
if xb(ii)>xb(ii+1),
minr=xb(ii+1); maxr=xb(ii);
else
minr=xb(ii); maxr=xb(ii+1);
end
if yb(ii)>yb(ii+1),
minc=yb(ii+1); maxc=yb(ii);
else
minc=yb(ii); maxc=yb(ii+1);
end
minr=(minr+1)/sf; maxr=(maxr+1)/sf;
minc=(minc+1)/sf; maxc=(maxc+1)/sf;
if minr==maxr,
for jj=minc+1/sf:2/sf:maxc,
addon=addon+1;
SLCs(addon,:)=[minr jj]+0/sf;
end
else
for jj=minr+1/sf:2/sf:maxr,
addon=addon+1;
SLCs(addon,:)=[jj minc]+0/sf;
end
end
end
% SLCs
% plot(SLCs(:,1),SLCs(:,2),'r*');
%Linking Lines & Links
for ii=1:size(SLCs,1),
XY=2*SLCs(ii,:)-1;
county=county+1;
if floor(XY(1))==XY(1),
CLink(county)=2*co*(XY(1)-1)+floor(XY(2))+1;
else
CLink(county)=rc+2*ro*(XY(2)-1)+floor(XY(1))+1;
end
end
CLink=sort(CLink);
%Answer
% line((xb+1)/sf,(yb+1)/sf,'color','b');
% for ii=2:2:Row,
% for jj=2:2:Col,
% plot((ii+1)/sf,(jj+1)/sf,'r*');
% end
% end
hold off
axis off;
axis square;
axis equal;
%Display Slither Link Numbers
for ii=3:2:Row,
for jj=3:2:Col,
text((ii+1)/sf,(jj+1)/sf,sprintf('%d',Maze_Num(ii,jj)),'color','k');
end
end
end
%--------------------------------------------------------------------------
if strcmp(action,'New'),
closereq;
Slither('Start');
end
%--------------------------------------------------------------------------
if strcmp(action,'Help'),
scrsz = get(0,'ScreenSize');
Helpfig=figure('Name','Help','NumberTitle','off','Visible','off','BackingStore','off');
figure(Helpfig);
image(imread('Help.jpg'));
set(Helpfig,'Position',[scrsz(3)/5 scrsz(3)/5 scrsz(3)/1.8 scrsz(4)/1.8]);
axis off
if ~waitforbuttonpress, close(Helpfig); end
end
%--------------------------------------------------------------------------
if strcmp(action,'Ans.'),
if strcmp(get(uh,'string'),'Ans.'),
%un-drawing Links
for ii=1:linker,
set(LIJ(LinkNum(ii)),'color','w');
end
for ii=1:length(CLink),
set(LIJ(CLink(ii)),'color','r');
end
set(uh,'string','back','BackgroundColor','y');
else
for ii=1:length(CLink),
set(LIJ(CLink(ii)),'color','w');
end
for ii=1:linker,
set(LIJ(LinkNum(ii)),'color','k');
end
set(uh,'string','Ans.','BackgroundColor','w');
end
end
%--------------------------------------------------------------------------
if strcmp(action,'Exit'),
closereq;
end
%--------------------------------------------------------------------------