function GUI_sudoku(akce)
global tab;
if(nargin == 0)
    akce = 'init';
end

switch(akce)
    case 'init'
        figure('position',[200 200 500 500])
        x = linspace(0,9/10-0.2,9);
        for i = 1:9
            for j = 1:9
                tab(i,j) = uicontrol('units','normalized','style','edit','position',[0.1+x(i) 0.1+x(j) 0.07 0.07],'fontsize',14);
                uicontrol('units','normalized','style','pushbutton','position',[0.27 0.01 0.2 0.08],'fontsize',14,...
                          'string','SOLVE','callback','GUI_sudoku(''solve'')');
                uicontrol('units','normalized','style','pushbutton','position',[0.47 0.01 0.2 0.08],'fontsize',14,...
                          'string','reset','callback','GUI_sudoku(''reset'')');      
                uicontrol('units','normalized','style','pushbutton','position',[0.67 0.01 0.2 0.08],'fontsize',14,...
                          'string','konec','callback','delete(gcf)');
                % cary
                uicontrol('Style','text', 'Units','normalized', 'Position',[0.1 0.35 0.77 0.005],'background','k');
                uicontrol('Style','text', 'Units','normalized', 'Position',[0.1 0.615 0.77 0.005],'background','k');
                uicontrol('Style','text', 'Units','normalized', 'Position',[0.35 0.1 0.005 0.77],'background','k');
                uicontrol('Style','text', 'Units','normalized', 'Position',[0.615 0.1 0.005 0.77],'background','k');
            end
        end
        
    case 'solve'
        tic;
        M = zeros(9,9);
        for i = 1:9
            for j = 1:9
                str = get(tab(i,j),'string');
                if(isempty(str) == 1)
                    M(i,j) = 0;
                else
                    M(i,j) = str2double(str);
                end
            end
        end
        [M,nalezeno] = sudoku(M);
        if(nalezeno)
            for i = 1:9
                for j = 1:9
                    str = get(tab(i,j),'string');
                    if(isempty(str) == 1)
                        set(tab(i,j),'string',num2str(M(i,j)),'foregroundcolor','r');
                    end
                end
            end
        else
            msgbox('Reseni nenalezeno','','error');
        end
        
     case 'reset'
         for i = 1:9
            for j = 1:9
                set(tab(i,j),'string','');
                set(tab(i,j),'foregroundcolor','k');
            end
         end
end



% Resic sudoku_____________________________________________________________
function [v_out,nalezeno] = sudoku(v_in)

% nalezeni prvniho prazdneho mista, postupuje se po jednotlivych radkach
stop = 0;
for i0 = 1:9
    for j0 = 1:9
        if(v_in(i0,j0) == 0)
            stop = 1;
            break;
        end
    end
    if(stop)
        break;
    end
end

% zastavovaci podminky
if(i0 == 9 && j0 == 9)
    moznosti = ones(9,1);
    for i = 1:9
        if(v_in(i,9) ~= 0)
            moznosti(v_in(i,9)) = 0;
        end
    end
    for i = 1:9
        if(moznosti(i) == 1)
            v_in(9,9) = i;
        end
    end
    v_out = v_in;
    nalezeno = 1;
    return;
end

if(toc > 3) % reseni se nenalezlo v danem case
    v_out = v_in;
    nalezeno = 0;
    return;
end

% nalezeni nekonfliktnich cisel
% horizontalne
moznosti = ones(9,1);
for i = 1:9
    if(v_in(i,j0) ~= 0)
        moznosti(v_in(i,j0)) = 0;
    end
end

% vertikalne
for j = 1:9
    if(v_in(i0,j) ~= 0)
        moznosti(v_in(i0,j)) = 0;
    end
end

% ctverec
i1 = 3*fix((i0-1)/3);
j1 = 3*fix((j0-1)/3);
for i = 1:3
    for j = 1:3
        if(v_in(i+i1,j+j1) ~= 0)
            moznosti(v_in(i+i1,j+j1)) = 0;
        end
    end
end

% slepa cesta
if(sum(moznosti) == 0)
    v_out = v_in;
    nalezeno = 0;
    return;
end

% volani rekurze
for k = 1:9
    if(moznosti(k) == 1)
        v_in(i0,j0) = k;
        [v_out,nalezeno] = sudoku(v_in);
        if(nalezeno == 1)
            break;
        end
    end
end
