Code covered by the BSD License  

Highlights from
Sudoku

image thumbnail

Sudoku

by

 

14 Oct 2010 (Updated )

Solves 9X9 Sudokus of any difficulty level

[b,d]=mupdate(b,d)
function [b,d]=mupdate(b,d)
%update b
%check for only solutions....
%part of project: sudoku

for j=1:9
    for i=1:9
        n2=0;
        for x=1:9                               %within columns
            if d(x,i,j)==0                      
                n2=n2+1;
            end
        end    
        if n2==8
            for x=1:9
                if d(x,i,j)~=0
                    b(x,j)=d(x,i,j);
                end
            end
        end
        for x=1:9                                %within cells
            n3=0;
            for k=(3*ceil(i/3)-2):(3*ceil(i/3)) 
                for l=(3*ceil(j/3)-2):(3*ceil(j/3))
                    if d(k,x,l)==x
                            n3=n3+1;
                            m=k;n=l;
                    end
                end
            end
            if n3==1
                b(m,n)=x;
                d(m,:,n)=0;
                d(m,x,n)=x;
            end
        end             
    end
end

%updating b with sure elements
for j=1:9
    for i=1:9
        n1=0;
        for x=1:9
            if d(i,x,j)==0
                n1=n1+1;            %check for sure element
            end
        end
        if n1==8
            for x=1:9               %find value of sure element
                if d(i,x,j)~=0;
                    b(i,j)=d(i,x,j);%updated b for sure elements
                end
            end
        end
    end
end

Contact us