function Number_Puzzle(N_Obj,k)
A=1;
try
if(k==1)
A=1;
end
catch
A=2;
end
if(A==1),Solve_Puzzle_part(N_Obj);
else Puzzle(N_Obj); end
end
function Puzzle(N)
nameofmainfig = 'Puzzle';
X1 = 100;
Y1 = 100;
X2 = N*50;
Y2 = N*50;
Main_Pos_1=[X1 Y1 X2 Y2];
allpanelshandle = figure('Name',nameofmainfig,'NumberTitle','off','Resize','off','Toolbar','none','Menubar','none','units','Pixels','Position',[X1 Y1 X2 Y2]);
% subpanelshandle_main = uipanel('Parent',allpanelshandle,'units','Pixels','Tag',[nameofmainfig,'_R1'],'Position',[0 0 X2 Y2],'FontSize',8,'BackgroundColor',[0 0.6 0.6]);
MyPuzzle = {};
Valus = zeros(1,N*N);
for ii = 1 : N
for jj = 1 : N
AA = (ii-1)*50;
BB = (jj-1)*50;
k = floor(rand(1,1)*N*N);
while (any(ismember(Valus,k)) || k == 0)
k = round(rand(1,1)*N*N);
end
if k == N*N
Valus((ii-1)*N+(jj-1)+1) = k;
MyPuzzle{ii,jj}=uicontrol('Parent',allpanelshandle,'units','Pixels','FontWeight','bold','Style','PushButton','String','','position',[AA BB 50 50],'BackgroundColor',[0 0.6 0.6]);
UD.BlankX = ii;
UD.BlankY = jj;
else
Valus((ii-1)*N+(jj-1)+1) = k;
MyPuzzle{ii,jj}=uicontrol('Parent',allpanelshandle,'units','Pixels','FontWeight','bold','Style','PushButton','String',num2str(k),'position',[AA BB 50 50]);
end
set(MyPuzzle{ii,jj},'Callback',['Number_Puzzle(gcbo,1)'])
end
end
UD.MyPuzzle = cell2mat(MyPuzzle);
UD.N = N;
set(allpanelshandle,'UserData',UD);
% saveas(allpanelshandle,'ds')
end
function Solve_Puzzle_part(Obj)
Parent = get(Obj,'Parent');
UD = get(Parent,'UserData');
[A B] = find(ismember(UD.MyPuzzle,Obj));
if(UD.BlankX == A && UD.BlankY ~= B)
if(UD.BlankY>B)
K = UD.BlankY : -1 : B;
else
K = UD.BlankY : B;
end
Prop.BC = get(UD.MyPuzzle(A,K(1)),'BackgroundColor');
Prop.String = get(UD.MyPuzzle(A,K(1)),'String');
for i = 1 : length(K)-1
set(UD.MyPuzzle(A,K(i)),'BackgroundColor',get(UD.MyPuzzle(A,K(i+1)),'BackgroundColor'));
set(UD.MyPuzzle(A,K(i)),'String',get(UD.MyPuzzle(A,K(i+1)),'String'));
disp('Row Input')
end
set(UD.MyPuzzle(A,K(end)),'BackgroundColor',Prop.BC);
set(UD.MyPuzzle(A,K(end)),'String',Prop.String);
UD.BlankX = A;
UD.BlankY = B;
set(Parent,'UserData',UD);
isPuzzleComplete(UD.MyPuzzle,Parent,UD.N);
elseif(UD.BlankY == B)
if(UD.BlankX>A)
K = UD.BlankX : -1 : A;
else
K = UD.BlankX : A;
end
Prop.BC = get(UD.MyPuzzle(K(1),B),'BackgroundColor');
Prop.String = get(UD.MyPuzzle(K(1),B),'String');
for i = 1 : length(K)-1
set(UD.MyPuzzle(K(i),B),'BackgroundColor',get(UD.MyPuzzle(K(i+1),B),'BackgroundColor'));
set(UD.MyPuzzle(K(i),B),'String',get(UD.MyPuzzle(K(i+1),B),'String'));
disp('Column Input')
end
set(UD.MyPuzzle(K(end),B),'BackgroundColor',Prop.BC);
set(UD.MyPuzzle(K(end),B),'String',Prop.String);
UD.BlankX = A;
UD.BlankY = B;
set(Parent,'UserData',UD);
isPuzzleComplete(UD.MyPuzzle,Parent,UD.N);
else
disp('Wrong Input')
end
end
function isPuzzleComplete(data,parent,N)
val=fliplr(reshape(get(data,'string'),N,N));
tf=true;
for i = 1:((N^2)-1)
if ~strcmp(num2str(i),val{i}),tf=false; end
end
if tf==true
try
button = questdlg(['Congratulations!!!' char(10) 'Do you want to continue??'],'Congratulations!','Yes','No','Yes');
if strcmp(button,'Yes')
answer = inputdlg(['Enter the size of the puzzle you want to play' char(10)],'What size?',1);
if str2num(answer{1})>2
delete(data);
delete(parent);
Number_Puzzle(str2num(answer{1}))
return;
end
else
delete(parent);
end
catch
close all;
end
end
end