image thumbnail

GUI for Hybrid Sudoku Solver

by

 

A simple GUI to solve Sudoku puzzles. Uses both logical and bruteforce methods.

[bool, C, iter]=BruteForce(C,index,empty,iter,PosVal)
%%%%%%%%%%Programmed by: Chi-Hang Kwan%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Creation Date: November 3, 2012%%%%%%%%%%%%%%%%%%%%
function [bool, C, iter]=BruteForce(C,index,empty,iter,PosVal)

iter = iter+1;
blank=0;
for n=1:81
    if C(n)==0;
        blank = blank+1;
    end
end

if blank == 0 %check to see if we are done
    bool=1;
    return
end


for num=1:9
    if PosVal(index,num)==1 %limit the guessing to what's possible
    if NoConflict(C,empty(index),num) %if no conflicts, accept
        C(empty(index))=num;
        [bool,C,iter]= BruteForce(C,index+1,empty,iter,PosVal); %call itself with new start value
        if bool==1 %if done, leave Bruteforce
            return
        end
        C(empty(index))=0;
    end
    end
end

bool=0; %Default is to return with bool=0 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%Function to Check Conflicts%%%%%%%%%%%%%%%%%%%%

function bool = NoConflict(C,n,num)


Col = floor((n-1)/9)+1; 
Row = rem(n-(Col-1)*9,10);

for ii=1:9
    if C(ii,Col)==num
        bool=0;
        return
    end
end

for jj=1:9
    if C(Row,jj)==num
        bool=0;
        return
    end
end

RemR = rem((Row-1),3);
RemC = rem((Col-1),3);

RowL = Row - RemR;
ColL = Col - RemC;


for ii=RowL:RowL+2
    for jj=ColL:ColL+2
       if C(ii,jj)==num
            bool=0;
            return
       end
    end
end

bool=1;


Contact us